Simon Mourier's Avatar
Simon Mourier's blog (1362 answers on StackOverflow) about Microsoft technologies C#, C/C++, .NET, WinRT, WinUI3, AOT, P/Invoke, COM, Interop, DirectX, Direct2D, Windows, ...
  • 🔎︎ Search
  • Categories
  • Archives
  • About Me
  • c#
  • interop
  • winapi

CsWin32 how to create an instance of PWSTR for e.g. GetWindowText

Nov 4, 2025 See the question and my original answer on StackOverflow

With 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();
...
  • SmoBlog © 2010-2025 Simon Mourier. All rights reserved.
  • Privacy & Terms