how do i close my app if its already running?
i dont know right words to search for if its already on this fourm.
Posted on 2002-05-08 16:36:45 by Qages
Search the board for "single instance" or use this code:


[...]
invoke CreateMutex, 0, 1, addr szMutexAppName
push eax
invoke GetLastError

.if eax == ERROR_ALREADY_EXISTS
pop eax
invoke CloseHandle, eax
jmp @F
.endif

invoke DialogBoxParam, hInstance, 1000, 0, addr DlgProc, 0
@@:
invoke ExitProcess, 0
[...]
Posted on 2002-05-08 16:48:16 by bazik
Hi,

I have a simple example. Like baZiK's example. :alright:

have nice days
Posted on 2002-05-08 16:56:23 by CYDONIA
hi Qages,

JimmyClif nailed it on the spot. The createmutex method works best if you want to close any new instances of your application that starts up, not the old one that is already running.

To close instances of your application that are already running you need to invoke FindWindow. Since you already know what the window class and name is this works well. then you just send a message to it to close it. here is an example. this should be the first code that is processed in your .code section.
invoke FindWindow,addr ClassName,addr Caption ;find any instances

.if eax != 0 ;if one is found, use the returned handle
invoke SendMessage,eax,WM_CLOSE,0,0 ;from FindWindow that is stored in eax
.endif ;to close the window
;else if nothing is found so what
Posted on 2002-05-08 18:43:32 by smurf
Qages,
is it that hard to copy and paste my code? :)


P.S.: smurf, you code won't work if I rename the exe ;)


; #################################################

.486
.model flat, stdcall
option casemap :none

; #################################################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

; #################################################

StartProgram PROTO

; #################################################

.data
szMutexAppName db "MY_APP_MUTEX", 0
szAlreadyRunning db "Another Instance of this Application is already running!", 0
szRunsNow db "This App runs now!", 13,10, "Leave this MessageBox open ",
"and doubleclick the exe again!", 0


; #################################################

; .data?


; #################################################

.code

start:

invoke CreateMutex, 0, 1, addr szMutexAppName
push eax
invoke GetLastError

.if eax == ERROR_ALREADY_EXISTS
pop eax
invoke CloseHandle, eax
invoke MessageBox, 0, addr szAlreadyRunning, 0, 0
jmp @F
.endif

call StartProgram
@@:
invoke ExitProcess, 0

StartProgram proc

invoke MessageBox, 0, addr szRunsNow, 0, 0
ret

StartProgram endp

end start

; #################################################



P.P.S.: Thanks Hiro for moving the post.
And we are back again at the "You wrote my code and I don't say 'Thank you'" topic....
Posted on 2002-05-09 03:16:38 by bazik
Three methods to implement it!;)

Just have a look at it!
Posted on 2002-05-09 06:38:19 by smallwaves
code originally posted by bazik
    invoke CreateMutex, 0, 1, addr szMutexAppName

push eax
invoke GetLastError

.if eax == ERROR_ALREADY_EXISTS
pop eax
invoke CloseHandle, eax
jmp @F
.endif

invoke DialogBoxParam, hInstance, 1000, 0, addr DlgProc, 0
@@:
invoke ExitProcess, 0

bazik: when you close the handle of your mutex like you have the very first instance of the program will never close its mutex once you decide to exit the program. it should still be floating around in the system somewhere. what do you think?
Posted on 2002-05-09 10:01:59 by smurf
ive done it!!!!!!!! Posted on 2002-05-09 16:57:34 by Qages
Wow...
I've read some confusing code, but that takes the cake... You mine as well have just taken a random sampling from some random program while using Oily... and then posted it arse-backwords

:) :) :)

Maybe it's just me...

Sliver

ps. In otherwords, I have no clue how the code you posted *actually* solves anything...

pps. But then again... Maybe it's just me...
Posted on 2002-05-09 17:06:44 by Sliver
well you see i have a shared segment and my first app puts 1 in AppInit and any other apps check to see ifits 1 then if its true we exit....
Posted on 2002-05-09 17:09:33 by Qages