FindWindow returns zero in MASM32 program even if the window exists

assembly, masm, masm32, winapi, x86

Solution

You should set `lpClassName` to NULL, not the address to an empty string.

invoke FindWindow, 0, offset NtpTitle

You are not testing the return value of `FindWindow`; mov does not modify flags.

test eax,eax
jz noNotepad

Problem

I'm trying to write a program in assembly and one of the first things that I need is the handle of the main window of a specific process. I've been trying to get it using FindWindow, but no luck so far; FindWindow apparently keeps returning zero. Can anyone point out what am I missing here? Thanks. ``` .486 .model flat, stdcall option casemap :none include \masm32\include\user32.inc include \masm32\include\kernel32.inc includelib \masm32\lib\user32.lib includelib \masm32\lib\kernel32.lib .data NtpTitle db "Untitled - Notepad",0 MsgNoNtp db "Notepad not found.",0 MsgNtp db "Found Notepad.",0 NullString db 0 hWndNtp dd 0 .code start: invoke FindWindow, offset NullString, offset NtpTitle mov hWndNtp, eax jz noNotepad invoke MessageBox, 0, offset MsgNtp, 0, 40h invoke ExitProcess, 0 noNotepad: invoke MessageBox, 0, offset MsgNoNtp, 0, 10h invoke ExitProcess, 1 end start ```

Original source