2
votes

Question: I need to call a C# dll from a C++ executable. I use COM, and it works fine for int, long and bool. But I can't get a string along...

The IDL file says it's a BSTR, but I can't pass it correctly, and neither retrieve one. I tried using wchar_t* and using sysalloc as I did with VB6, but that doesn't seem to work.

Anybody knows how, or what might be wrong ?

1
Using SysAllocString to go from a wchar_t* to a BSTR is certainly the correct route. Can you give more details on what goes wrong when you do this? Compilation errror, runtime exception, etc ...JaredPar
I get nothing in C# and I only get questionmarks back.Stefan Steiger

1 Answers

2
votes

If you're using ATL you can do this:

std::string theString = "hello";
CComBSTR bstr(theString.c_str());
DoSomething(bstr);  // Function that takes a BSTR as an argument

Or if no ATL:

const wchar_t* theString = L"hello";
BSTR bstr = SysAllocString(theString);
DoSomething(bstr);
SysFreeString(bstr);