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
Invoke ExitProcess,NULL
Posted on 2002-05-08 16:38:17 by Sliver
noo, i mean if i have my program already loaded and i load up a nother, i want to close the second instance
Posted on 2002-05-08 16:39:41 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
Which would be the 'second instance'? The first one executing, or the new instance that detects another instance is running? If you detect another instance, bring it to the foreground, and ExitProcess. For what reason do you need to Kill the other process and keep the second one?
Posted on 2002-05-08 16:49:22 by bitRAKE
Hi,

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

have nice days
Posted on 2002-05-08 16:56:23 by CYDONIA
i can only have 1 of my program loaded into ram at any 1 time
on my single programn exe files startup will chek for other instances so the second/ third loaded etc will see the first one loaded and close
Posted on 2002-05-08 16:56:57 by Qages
i tried this and didnt work


.data
mutexAttributes SECURITY_ATTRIBUTES <SIZEOF SECURITY_ATTRIBUTES,NULL,TRUE>
commandLinePtr LPSTR ?
.const
MUTEX_ALL_ACCESS equ 1F0001h
.code
invoke GetCommandLine
mov commandLinePtr,eax
invoke OpenMutex, MUTEX_ALL_ACCESS,FALSE, commandLinePtr
.if eax != NULL ;another instance is already running
invoke ExitProcess,0
.endif
invoke CreateMutex, offset mutexAttributes, TRUE, commandLinePtr
Posted on 2002-05-08 17:59:56 by Qages
just do a FindWindow searching for your AppName and bring the app to top and close it...


Don't use Mutex' or similar.. If the app crashes for an unknown reason you can't restart it as the Mutex didn't get killed.
Posted on 2002-05-08 18:07:46 by JimmyClif
Here's another good method:

MessageBox( NULL, "Hey if there's already another one of this program running could you please shut it down? Thanks!", "", MB_OK );


;) ;) :p
Posted on 2002-05-08 18:19:19 by iblis
there has to be a way, can i make part of my exe shared so that all other instances have access to that same memory so, i can check a value inthere if its tru then unload?
Posted on 2002-05-08 18:27:39 by Qages
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
im not using any windows, its just an app that runs some code in a loop only user interface is ctrl+f1 makes a messsagebox


so how do i create an invisable window and close the app instance that it belongs to?
Posted on 2002-05-08 19:20:33 by Qages
removed posted code. doesnt work like intended.

also im confused as to what you really want. bazik has posted a perfect solution to prevent any new instances that you may want to prevent.
Posted on 2002-05-09 00:59:28 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
i dont have any windows to search for so smallwaves ones wont work, all my app is it runs invsiably in the background chaning a setting in msn(allows 1200 max chars)
ill try the mutex hold on
Posted on 2002-05-09 15:47:28 by Qages
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