C# - Calling unmanaged DLL from managed C# code.

G

Guest

I have a DLL that i'm interfacing with that's in C++. here's the interface
defn:
SendCommand(void *h, char *cmd, char *data, char *respbuffer);

here's the wrapper for the dll call in my C# code:
[DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "SendCommand")]
unsafe public static extern char* SendCommand(void* h,
[MarshalAs(UnmanagedType.LPStr)] string cmd, [MarshalAs(UnmanagedType.LPStr)]
string data, [MarshalAs(UnmanagedType.LPArray)] ref byte[] buffer);

and here's my function call:


//code here...
byte buf = new byte[25];
SendCommand(handle, "cmd string", "data", buf);

i think the cmd parameter is getting messed up when i marshal it as LPStr!!
am i doing something wrong? the reason i say this is i do not get any
response in the response buffer!!!

i could get the same function call working in C++ and i do see the response
in the buffer...can someone please help?
 
W

Willy Denoyette [MVP]

rkpat said:
I have a DLL that i'm interfacing with that's in C++. here's the interface
defn:
SendCommand(void *h, char *cmd, char *data, char *respbuffer);

here's the wrapper for the dll call in my C# code:
[DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "SendCommand")]
unsafe public static extern char* SendCommand(void* h,
[MarshalAs(UnmanagedType.LPStr)] string cmd, [MarshalAs(UnmanagedType.LPStr)]
string data, [MarshalAs(UnmanagedType.LPArray)] ref byte[] buffer);

and here's my function call:


//code here...
byte buf = new byte[25];
SendCommand(handle, "cmd string", "data", buf);

i think the cmd parameter is getting messed up when i marshal it as LPStr!!
am i doing something wrong? the reason i say this is i do not get any
response in the response buffer!!!

i could get the same function call working in C++ and i do see the response
in the buffer...can someone please help?



Why the char* in :
extern char* SendCommand
there is no return defined in your C function.

Try this:
DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "SendCommand")]
public static extern void SendCommand(IntPtr h, string cmd, string data, StringBuilder
buffer);

IntPtr handle = ....;
StringBuilder buf = new StringBuilder(25);
SendCommand(handle, ....);

Willy.
 
G

Guest

thanks for the reply Willy.

actually, the sendcommand function looks like this (my bad - typo)
char * SendCommand(void *h, char *cmd, char *data, char *respbuffer);
i'll try the recommended changes and will let u know how it goes. THANKS.


Willy Denoyette said:
rkpat said:
I have a DLL that i'm interfacing with that's in C++. here's the interface
defn:
SendCommand(void *h, char *cmd, char *data, char *respbuffer);

here's the wrapper for the dll call in my C# code:
[DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "SendCommand")]
unsafe public static extern char* SendCommand(void* h,
[MarshalAs(UnmanagedType.LPStr)] string cmd, [MarshalAs(UnmanagedType.LPStr)]
string data, [MarshalAs(UnmanagedType.LPArray)] ref byte[] buffer);

and here's my function call:


//code here...
byte buf = new byte[25];
SendCommand(handle, "cmd string", "data", buf);

i think the cmd parameter is getting messed up when i marshal it as LPStr!!
am i doing something wrong? the reason i say this is i do not get any
response in the response buffer!!!

i could get the same function call working in C++ and i do see the response
in the buffer...can someone please help?



Why the char* in :
extern char* SendCommand
there is no return defined in your C function.

Try this:
DllImport("test.dll", CharSet = CharSet.Ansi, EntryPoint = "SendCommand")]
public static extern void SendCommand(IntPtr h, string cmd, string data, StringBuilder
buffer);

IntPtr handle = ....;
StringBuilder buf = new StringBuilder(25);
SendCommand(handle, ....);

Willy.
 

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