10K35H 5H4KY4
10K35H 5H4KY4

Reputation: 1526

Passing C# 'string' in C++ dll as 'Char*'

Hi I have create c++ function as

void MyClass::GetPRM(BSTR BString)
{
//----
}

In C# the dll interface looks like:

GetPRM(char* BString)

My question is how can I pass string as char* from c# to c++ dll? I have tried doing void MyClass::GetPRM(std::string BString) but no luck. Any Suggestions

Upvotes: 1

Views: 651

Answers (1)

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36494

You should be able to use

 [DllImport("mycppdll", EntryPoint="MyClass_GetPRM")]
 extern static void GetPRM([MarshalAs(UnmanagedType.BStr)] string BString)

However, that doesn't take into account C++ name-mangling, nor your C++ method's this pointer if that method is not declared static.

On the C side, you may need a wrapper function like this:

 extern "C" __declspec(dllexport) void __stdcall
 MyClass_GetPRM(BSTR BString)
 {
     MyClass::GetPRM(BString);
 }

which would require adaptation of the C# declaration to match the exported name:

 [DllImport("mycppdll", EntryPoint="MyClass_GetPRM")]
 extern static void GetPRM([MarshalAs(UnmanagedType.BStr)] string BString)

Upvotes: 2

Related Questions