c->;从dll使用本机函数时的c#数据类型转换
这个清单对吗 unsigned int(c) -> uint(c#) const char*(c) -> String(c#) unsigned int*(c) -> uint[](c#) unsigned char*(c) -> byte[](c#) 平沃克是:c->;从dll使用本机函数时的c#数据类型转换,c#,types,pinvoke,C#,Types,Pinvoke,这个清单对吗 unsigned int(c) -> uint(c#) const char*(c) -> String(c#) unsigned int*(c) -> uint[](c#) unsigned char*(c) -> byte[](c#) 平沃克是: [DllImport(@"lib.dll", EntryPoint = "something")]<br> public static extern bool something( byte[] a, ui
[DllImport(@"lib.dll", EntryPoint = "something")]<br>
public static extern bool something(
byte[] a,
uint a_length,
byte[] b,
uint[] b_length);
[DllImport(@“lib.dll”,EntryPoint=“something”)]
公共静态外部文件(
字节[]a,
单位长度,
字节[]b,
uint[]b_长度);
首先,是你的朋友
其次,您的转换是正确的,除了您应该使用StringBuilder
来处理将char*
作为缓冲区填充的函数([in out]
)
堆栈不平衡可能是由于使用了不同的调用约定。C的默认调用约定是\uu stdcall
,但您的C函数可能是\uu cdecl
。如果是这种情况,则需要将添加到DLLImport
属性中
编辑:同样,正如格罗所指出的,如果C函数中的指针参数实际上只是指向
无符号int
(例如,与预期的int
数组相反),那么应该使用ref-uint
而不是int[]
为什么最后一个字符*是字节?你能分享你的pinvoke吗?最后一个无符号字符*因此,你应该发布C函数原型和C#P/Invoke签名,这样我们就可以看到你在说什么。只需显示你的代码和本机函数声明。对于第二个参数,将其封送为以null结尾的字符串(MarshalAs)是有意义的(UnmanagedType.LPStr)
attribute),但您应该确保最后两个参数实际上是数组,而不仅仅是引用(因为在封送处理之前,您必须知道数组大小,以便在托管大小上分配它们)。发布两个签名并解释该方法的作用将非常有帮助。我添加了CallingConvention=CallingConvention.Cdecl,现在它工作正常。但是,我仍然感到焦虑。无论如何,谢谢。
[DllImport(@"lib.dll", EntryPoint = "something")]<br>
public static extern bool something(
byte[] a,
uint a_length,
byte[] b,
uint[] b_length);