CsWin32 how to create an instance of PWSTR for e.g. GetWindowText
See the question and my original answer on StackOverflowWith the latest version of CsWin32, currently 0.3.242, this is much more simple and natural than it used to be:
NativeMethods.txt:
GetWindowTextW
GetWindowTextLengthW
Program.cs:
...
var len = PInvoke.GetWindowTextLength(hwnd);
Span<char> chars = new char[len + 1];
PInvoke.GetWindowText(hwnd, chars);
var text = chars.ToString();
...