【发布时间】:2014-12-18 20:17:09
【问题描述】:
我正在尝试构建一个包装器以在 c# 代码中使用 cpp 代码,并且我想返回自定义结构(类)作为方法 2 的输出,以及方法 1 的 std::string,这是我在 cpp 中的代码
extern "C" __declspec(dllexport) std::string method1()
{
std::string s;
//Some Code/////////
return s;
}
这是应该返回自定义结构(或类)的方法
extern "C" __declspec(dllexport) MyStruct method2()
{
MyStruct s;
//Some Code//////
return s;
}
我尝试为这两种方法编写 c# 代码,这是我的 c# 代码
[DllImport("<dllPath>", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string method1(); //Exception
[DllImport("<DllPath>")]
public static extern MyStruct method2(); //Compile Error
现在,当我尝试运行 c# 代码时,我得到了方法 1 的 MarshalDirectiveException 和方法 2 的编译错误?
【问题讨论】: