Possible Duplicate:
Passing char pointer from C# to c++ function
I have this type of problem:
I have a c++ function with this signature:
int myfunction ( char* Buffer, int * rotation)
The buffer parameter must be filled with space chars ( 0x20 hex )
In C++ i can solve the problem simply doing this:
char* buffer = (char *)malloc(256);
memset(buffer,0x20,256);
res = myfunction (buffer, rotation);
I’m trying to call this function from C#.
This is my p/invoke declaration:
[DllImport("mydll.dll", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern unsafe int myfunction (StringBuilder Buffer, int* RotDegree);
In my C# class i’ve tried to do this:
StringBuilder buffer = new StringBuilder(256);
buffer.Append(' ', 256);
...
myfunction(buffer, rotation);
but it doesn’t work….
Anyone can help me?
Thanks.
Your p/invoke doesn’t look quite right. It should (presumably) use the
Cdeclcalling convention. You should not useSetLastError. And there’s no need for unsafe code.I would write it like this:
Then call it like this:
I didn’t specify the
CharSetsinceAnsiis the default.