开发者

Passing an array of strings from managed C# to unmanaged function using P-Invoke

开发者 https://www.devze.com 2023-04-08 06:08 出处:网络
Is it possible to pass a string array from managed C# to an unmanaged function using P-Invoke? This works fine: 开发者_C百科

Is it possible to pass a string array from managed C# to an unmanaged function using P-Invoke?

This works fine:

开发者_C百科
[DllImport("LibraryName.dll")]
private static extern void Function_Name(string message);

While this:

[DllImport("LibraryName.dll")]
private static extern void Function_Name(string[] message);

fails with

Unhandled exception: System.NotSupportedException: NotSupportedException

I have tried using MarshalAs with no luck ([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] String[] dataToLoadArr)

Is it possible to pass string arrays this way?


[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)

This works fine in my OpenCL library, OpenCL.NET (http://openclnet.codeplex.com/SourceControl/changeset/view/94246#1251571). Note that I am passing in the count using SizeParamIndex as well.

0

精彩评论

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

关注公众号