我正在为C ++项目编写一个插件,我正在利用UnmanagedExports Nuget包,它允许在托管的.NET代码中暴露C函数。 https://www.nuget.org/packages/UnmanagedExports
我编写了一个接收字符串的插件(在c ++中定义为char *) 下面是我为此定义的UnmanagedExport方法。
[DllExport("GetString", CallingConvention = CallingConvention.Cdecl)]
public static void GetString(StringBuilder MyString)
{
//Use and modify the StringBuilder. It receives the string passed and returns the modified version because it is being passed by reference.
}
上面的代码运行得很漂亮。
现在问题是如何将字符串数组传递给我的UnmanagedExport代码。 C ++将调用定义为需要char * []
这不起作用。
[DllExport("GetString", CallingConvention = CallingConvention.Cdecl)]
public static void GetString(StringBuilder[] MyString)
{
}
答案 0 :(得分:0)
这允许传递一个字符串[],但它只是一种方式。
[DllExport("GetStrings", CallingConvention = CallingConvention.Cdecl)]
public static void GetStrings([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]string[] MyStrings, int size)
{
foreach(var s in MyStrings)
{
MessageBox.Show(s);
}
}