FindWindow

i just had a few questions on how FindWindow function works. what does the 0 mean in FindWindow? what if i put a 1 instead? should it go on the left or right side? both of them seem to work though. i was wondering if there was a "proper" way to use the function? also, is there a difference with FindWindowA, FindWindowEx, FindWindowExA, FindWindowExW, and FindWindowW? i tried googling, but it didn't help.
 
FindWindow(0, L"notepad")

 
FindWindow(L"notepad", 0)
Last edited on
What don't you understand? Remember that NULL is the same as 0 (basically).
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa
oh, i see. how about my other questions?
what if i put a 1 instead?


Why would this occur to you?

The documentation states that the function expects a "string", which in the parlance of the C oriented API of Windows means LPCSTR, a type indicating a pointer to a "C" string, or null terminated string. Providing a "0" or "NULL" indicates the parameter should be ignored. Naturally, a C programmer would think that since the parameter is a pointer to a "C" string, any numeric information would resolve to an invalid pointer, likely causing a crash or some undefined behavior. If you see behavior that doesn't crash, it is undefined by the API and shouldn't be relied upon to do anything in particular but crash.

I was pointed directly to Microsoft's documentation on the function when searched.

i was wondering if there was a "proper" way to use the function?


Well, use should be according to the documentation. It finds a window either by class or by title, returning the handle to that window. If what you're really asking is what to do next, that's half a book already written by Petzold, and it's rather thick.

should it go on the left or right side?


In the context of the post, I had to think what you are really asking, and I think you're asking which parameter takes the title. The first parameter is a classname, which is probably not what you're looking for. An example of a window class could be something like a button or an edit control, maybe a frame window, but when custom classes are registered the name may be unrecognizable. The registerclass function returns a kind of handle that can be used in certain circumstances, but again - it's the kind of there were "if you have to ask...."

However, the second parameter is the "name" or "title" of the window. It's likely what is displayed in the window title, and what you're looking for.

That said, it may not find what you expected because "Notepad" (and likely many other such titles" include the file name opened ("untitled" in some cases). As such, looking for "Notepad" might not find any window.

is there a difference with FindWindowA, FindWindowEx, FindWindowExA, FindWindowExW, and FindWindowW?


The "A" subscript hints that the function accepts simple "Ascii" strings, while the "W" versions take "wide character" strings (Unicode most likely).

The "Ex" version takes more parameters, and can start the search from a particular window, searching through its children. This is a way of searching the entire hierarchy (by drilling down, level by level, into "children of children" windows).

That all said, when I need to find windows with complicated sub-string searching (like ANY notepad, no matter what file is open), I prefer EnumWindows (and, possibly, it's related EnumChildWindows), especially if I need a more complete review of the window hierarchy.



When using MS functions like FindWindow(), google “MSDN FindWindow” and hit “I’m feeling lucky!”. This will take you to the official Microsoft documentation and tell you all there is to know about the function.

MS documentation isn’t the easiest to read for a beginner (especially with their most recent CSS), but it is far easier than a lot of other nonsense on the internet, and is usually very (very, very, excruciatingly) complete.

Googling “FindWindow example C/C++” is also a very good choice of search terms.

Hope this helps.
The difference(s) between, for example, FindWindow(), FindWindowA() and FindWindowW() is how the Win32 API deals with character strings.

Specifically the difference between 8-bit ANSI character strings and 16-bit Unicode character strings.

Long story short....very simplified.....

ANSI character strings were the strings that Win9x/Me expected to be used as Win32 function parameters. NT and later (Win XP/7/8/10) use Unicode strings.

Data type char and wchar_t.

FindWindow() is a pre-processor placeholder, the compiler selects either FindWindowA() or FindWindowW() based on whether Unicode is defined or not.
Topic archived. No new replies allowed.