c -> c# data types conversion when using native functions from dll
Is this list correct?
unsigned int(c) -> uint(c#) const char*(c) -> String(c#) unsigned int*(c) -> uint[](c#) unsigned char*(c) -> byte[](c#)
I think there's a mistake here because开发者_如何转开发 with these 4 parameters for native function I have PInvokeStackImbalance.
C function is:
bool something
(unsigned char *a,
unsigned int a_length,
unsigned char *b,
unsigned int *b_length);
PInvoke is:
[DllImport(@"lib.dll", EntryPoint = "something")]<br>
public static extern bool something(
byte[] a,
uint a_length,
byte[] b,
uint[] b_length);
First, PInvoke.net is your friend.
Second, You conversions are correct except that you should use a StringBuilder
for functions that take a char*
as a buffer to fill ([in out]
).
Your stack imbalance may be due to the use of different calling conventions. The default calling convention for C# is __stdcall
, but your C function is probably __cdecl
. If that is the case you will need to add the CallingConvention to your DLLImport
attribute.
EDIT: Also, as Groo pointed out, if the pointer arguments in your C function are actually just pointers to unsigned int
(for example, as opposed to expecting an array of int
) then you should use ref uint
instead of an int[]
.
精彩评论