I have a dll written in C#, and exposed to COM. I am using the dll in builder... I can instantiate the class, but am having problems with marshalling the return value from the C# method calls.
C#
public string GetValue([MarshalAs(UnmanagedType.LPWStr)] string key)
{
return "value";
}
The translated function as it's imported into builder:
virtual HRESULT STDMETHODCALLTYPE GetValue(LPWSTR key/*[in]*/,
BSTR* pRetVal/*[out,retval]*/) = 0;
I know very little about C++. The 'key' parameter gets passed in fine because I'm able to use the 'MarshalAs' attribute on the parameter, but I either don't know how to declare it for the return value, or don't know how to call the function on the C++ side (I've tried several things, just guessing).
UPDATE: Okay, I was just able to solve the issue by taking Anton's example and trying modifications based on Hans' comments. Antons answer works precisely as he shows, but because of the concerns expressed about the memory management issue, I ended up not applying the return attribute in C#, and the C++ code calls the function as follows:
BSTR result;
obj->GetValue(key, &result);
SysFreeString(key);
SysFreeString(result);
I wish I could give credit to both answers for helping me with this, they were both necessary to supplying me with the information I needed.