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

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.
 
J

JJ

Forgot to write one thing:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top