C# 中的 std::string ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/874551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:01:23  来源:igfitidea点击:

std::string in C#?

c#c++dll

提问by Ivan Prodanov

I thought the problem is inside my C++ function,but I tried this

我以为问题出在我的 C++ 函数中,但我试过了

C++ Function in C++ dll:

C++ dll 中的 C++ 函数:

bool __declspec( dllexport ) OpenA(std::string file)
{
return true;
}

C# code:

C#代码:

[DllImport("pk2.dll")]
public static extern bool OpenA(string path);

    if (OpenA(@"E:\asdasd\"))

I get an exception that the memory is corrupt,why?

我得到一个内存损坏的异常,为什么?

If I remove the std::string parameter,it works great,but with std::string it doesnt work.

如果我删除 std::string 参数,它工作得很好,但使用 std::string 它不起作用。

采纳答案by shoosh

std::string and c# string are not compatible with each other. As far as I know the c# string corresponds to passing char*or wchar_t*in c++ as far as interop is concerned.
One of the reasons for this is that There can be many different implementations to std::string and c# can't assume that you're using any particular one.

std::string 和 c# 字符串彼此不兼容。据我所知,就互操作而言,c# 字符串对应于传递char*wchar_t*在 c++ 中。
原因之一是 std::string 可以有许多不同的实现,而 c# 不能假设您正在使用任何特定的实现。

回答by Will Dean

Try something like this:

尝试这样的事情:

bool __declspec( dllexport ) OpenA(const TCHAR* pFile)
{ 
   std::string filename(pFile);
   ...
   return true;
}

You should also specify the appropriate character set (unicode/ansi) in your DllImport attribute.

您还应该在 DllImport 属性中指定适当的字符集(unicode/ansi)。

As an aside, unrelated to your marshalling problem, one would normally pass a std:string as a const reference: const std:string& filename.

顺便说一句,与您的编组问题无关,通常会将 std:string 作为常量引用传递:const std:string& 文件名。

回答by JaredPar

It's not possible to marshal a C++ std::string in the way you are attempting. What you really need to do here is write a wrapper function which uses a plain old const char*and converts to a std::string under the hood.

不可能以您尝试的方式编组 C++ std::string。你真正需要在这里做的是编写一个包装函数,它使用一个普通的旧函数const char*并在引擎盖下转换为 std::string 。

C++

C++

extern C { 
  void OpenWrapper(const WCHAR* pName) {
    std::string name = pName;
    OpenA(name);
  }
}

C#

C#

[DllImport("pk2.dll")]
public static extern void OpenWrapper( [In] string name);

回答by Nitay

I know this topic is a tad old but for future googlers, this should also work (without using char* in C++)

我知道这个话题有点旧,但对于未来的谷歌人来说,这也应该有效(在 C++ 中不使用 char*)

C#:

C#:

public static extern bool OpenA([In, MarshalAs(UnmanagedType.LPStr)] path);

C++:

C++:

bool __declspec( dllexport ) OpenA(std::string file);

回答by sailfish009

std::wstring and System.string can be compatible through below conversion:

std::wstring 和 System.string 可以通过以下转换兼容:

C++ :

C++:

bool func(std::wstring str, int number)
{
  BSTR tmp_str = SysAllocStringLen(str.c_str(), str.size());
  VARIANT_BOOL ret = VARIANT_FALSE;

  // call c# COM DLL
  ptr->my_com_function(tmp_str, number, &ret);

  SysFreeString(tmp_str);

  return (ret != VARIANT_FALSE) ? true : false;
}