I made my first little program. It uses FindWindow and PostMessage to close a window (calculator), however it doesnt quite work and I dont know why. Could someone just look at the source? I commented it partially, and it's really short.

I think either something in my IF-logic section is messing up (but i dont see why it would), or Windows 2000 handles things differently. I've only testing a similar program in visual basic on win98 years ago.



; section of code that must be having the error
.if winHwnd == 0
invoke PostMessage, ADDR Target1, WM_CLOSE, NULL, NULL
mov RetVal,eax
.if RetVal == 0
invoke MessageBox, NULL, ADDR ErrorMsg, NULL, MB_OK
.endif
.else
invoke MessageBox, NULL, ADDR ClosedMsg, NULL, MB_OK
.endif



I tried posting WM_CLOSE with HWND_BROADCAST, and that worked.. but using ADDR Target1 ("Calculator",0) woulnd't.

It is really confusing me and i've looked through it and compared it to the visual basic counterpart several time. Looks good ..
I found the visual basic stuff at http://www.vb-world.net/ but you shouldnt need it.

If anybody can help at all, great! I havent got a debug program and if anybody could show me a free one too that would be much appreciated. (cant even think of buying softice! brrrroke!)

thank-you
Posted on 2001-11-29 23:37:13 by matthew
Wellcome newbie :)

AFAIK PostMessage requires a handle to the destination window (HWND) and not a pointer to a null terminated string (i.e. ADDR Target1)

you should use FindWindow to get this handle and then use it ..

or in a more professional action you can use EnumWindows to enumerate all active windows and present the user with a listbox or listview from where he/she can choose what window to close :)
Posted on 2001-11-30 01:00:15 by BogdanOntanu
you cannot find calc without the classname, FindWindow
need the Class and the Windows-Caption (if NULL the TopMost
Window that matches with the ClassName is retrieved).
the class of CALC is db "SciCalc",0 ... just read iczelions
tutorials if you want to know what a classname is or how
you retrieve it. ok other thing is why you're doing this?

.if winHwnd == 0
invoke PostMessage, ADDR Target1, WM_CLOSE, NULL,

FindWindow returns the WindowHandle in the EAX (every
function returns EAX) so in this case if EAX is NULL FindWindow
failed (see ApiReference).

doit this way:

.DATA
class db "SciCalc",0
name db "Calculator",0

.CODE
invoke FindWindow,addr Class, addr Name
test eax,eax ;cmp EAX,0 / .IF EAX == 0
jz ERROR

invoke SendMessage,eax,WM_CLOSE,0,0

ERROR:
...

hmm, i use OllyDebug, get it from
home.t-online.de/homeOllyDbg or try search it with
google...

i started win32assembly with killing calc, too... :)
good luck, bye
Posted on 2001-11-30 18:06:28 by _mob
Can anyone provide information on the process to follow if someone wanted to create a similar program on an app that you only know the class name or window title? ....Thanks:)



Matthew, here is another way of writting your program if you are still confused:


start:
xor ebx, ebx

call FindWindowA, offset [cClass], offset [cName]
mov hCalc, eax

.IF (hCalc == ebx)
call MessageBoxA, ebx, offset[msgError], offset[msgTitle], ebx
.ELSE
call SendMessageA, hCalc, WM_SETTEXT, ebx, offset[text]

call MessageBoxA, ebx, offset[msgCaption], offset[msgTitle], 4

.IF (eax == 6)
call SendMessageA, hCalc, WM_CLOSE, ebx, ebx
.ENDIF
.ENDIF
call ExitProcess
end start
Posted on 2001-11-30 23:31:09 by -T-
-T-, "It takes a big man to cry, but it takes a bigger man to laugh at that man."

That is so funny, did you get it from the NVidia demo, or another place?
Posted on 2001-12-01 00:32:51 by bitRAKE
heh, Thats the jack handey character from SNL.

Uh oh, that was your 666th post..... I am scared!
Posted on 2001-12-01 09:59:30 by -T-
Thanks! I havent tested any code yet but i can see where i've failed, and I think I know what to fix. I'll know for sure when i'm not so tired :) .

I'm just writing it for no specific reason, just usefull to know maybe. Also, nostalgia. In tech10 the teacher didnt want people playing games, and half-life was a popular one so i made a program to close half-life in vbasic and later made a 'vaccine' which i gave to the grade 12's to gain instant popularity. w00t!

