使用P-Invoke将字符串数组从托管C#传递到非托管函数
是否可以使用p-Invoke将字符串数组从托管C#传递到非托管函数 这很好:使用P-Invoke将字符串数组从托管C#传递到非托管函数,c#,.net,pinvoke,marshalling,unmanaged,C#,.net,Pinvoke,Marshalling,Unmanaged,是否可以使用p-Invoke将字符串数组从托管C#传递到非托管函数 这很好: [DllImport("LibraryName.dll")] private static extern void Function_Name(string message); 而这: [DllImport("LibraryName.dll")] private static extern void Function_Name(string[] message); 失败于 未处理的异常:System.NotSupp
[DllImport("LibraryName.dll")]
private static extern void Function_Name(string message);
而这:
[DllImport("LibraryName.dll")]
private static extern void Function_Name(string[] message);
失败于
未处理的异常:System.NotSupportedException:NotSupportedException
我尝试过使用marshallas
,但运气不佳([marshallas(UnmanagedType.LPArray,ArraySubType=UnmanagedType.LPWStr)]字符串[]dataToLoadArr)
可以这样传递字符串数组吗
[DllImport(Library)]
private static extern IntPtr clCreateProgramWithSource(Context context,
cl_uint count,
[In] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr, SizeParamIndex = 1)] string[] strings,
[In] [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.SysUInt, SizeParamIndex = 1)] IntPtr[] lengths,
out ErrorCode errcodeRet);
public static Program CreateProgramWithSource(Context context,
cl_uint count,
string[] strings,
IntPtr[] lengths,
out ErrorCode errcodeRet)
这在我的OpenCL库OpenCL.NET中运行良好(http://openclnet.codeplex.com/SourceControl/changeset/view/94246#1251571). 请注意,我也在使用SizeParamIndex传递计数。异常消息是什么?另外,非托管函数的声明是什么?有帮助吗?谢谢,mtijn,这很有帮助。通过使用表示要封送的字符串的IntPtr结构来解决此问题。尝试此方法时效果很好,不需要手动封送。不过,这种方法存在一个严重的缺陷,本地代码无法判断数组有多大。需要一个额外的参数。您使用的是什么版本的Windows?这是Windows CE还是类似的东西?