I have tried to do this in many ways, but none is working. Does anyone have a correct example for this? I just want to move the wchar_t* value from a function to the C# level.
I have tried to do this in many ways, but none is working. Does
Share
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 theLPWSTRtype defined inwindows.h.So, you marshal it as a
stringtype. However, since it’s anoutparameter (or a return value), you’ll need to use theStringBuilderclass on the C# end, rather than thestringtype.The P/Invoke syntax would look something like this:
And to use it, you first declare an instance of the
StringBuilerclass with the appropriate capacity, and then call the function: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.