Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf characters when using user32.dll FindWindow in c# application

LS, I am using FindWindow method in c# application to get window handle from 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 chars like here:

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

but it fails when utf chars are present in window title:

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

e.g. hwnd == 0

Would You please provide me with any suggestion how to get handle of browser window containing utf-8 chars in c# application. Thanks in advance.

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

like image 504
user2413800 Avatar asked May 23 '13 13:05

user2413800


1 Answers

I haven't tried this myself, but you should be able to do this:

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

According to MSDN's article on the DllImportAttribute.CharSet Field, the default assumption will be CharSet.Ansi, and that would result in the behavior you're describing.

like image 85
jszigeti Avatar answered Sep 28 '22 02:09

jszigeti