Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I use SetWindowTextW in Win32 using PowerShell?

I’m trying to use PowerShell to change a Window title to display emojis.

I can change the Window title of a process (that has a window) using…

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public static class Win32 {
  [DllImport("User32.dll", EntryPoint="SetWindowText")]
  public static extern int SetWindowText(IntPtr hWnd, string strTitle);
}
"@

$MyNotepadProcess = start-process notepad -PassThru

[Win32]::SetWindowText($MyNotepadProcess.MainWindowHandle, 'My Title')

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But using SetWindowTextW just garbles the output…

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public static class Win32 {
  [DllImport("User32.dll", EntryPoint="SetWindowTextW")]
  public static extern int SetWindowTextW(IntPtr hWnd, string strTitle);
}
"@

$MyNotepadProcess = start-process notepad -PassThru

[Win32]::SetWindowTextW($MyNotepadProcess.MainWindowHandle, 'My Title')

enter image description here

One issue I think I have, is that SetWindowTextW only accept wide string, but I don’t know how to provide that as input.
And then I need to add the emojis using the UniCode numbers as in `u{1F600} (that’s a 🙂 too you).
(See Get Started with Win32 and C++ – Working with Strings)

>Solution :

The simplest way to ensure that the Unicode version of a WinAPI function is called properly is to:

  • Add CharSet=CharSet.Unicode to the [DllImport] attribute.

  • Omit the W suffix from the function name.

// Note the use of "CharSet=CharSet.Unicode" and 
// the function name *without suffix*
[DllImport("User32.dll", CharSet=CharSet.Unicode)]
public static extern int SetWindowText(IntPtr hWnd, string strTitle);

This ensures that SetWindowTextW, i.e. the Unicode implementation of the function is called, and that .NET marshals the .NET string essentially as-is as as a native LPCWSTR string, i.e. as a null-terminated array of Unicode code units.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading