Calling a Windows DLL method (C++) from C#, passing a structurereference.

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Hi all,

I am trying to call a method in a library (a windows DLL written in C+
+ (NOT COM)) from my C# code.

The external library function signature looks like this:

i32 LibraryMethod ( void *Proxy, i8 *Path, i32 *Total, CustomType
*List);

Where we have the definitions like:

typedef signed char i8;
typedef signed int i32;

typedef struct _CustomType {
// Some internals...
} CustomType;


Now, in my C# code, I need to make a call to LibraryMethod and I have
to pass the "CustomType" structure. How do I do that? I tried the
following:

1. Created an equivalent structure in C#, as below

[StructLayout(LayoutKind.Explicit)]
unsafe public struct CustomType
{
// Some internals...
}

and tried passing this as an out parameter. Simply doesn't work.

2. Passed a byte array. But then realized that the called method won't
be intelligent enough to write in a byte stream.

3. Passed a void *. Used the code below:

[DllImport(@"mylibrary.dll")]
private static extern unsafe int LibraryMethod(void* Proxy, char[]
Path, out int total, void* MyStructList);

public unsafe static CustomType[] MyMethod(string path)
{
// Call LibraryMethod here.
}

Now, this also doesn't work as some unblittable types detected. (The
struct in C++ has a pointer to the next struct in the list, and I am
using a void * here. Don't know what else to use.)


Please suggest me.
 
Forgot to write one thing:

The parameter "List" is an out parameter, so LibraryMethod populates
it.
 
Back
Top