tags:

views:

1382

answers:

2

I am importing the CreateICeeFileGen() function from the unmanaged DLL mscorpe.dll in a C# application, in order to generate a PE file. This function returns a pointer to an C++ object defined here, is there any way I can access fields from this class via C# or do I need to write an unmanaged wrapper DLL?

The code I'm using currently is as follows:-

[DllImport(@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorpe.dll", EntryPoint = "CreateICeeFileGen")]
static extern void CreateICeeFileGen(out IntPtr iceeFileGenPointer);
...
IntPtr filePtr;
CreateICeeFileGen(out filePtr);

N.B.: I know you can do similar things with .net libraries but I need to use unmanaged libraries for my purposes.

Thanks in advance!!

+1  A: 

You need a wrapper library to be able to use the class from C#.

The best bet would be to create the wrapper using C++/CLI, which can directly call the unmanaged function and expose the details with a managed class. This will eliminate the need to use P/Invoke for anything.

(Well, technically if you know the class layout you could mess around with pointer arithmetic to access the fields, but this would be very fragile and messy, and trying to call virtual functions in this way would be extremely ugly).

Rob Walker
Thank you very much, works well, however since I want to do COM interop I'd prefer it to act as a very small wrapper around ICeeFileGen passing in a pointer. Unfortunately this does mean my library has to use unsafe code... grrr... anyway! It works! Thank you!!
kronoz
A: 

It looks like COM class/interface. Can you not just use COM instead?

leppie
It's not COM as far as I can see as it's defined as a C++ class rather than a COM interface, though confusingly it prefixes the class name with I. It would be a lot easier it was using COM...!
kronoz