This isn't as difficult as you think it is... What is wchar_t*
? What value does that type typically represent? A string. It's the equivalent to the LPWSTR
type defined in windows.h
.
So, you marshal it as a string
type. However, since it's an out
parameter (or a return value), you'll need to use the StringBuilder
class on the C# end, rather than the string
type.
The P/Invoke syntax would look something like this:
[DllImport("MyLib.dll")]
public static extern void MyFunction(StringBuilder str);
And to use it, you first declare an instance of the StringBuiler
class with the appropriate capacity, and then call the function:
StringBuilder myString = new StringBuilder(255);
MyFunction(myString);
Remember that the unmanaged C++ code must free the string in order to prevent a memory leak. It's the only one with access to the unmanaged memory area where the string was allocated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…