How can I check if my application is arleady running?
I want only one instance in memory
I want only one instance in memory
Search :) and you will find...
keywords : only AND one AND instance
;)
keywords : only AND one AND instance
;)
Use a Mutex, it can only be created once until released. If you create a mutex when your app starts any subsequent versions of your app that attempt to create the same mutex will fail to create a new one.
invoke CreateMutex,NULL,TRUE,ADDR MutexString
invoke GetLastError
.IF eax != 0
invoke ExitProcess,0
.endif
invoke CreateMutex,NULL,TRUE,ADDR MutexString
invoke GetLastError
.IF eax != 0
invoke ExitProcess,0
.endif
thanks
dont use mutex, it uses system memory. try a shared section approtch.
first you want to add to your build.bat under the linker.
then in notepad(or whatever you use) put at the top
then somewhere in code
first you want to add to your build.bat under the linker.
/section:share,rws
then in notepad(or whatever you use) put at the top
share segment
AppInit dd 0
share ends
then somewhere in code
cmp AppInit,1
je @F
mov AppInit,1
jmp endFFFF
@@:
invoke ExitProcess,0;exit!
endFFFF:
;code here
Mutex uses system memory but only a very SMALL PIECE of system memory
yes but if your program abnormally termanates without relasing the mutex, its stuck there and will prevent the program from being opend again.
It is not true
The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
and there is only one handle of this mutex
The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
and there is only one handle of this mutex
not from my experance. if i didnt close it by my self i couldnt use it any more.
Sometimes it forgets to close the objects opened by the process when it crashes. But then it won't unload the module either. If you're concerned about memory, at least don't put a variable in it's own tiny little section, since it's going to use a whole page.
This is instant crash code that I tested, the mutex was destroyed on each crash:
invoke CreateMutex,NULL,TRUE,OFFSET MutexString
invoke GetLastError
.IF eax != 0
invoke ExitProcess,0
.endif
mov eax,0
mov [eax],DWORD PTR 1
The other way added 512 bytes to the executable for one DWORD, that's quite a bit of overhead.whats 512 bytes to under 10 clocks to check it. the call to the api and system overhead is 1000x of the share seg way.
It's not run in a loop and at somewhere around 8700 clocks (in my tests using CreateMutex and GetLastError) it will not even be noticable during an application start. The saving is not significant as it cannot be run more than once.
Or you can use the brainless method I have used in the past.
I set a registry key in my app's section called "Running", then I toggle it from 1 to 0 based
on whether the app is running.
I needed another app to be aware of my running app, but was crunched for time.
I haven't bothered with a better solution with this app since it's hasn't failed in this regard (yet :))
EDIT: ( I also have a key "ExitSuccesful" making sure it hasn't crashed...)
RobotBob
I set a registry key in my app's section called "Running", then I toggle it from 1 to 0 based
on whether the app is running.
I needed another app to be aware of my running app, but was crunched for time.
I haven't bothered with a better solution with this app since it's hasn't failed in this regard (yet :))
EDIT: ( I also have a key "ExitSuccesful" making sure it hasn't crashed...)
RobotBob
Qages, what kind of special hard drive do you have, which can load and transfer a sector in less than 10 clocks?
im not talking about loading a sector, im talking about the time it takes to acccess that varable and compare from memory!
btw i have a 33 meg a second( ramdom read)(100meg bufferd) an extra 512 bytes wont matter unless its fragmented. you could have a 200k exe and it would load in approxamtly the same time a 2k exe would.
btw i have a 33 meg a second( ramdom read)(100meg bufferd) an extra 512 bytes wont matter unless its fragmented. you could have a 200k exe and it would load in approxamtly the same time a 2k exe would.
Good read lingo12, I'd use a version of Qages' code and leave it up to the OS then. :grin: Try this on a multi-processor machine using another program to force two versions of the program:
Posted on 2003-07-15 18:28:40 by bitRAKE
mov eax, 1
xchg AppInit, eax
test eax, eax
je @F
invoke ExitProcess, 0 ; exit!
@@: ; your program here
If this doesn't work with the shared segment then the OS is broken!
Posted on 2003-07-15 18:28:40 by bitRAKE
greenant,
Use the FindWindow method, it seems more reliable than the CreateMutex and CreateSemaphore techniques:
SingleInstance PROC pszAppName:DWORD
LOCAL hWin:DWORD
; Limit the application to a single instance
invoke FindWindow, 0, pszAppName
.IF eax
mov hWin, eax
invoke EnableWindow,hWin,TRUE
invoke ShowWindow,hWin,SW_RESTORE
invoke SetForegroundWindow,hWin
mov eax, FALSE
.ELSE
mov eax, TRUE
.ENDIF
ret
SingleInstance ENDP
Use the FindWindow method, it seems more reliable than the CreateMutex and CreateSemaphore techniques:
SingleInstance PROC pszAppName:DWORD
LOCAL hWin:DWORD
; Limit the application to a single instance
invoke FindWindow, 0, pszAppName
.IF eax
mov hWin, eax
invoke EnableWindow,hWin,TRUE
invoke ShowWindow,hWin,SW_RESTORE
invoke SetForegroundWindow,hWin
mov eax, FALSE
.ELSE
mov eax, TRUE
.ENDIF
ret
SingleInstance ENDP
poimander, did you read the article lingo posted? It's quite a good read. Hint: race conditions.