(sidenote!) I'm in grade 11 myself now ;)
Posted on 2001-12-02 05:06:38 by matthew
--------------------------------------------------------------
Matthew, here is another way of writting your program if you are still confused:
--------------------------------------------------------------

xor ebx, ebx

...

.IF (hCalc == ebx)

huh? now I am confused :)
Posted on 2001-12-02 16:32:46 by _mob
Why do you clear ebx at the beginning? What is it even for? Is it the lower end of eax or something?
Posted on 2001-12-03 23:15:46 by matthew
I am simply using ebx as null. xor'ing two equal numbers gets you zero.

So if the handle = null or zero then calculator can't be found or does not exist.
Posted on 2001-12-04 18:41:09 by -T-
Could somebody please help me again? I've done a bunch of debugging, after learning how to use the debugger ;) and now i've figured some stuff out and i feel i've done all i can.. So i find myself here.

I found that everything is working now up untill postmessage. PostMessage returns with 0 indicating failure. I soon found out that I could use GetLastError to find out what went wrong. It returned with 578 (1400 decimal) which means:

1400 Invalid window handle. ERROR_INVALID_WINDOW_HANDLE

How could it be invalid? FindWindow returned with 2c05a8 as
SciCalc & Calculator (class and title respectively), which after using icz-lesson 24's mouse hook tool i found out that that is the right handle.

Debugging is actually kind of fun.. soon starts to suck when you dont know how to fix your problem ..

Thanks.
Posted on 2001-12-05 23:38:01 by matthew
t but why are you xoring ebx???
you can use a simple cmp XXX,0
or even better test XXX,XXX...

...that doesn't make sense :)
Posted on 2001-12-06 20:42:51 by mob
simple mistake

change this line:
invoke PostMessage, addr hCalc, WM_CLOSE, NULL, NULL

to this:
invoke PostMessage, hCalc, WM_CLOSE, NULL, NULL
Posted on 2001-12-07 09:04:54 by smurf
cmp , dword = 10 bytes
xor ebx, ebx / cmp , ebx = 8 bytes.

Nothing really major, but oh well.
Posted on 2001-12-07 10:41:22 by f0dder
thats clear fodder... but don't say you would
do a xor ebx,ebx | cmp eax,ebx | jz XXX istead
of test eax,eax | jz XXX... but then who cares :)
Posted on 2001-12-07 10:46:54 by mob
okay fine.... Think of that line as:



##Whatever operation you want to set ebx to zero##


maybe a simple mov floats your boat, or a sub, or add, or cmp.... why not even a

.IF (GRAVITATIONAL_FIELD_OF_THE_SUN == 4935804385)
add ebx, 0Fh
sub ebx, ebx
.ELSE
xor ebx, ebx
mov ebx, 0
.ENDIF



:) :) :)
Posted on 2001-12-07 16:49:56 by -T-
what?

t i really don't know what you want...
i'm not sure, are you thinking i don't
know what xor is? sorry but i do...
or you just missed the point here but
i don't care anymore i have more important
things to do so i won't discuss about
your senseless attempt to check if a
return value is zero or not... but wait:

check out this:



mov ebx,0A5C414H
add ebx,5
sub ebx,2
lodsw
lea eax, [ebx + eax * 4 + 2]
shr ebx,16
sub eax,eax
xor ebx,eax

invoke BLABLA,...

pushad
push ebx
mov ebx,eax
mov RETURN,ebx
pop ebx
popad

.IF (RETURN == EBX) && ( EBX == RETURN )
...
.ENDIF


sorry that was childish, bye

:) :) :)
Posted on 2001-12-08 09:10:32 by mob
"t i really don't know what you want...
i'm not sure, are you thinking i don't
know what xor is? sorry but i do...
or you just missed the point here but
i don't care anymore i have more important
things to do so i won't discuss about
your senseless attempt to check if a
return value is zero or not... but wait:
"

I was not trying to insult you by answering your original question. You and mathew asked what xor in my program was for... I answered.... simple.

I come back and see you getting mad about me coding in a different way then you so I try to ease the tension with a light hearted joke.

Then suddenly a need to mock me?

Is there somthing wrong with me? Do you not like my haircut? I am lost.
Posted on 2001-12-09 01:14:20 by -T-
grow up...fast :rolleyes:
Posted on 2001-12-09 09:33:13 by Hiroshimator
Admins, i know you dont really have control over such features... but, it would be nice if users could freeze threads that they started..
Posted on 2001-12-10 00:04:52 by matthew