我有一个用 VC++6 编写的本机动态链接库,但无法访问源代码。我想将其转换为托管代码,以便 C# 可以调用该库。问题是 VC6 库大量使用 std::vector 和 std::string 作为函数参数,并且无法直接将这些类型封送到 C# 中。
所以我需要的是 VC6 和 C# 之间的桥梁,我为 VC++6 实现了 VC++2005 包装器。
然而,我从(jalf)对以下线程的回复中意识到“VC++6和VC++2005对std::string有不同的定义(并且编译器也可能插入不同的填充和其他更改),并且结果是垃圾和/或不可预测的行为和崩溃”。
将 std::string 从 VC++2005 传递到 VC++6 DLL 会产生垃圾 https://stackoverflow.com/questions/1804359/passing-stdstring-from-vc2005-to-vc6-dll-results-in-garbage
所以现在最大的问题是如何从 C# 调用 VC++6 本机库?
我很茫然,非常感谢任何帮助。
如果您可以访问 VC++6,请使用它编译“桥接/包装”dll。然后,公开可以从 C# P/Invoke 的类型的函数,例如 Char* 等。
顺便提一句,
您也许可以在 eBay 上以便宜的价格获得“二手”许可证。
EDIT:
这是您所要求的示例代码(我认为......):
[System.Runtime.InteropServices.DllImport("somedll.dll")]
public static extern int Fun1(int n, IntPtr[] lpNames);
//////////////////////////////////////////////////////////////////////////
List<string> strList = new List<string>();
IntPtr[] strPointerArr = new IntPtr[strList.Count];
for(int i=0; i<strList.Count; ++i){
strPointerArr[i] = System.Runtime.InteropServices.Marshal.StringToHGlobalUni(strList[i]);
}
Fun1(strList.Count, strPointerArr);
//////////////////////////////////////////////////////////////////////////
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)