开发者

c -> c# data types conversion when using native functions from dll

开发者 https://www.devze.com 2023-03-11 15:10 出处:网络
Is this list correct? unsigned int(c) -> uint(c#) const char*(c) -> String(c#) unsigned int*(c) -> uint[](c#)

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[].

0

精彩评论

暂无评论...
验证码 换一张
取 消