我认为问题出在我的C++函数中,但我试过了
C++中的C++函数:
bool __declspec( dllexport ) OpenA(std::string file)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
C#代码:
[DllImport("pk2.dll")]
public static extern bool OpenA(string path);
if (OpenA(@"E:\asdasd\"))
Run Code Online (Sandbox Code Playgroud)
我得到一个例外,内存已损坏,为什么?
如果我删除了std :: string参数,它的效果很好,但是使用std :: string它不起作用.
sho*_*osh 14
std :: string和c#string彼此不兼容.据我所知,就互操作而言,c#字符串对应于传递char*或wchar_t*在c ++中.
其中一个原因是std :: string可以有许多不同的实现,而c#不能假设您正在使用任何特定的实现.
尝试这样的事情:
bool __declspec( dllexport ) OpenA(const TCHAR* pFile)
{
std::string filename(pFile);
...
return true;
}
Run Code Online (Sandbox Code Playgroud)
您还应在DllImport属性中指定适当的字符集(unicode/ansi).
另外,与您的编组问题无关,通常会将std:string作为const引用传递:const std:string&filename.
| 归档时间: |
|
| 查看次数: |
17719 次 |
| 最近记录: |