Hi everyone,
I am writing a library using C++ and want to make it portable and accessable in other languages (like .NET languages). This is why I decided to wrap the library inside an COM DLL, using ATL.
Now my problem is that I am unable to pass ANSI strings from the library through the RCW back to the client. My code looks something like this:
[propget, helpstring("String")] HRESULT String([out, retval] LPSTR* plpszValue);
STDMETHODIMP CMyClass::get_String(LPSTR* plpszValue)
{
std::string sStdStr("Test");
*plpszValue = _strdup(sStdStr.c_str());
return S_OK;
}
Now accessing the code looks like this:
MyClass oClass = new MyClass();
Console.WriteLine(oClass.String);
However passing strings into the other direction (client -> server, means propput) works fine. Also returning an empty or predefined string does not throw an exception:
STDMETHODIMP CMyClass::get_String(LPSTR* plpszValue)
{
std::string sStdStr("Test"); *plpszValue = "Test";
return S_OK;
}
This is why I think the problem is related into
_strdup()
. So can anyone tell me what I did wrong? What's wrong with my idea of allocating memory for a string and returning it's pointer to the client?
I hope you can help, thank you!
P.S.: My project is built with Multi-Byte-Characterset (ANSI), not Unicode.