problem w/ returned char pointer from unmanaged c++ dll

G

Guest

have an unmanaged C++ DLL with following method:

the DLL interface for unmanaged C++ is:
SendCommand(void *h, char *c, char *d, char *b);

the declaration in C# is:
[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);

when i call the function
byte buf = new byte[25];
SendCommand(r, "test string", "", buf);

The problem is that buf should have a value in it when the call is
completed. However buf is empty. I have another function which only accepts a
byte array as the input and it is a char * on the c++ side and that returns
valid values in the byte array. So I am guessing the problem could be with
the passing of the strings as the SendCommand method will return correct
values when it understands the strings correctly. The SendCommand method
works correctly if I make the call using C++ code but does not work correctly
from C#. Could be LPSTR not be working correctly as it is supposed to?

THANKS
 
M

Mattias Sjögren

C0d3r (or should I call you rkpat?)

In addition to what Willy wrote in reply to your previous message...
the declaration in C# is:
[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); [...]
SendCommand(r, "test string", "", buf);

This shouldn't even compile, since the last parameter is a ref
parameter in the method declaration, but not used as such on the call
site.

Try removing the ref modifier.


Mattias
 
W

Willy Denoyette [MVP]

Mattias Sjögren said:
C0d3r (or should I call you rkpat?)

In addition to what Willy wrote in reply to your previous message...
the declaration in C# is:
[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); [...]
SendCommand(r, "test string", "", buf);

This shouldn't even compile, since the last parameter is a ref
parameter in the method declaration, but not used as such on the call
site.

Try removing the ref modifier.


Mattias
 
W

Willy Denoyette [MVP]

Mattias Sjögren said:
C0d3r (or should I call you rkpat?)

In addition to what Willy wrote in reply to your previous message...

Guess rkpat died, therefore, could not read the reply to it's previous post, hope his
reincarnation will do ;-).

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