Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string in C#?

Tags:

c++

c#

dll

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

C++ Function in C++ dll:

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

C# code:

[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.

like image 204
Ivan Prodanov Avatar asked May 17 '09 12:05

Ivan Prodanov


People also ask

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type. That said, there are times where you would prefer std::string over string .

What mean std::string?

std::string is a typedef for a particular instantiation of the std::basic_string template class. Its definition is found in the <string> header: using string = std::basic_string<char>; Thus string provides basic_string functionality for strings having elements of type char.

Why do I need std::string?

Because the declaration of class string is in the namespace std. Thus you either need to always access it via std::string (then you don't need to have using) or do it as you did. Save this answer.

Are C strings faster than std::string?

C-strings are usually faster, because they do not call malloc/new. But there are cases where std::string is faster. Function strlen() is O(N), but std::string::size() is O(1). Also when you search for substring, in C strings you need to check for '\0' on every cycle, in std::string - you don't.


3 Answers

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.

like image 171
shoosh Avatar answered Oct 12 '22 21:10

shoosh


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.

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

like image 23
Will Dean Avatar answered Oct 12 '22 21:10

Will Dean


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++

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

C#

[DllImport("pk2.dll")]
public static extern void OpenWrapper( [In] string name);
like image 20
JaredPar Avatar answered Oct 12 '22 22:10

JaredPar