Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindWindow returns zero in MASM32 program even if the window exists

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
like image 740
Bruce Wayne Avatar asked Nov 20 '12 20:11

Bruce Wayne


1 Answers

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
like image 183
Jens Björnhager Avatar answered Oct 19 '22 01:10

Jens Björnhager