I have a C++ DLL and some functions return Unicode null-terminated strings:
void SomeFunc(wchar_t* StrBuf)
The caller must allocate StrBuf – string can be up to 256 characters.
This DLL also exposes a COM object to use this DLL from C# via COM. Definition in IDL:
[id(1), helpstring("bla")]
HRESULT SomeFunc([in,out] BSTR* TextStr, [out,retval] LONG* RetValue);
Currently the C# code looks like this:
string val = new string('\0', 256); // Allocate memory
MyComObj.SomeFunc(ref val); // Get string
val = val.Substring(0, val.IndexOf('\0')); // Convert from null-terminated string
Is there a way to define such a COM function so it can be used from C# easier? Right now it looks ugly and takes three lines to call the function or five lines if a function has two string parameters.
Solved. In C++ I use this code in the COM wrapper:
IDL:
In C# it can now be called easily: