8 years after this question with Windows 11 22H2 FR, in Powershell PSVersion 5.1.22621.169 the command:
FindWindow("ConsoleWindowClass", "Administrateur : X:\windows\system32\cmd.exe")
outputs 0.
After some rechearch, I use SPY++ for reading caption and class.
I have copy/pasted from the editbox of SpY++ into a PS string variable ($title).
And I notice in this copy/pasted $title string:
[byte]$title[14]= 160
[byte]$title[14] looks like a space and is here, in the middle "r :"
String index 14 is for french version of OS.
FindWindow returns a good handle with this character!
Question : Why [byte]$title[14] = 160 (at index 14 in the string for FR) is found in the title of an elevated cmd console? Is it normal? Where is it documented?
The code I used:
$code = @'
[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError = true)]
public static extern IntPtr FindWindow(string ClassName,string WindowName);
[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError = true)]
public static extern bool ShowWindow( IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SetWindowText(IntPtr HWND, string Text);
'@
Add-Type -MemberDefinition $code -Name Window -Namespace Test
# please, open an elevated console cmd
#Be carrefull, "administrateur" in french OS. So, adapt if you test...
# here, "[byte]$title[14] = 32" -> it's a space, it's not 160
$name = "Administrateur : X:\windows\system32\cmd.exe"
$classW = "ConsoleWindowClass"
[Test.Window]::FindWindow($classW, $name)
#it gives 0
I don't know how to put a code point of 160 into a system.string (I can only copy/paste the title from SPY++). So I can't give the correct code for the second test.
In summary:
I launch a cmd console elevated and see the title:
"Administrateur : X:\windows\system32\cmd.exe"
...............|..............................
The above line is for locating the position of the "strange" character with code point of 160.
With PS, i try to use FindWindow to get a handle on this window
If I copy/paste the title from SPY++ ([byte]$title[14] = 160), FindWindow is OK.
If I enter the text from the keyboard ([byte]$title[14] = 32), FindWindow returns 0 (error).
Is it normal to get a title text with code point 160 (at index 14 in the string for FR)?