Utf when using user32.dll FindWindow in c # application

LS, I am using the FindWindow method in a C # application to get a window handle from a web browser

[DllImport("user32.dll")] public static extern int FindWindow(string lpClassName, string lpWindowName ); 

it works well when the window title does not contain utf characters, as here:

 string caption1 = "pinvoke.net: findwindow (user32) - Google Chrome"; int hwnd = FindWindow(null, caption1); 

but it fails if utf characters are present in the window title:

 string caption2 = "Słownik języka polskiego - Google Chrome"; int hwnd2 = FindWindow(null, caption2); 

eg. hwnd == 0

Could you provide me any suggestion on how to handle a browser window containing utf-8 characters in a C # application. Thanks in advance.

ps I already saw a comment about using FindWindow with utf in C ++ saying: "You can explicitly use the Unicode version of the HWND API windowHnd = FindWindowW (NULL, L" Minesweeper "); but I still don't know how to do this in c #

+6
source share
1 answer

I have not tried this myself, but you must do this:

 [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int FindWindow(string lpClassName, string lpWindowName ); 

According to the MSDN article in the DllImportAttribute.CharSet field, the default assumption will be CharSet.Ansi , and this will lead to a description of the behavior.

+9
source

Source: https://habr.com/ru/post/945687/


All Articles