The MASM Forum

General => The Workshop => Topic started by: K_F on July 04, 2015, 06:54:52 AM

Title: Calling C# dll exports from asm
Post by: K_F on July 04, 2015, 06:54:52 AM
Does anyone know of any Asm examples for calling C# dll exports dynamically.. ?
A C++ example would be ok as well,  I can convert it.

Thanks
:idea:
Title: Re: Calling C# dll exports from asm
Post by: Tedd on July 05, 2015, 11:00:24 PM
Using COM: https://msdn.microsoft.com/en-us/library/zsfww439.aspx (https://msdn.microsoft.com/en-us/library/zsfww439.aspx)
Title: Re: Calling C# dll exports from asm
Post by: K_F on July 05, 2015, 11:28:08 PM
Yeah.. I've been looking at that - thought there might be an Asm example lying around... not to worry though I'll make one.
:biggrin:
Title: Re: Calling C# dll exports from asm
Post by: Zen on July 07, 2015, 03:41:27 AM
VAN,
You're referring to an exported .NET Framework class ???
VERTOGRAD wrote an exploratory app about a year ago: Finally I did it ... (http://masm32.com/board/index.php?topic=2106.msg23169#msg23169)
VERTOGRAD explains in great detail: Here's the sequence of calls for obtaining the pointer to virtual functions table of _AppDomain interface,... (http://masm32.com/board/index.php?topic=2106.msg23189#msg23189)
Title: Re: Calling C# dll exports from asm
Post by: K_F on July 07, 2015, 07:45:48 AM
Actually I was thinking of writing a small mod for a game.. which unfortunately is written in C# (heaven knows why ?)
The thing is that I'm not sure whether the game exports functions outside .NET..

So I'm going to have to fiddle with COM and check if the GUIs are available.
The developers supply the interface (API) code for modders and I can extract the GUIs with IDA.
I'll give it a try.. otherwise I have to resort to ....ssshhh... keep quiet...Scripting in C# :-)
:shock: :icon_rolleyes: :greensml:
Title: Re: Calling C# dll exports from asm
Post by: Zen on July 08, 2015, 06:35:18 AM
Hi, VAN,   
DirectX can be accessed as managed (.NET Framework) code,...which is probably why the game was written in C#.
I just assumed that since you are interoperating with a C# module that it was a .NET Framework class or component. The .NET Framework is designed so that you can execute native code from within the C# module (https://msdn.microsoft.com/en-us/library/ms717435(v=vs.100).aspx). The whole Interop syntax (it was engineered primarily for COM and NET interoperability) is a pain in the ass,...and, very easy to get wrong. I don't even know how you would do it in MASM (but, I'll bet EDGAR or QWORD does). I assume that it's possible, but, prototyping the various C# routines would be a challenge.
It's been years since I wrote any NET Framework code, but, as I recall there is a utility that uses MetaData to disassemble a NET Framework app.
I think it's: (Ildasm.exe, IL Disassembler (https://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.110).aspx)).

These sites might be helpful:
Ildasm.exe Tutorial, MSDN (https://msdn.microsoft.com/en-us/library/aa309387(v=vs.71).aspx)
Reflection in the .NET Framework, MSDN (https://msdn.microsoft.com/en-us/library/f7ykdhsy(v=vs.110).aspx)
Interoperating with Unmanaged Code. MSDN (https://msdn.microsoft.com/en-us/library/sd10k43k(v=vs.110).aspx)