【发布时间】:2015-08-24 12:05:38
【问题描述】:
我有一个 cpp 项目(创建 test.DLL)和一个 C# win forms 项目(使用 test.DLL)。如何将 C++ 字典转换为 .net 字典?
我尝试了这段代码,但我得到了 System.Runtime.InteropServices.MarshalDirectiveException 类型的 unhandled exception
DLL cpp 项目
Dictionary<int, int>^ TestDictionaryElements()
{
Dictionary<int, int>^ h_result = gcnew Dictionary<int, int>();
h_result->Add(1, 2);
return (h_result);
}
extern "C"
{
Dictionary<int, int>^ TestDic()
{
Dictionary<int, int>^ dic_result = TestDictionaryElements();
return dic_result;
}
}
C# win 表单项目
class Program
{
[DllImport("test.dll")]
public static extern Dictionary<int, int> TestDic();
static void Main(string[] args)
{
Dictionary<int, int> resulDic = TestDic();
}
}
【问题讨论】:
-
@user1810087 这是 C++/CLI。
标签: c# c++ exception dictionary