c# - 如何从 C# 调用 native VC++6 库(以 std::string 作为参数)

标签 c#

我有一个用 VC++6 编写的 native 动态链接库,但我无法访问源代码。我想将它转换为托管代码,以便 C# 可以调用该库。问题在于 VC6 库大量使用 std::vector 和 std::string 作为函数参数,并且无法将这些类型直接编码到 C# 中。

所以我需要的是 VC6 和 C# 之间的桥梁,我为 VC++6 实现了 VC++2005 包装器。

但是,我从 jalf 对以下线程的回复中意识到“VC++6 和 VC++2005 对 std::string 的定义不同(并且编译器也可能插入不同的填充和其他更改) ,结果是垃圾,和/或不可预测的行为和崩溃。

Passing std::string from VC++2005 to VC++6 DLL results in garbage

那么现在,最大的问题是,如何从 C# 调用 VC++6 原生库?

我很茫然,非常感谢任何帮助。

最佳答案

如果您可以访问 VC++6,请使用它编译“桥接/包装器”dll。然后,公开具有您可以从 C# 中 P/调用的类型的函数,例如 Char* 等。

顺便说一句, 您或许可以在 eBay 上以便宜的价格获得“二手”许可证。

编辑:
这是您所问内容的示例代码(我认为...):

[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);

//////////////////////////////////////////////////////////////////////////

关于c# - 如何从 C# 调用 native VC++6 库(以 std::string 作为参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1806402/

相关文章:

c# - Internet Explorer 缓存从数据库返回的数据

c# - BufferManager 在不调用 ReturnBuffer 的情况下调用 TakeBuffer

c# - ADO.NET CommandBuilder、InsertCommand 和默认约束

c# - 无法将类型 'System.DateTime?' 隐式转换为 'System.DateTime' 。存在显式转换(您是否缺少转换?)

c# - FoxIt PDF SDK - PrintWithDialog 中的 AccessViolationException

c# - 通过 WCF 服务返回 MySqlDataAdapter 列表导致错误

c# - Windows 8 中应用程序的源代码

c# - 如何在 Visual Studio 2010 中使用 C# 创建简单的 SOAP 服务器?

c# - 用Deflate算法解压Span<byte>中的数据

c# - 谷歌地图上的移动位置跟踪