【发布时间】:2013-12-21 00:17:07
【问题描述】:
我正在尝试在 C# 中导入我的 C++ Dll。它似乎适用于没有参数的函数,但我的函数有一些问题。
我的 C++ 函数:
__declspec(dllexport) bool SetValue(const std::string& strV, bool bUpload)
{
return ::MyClass::SetValue(strV.c_str(), bUpload);
}
它被包裹在"extern "C" {"中
该函数调用另一个函数:
bool SetValue(const char* szValue, bool bUpload)
{
}
我的 C# 函数:
[DllImport("MyDll.dll", EntryPoint = "SetValue", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetValue([MarshalAs(UnmanagedType.LPStr)]string strVal, bool bUpload);
当我使用调试模式并进入 SetValue(const char* sZvalue, bool bUpload) 函数时,sZvalue 为“0x4552494F”,但是当我尝试展开 Visual Studio 的视图以查看显示“未定义值”的值时”。
也许有人知道我的代码有什么问题?
谢谢!
【问题讨论】:
-
C# 不知道
std::string是什么。您需要导出采用原始字符指针的函数版本。