【发布时间】:2017-02-24 16:06:37
【问题描述】:
我正在为 Arma 3 编写一个扩展。通常你可以简单地为此编写一个 C++ 库并实现这个功能
int __stdcall RVExtensionArgs(char *output, int outputSize, const char *function, const char **args, int argCnt);
我正在开发一个 C# 库,我想在其中实现这个功能。函数 RVExtensionArgs 有一个修饰名称,对于 32 位是 _RVExtensionArgs@20,对于 64 位是 RVExtensionArgs
我正在努力处理指向 args 参数的 const char 指针的指针。我目前的实现是
#if WIN64
[DllExport("RVExtensionArgs", CallingConvention = CallingConvention.Winapi)]
#else
[DllExport("_RVExtensionArgs@20", CallingConvention = CallingConvention.Winapi)]
#endif
public static int RvExtensionArgs(StringBuilder output, int outputSize,
[MarshalAs(UnmanagedType.LPStr)] string function,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4, ArraySubType = UnmanagedType.LPStr)] string[] args,
int argCount)
使用的 DllExport 来自 UnmanagedExports
有一些关于here的扩展信息
但是这样游戏就崩溃了。我很确定这是因为 args 但我无法验证。我错过了什么吗?
【问题讨论】:
-
将其作为 byte* 输出、int outputSize、byte* 函数、byte** args、int argCnt 导入到不安全的类中以消除猜测是否会更简单、更高效?编组?唯一的“挑战”是将 .NET 字符串中的 UTF-16 字符转换为字节。
-
@hoodaticus 我对我得到的每一个解决方案都持开放态度。你能告诉我更多关于如何实施你的想法的信息吗?
-
等等 - 为什么在 C++ 中使用 stdcall 时要指定 WINAPI 调用约定?
-
@hoodaticus 是的,我做到了,而且效果很好!我只需要弄清楚如何将字节**转换为字符串数组
-
@chris579 - 我会这样做并更新你。
标签: c# marshalling