我正在编写一个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)
{
}
发布于 2016-12-14 22:11:59
这允许传递string[],但这只是一种方式。
[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);
}
}
https://stackoverflow.com/questions/41132137
复制相似问题