Hi Everyone ! :grin:
I have a question how to close another program.
I'm using ShellExecute to run NotePad

invoke ShellExecute,NULL,SADD("open"),SADD("notepad"),NULL,NULL,SW_SHOWDEFAULT
mov NotePadhWnd,eax ;Get the hWnd of Notepad

Then i want to close it when i want. I tried SendMessage to Handle of that to close NotePad, but still not work. (May be this way not work)

invoke SendMessage,NotePadhWnd,WM_QUIT,0,0

Can someone give me another way better to do this.
Thank.
Posted on 2004-04-24 01:16:08 by neverending
Afternoon, neverending.

Tried WM_CLOSE yet?

i.e.


invoke PostMessage, NotePadhWnd, WM_CLOSE, NULL, NULL
[\code]
Note that PostMessage is used - not SendMessage.
PostMessage will place the message into that processes Message Queue.
SendMessage waits until the call returns before allowing continuation.

Cheers,
Scronty
Posted on 2004-04-24 01:22:39 by Scronty
First off where did you read that ShellExecute returns a window handle ? You can only send messages to a window handle or a thread. Neither of which is returned by ShellExecute.

You can use ShellExecuteEx and get the process ID and use TerminateProcess or as Scronty said use FindWindow to get a window handle and send WM_CLOSE. If you have the PID you can get a thread handle and send the message this long obfuscated, mostly useless way (but it does have a good purpose in the app I wrote it for)...

SendThreadMessage FRAME uMsg,wParam,lParam

LOCAL MsgParams :MSGPARAMS

mov D[MsgParams.Result],0
cmp D[idThread],0
je >.EXIT

mov eax,[uMsg]
mov [MsgParams.ID],eax
mov eax,[wParam]
mov D[MsgParams.lParam],eax
mov eax,[lParam]
mov D[MsgParams.wParam],eax
invoke EnumThreadWindows,[idThread],OFFSET EnumThreadWndProc,OFFSET MsgParams
mov eax,[MsgParams.Result]
.EXIT
RET
ENDF

FindProcessThread FRAME PID
LOCAL hThreadSnap :D
LOCAL te32 :THREADENTRY32

/*
Returns a thread ID from a given process
used to send the shutdown messages to the process

The thread ID is necessary in order to send the messages
to the application
*/

invoke CreateToolhelp32Snapshot,TH32CS_SNAPTHREAD,0
mov [hThreadSnap],eax

mov D[te32.dwSize],SIZEOF THREADENTRY32
invoke Thread32First,[hThreadSnap],OFFSET te32
or eax,eax
jz >.EXIT
W1:
mov eax,[te32.th32OwnerProcessID]
cmp eax,[PID]
je >W2
invoke Thread32Next,[hThreadSnap],OFFSET te32
or eax,eax
jnz <W1
jmp >.EXIT
W2:
mov eax,[te32.th32ThreadID]
.EXIT
push eax
invoke CloseHandle,[hThreadSnap]
pop eax
RET
ENDF

EnumThreadWndProc FRAME hwnd,lParam
uses edi

/*
This procedure will send a message to the parent application
it requires that lParam contain a pointer to a MSGPARAMS structure
the enumeration will stop when a non-zero result has been recieved

MSGPARAMS STRUCT
MsgID DD
MsgwParam DD
MsglParam DD
Result DD
ENDS

The procedure will wait a maximum of 2 seconds for a reply from each window
responding to this message from a dialog requires that the parent
use the following syntax to properly return a value:

invoke SetWindowLong,DWL_MSGRESULT,[dwResult]
xor eax,eax
inc
ret
*/

mov edi,[lParam]
mov edx,edi

add edx,MSGPARAMS.Result
mov D[edx],FALSE

invoke SendMessageTimeout,[hwnd],[edi+MSGPARAMS.ID],[edi+MSGPARAMS.wParam],\
[edi+MSGPARAMS.lParam],SMTO_ABORTIFHUNG,2000,edx

; Stop the enumeration when we get a non-zero response
mov eax,[edi+MSGPARAMS.Result]
or eax,eax
jz >
xor eax,eax
ret
:
xor eax,eax
inc eax
ret
ENDF
Posted on 2004-04-24 01:47:08 by donkey
neverending,

Check out:

http://www.asmcommunity.net/board/search.php?s=&action=showresults&searchid=255229&sortby=lastpost&sortorder=descending

It opens WordPad, inserts some text, formats the page. and closes WordPad

hth,

farrier
Posted on 2004-04-24 05:52:37 by farrier
Thank :alright:
Posted on 2004-04-25 01:01:22 by neverending