gpt4 book ai didi

c# - 使用 P-Invoke 将字符串数组从托管 C# 传递到非托管函数

转载 作者:行者123 更新时间:2023-11-30 12:51:08 25 4
gpt4 key购买 nike

是否可以使用 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);

失败了

Unhandled exception: System.NotSupportedException: NotSupportedException

我曾尝试使用 MarshalAs 但没有成功 ([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] String[] 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 传递计数。

关于c# - 使用 P-Invoke 将字符串数组从托管 C# 传递到非托管函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7570044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com