How can I check if my application is arleady running?
I want only one instance in memory
Posted on 2003-07-15 10:12:30 by greenant
Search :) and you will find...

keywords : only AND one AND instance

Posted on 2003-07-15 10:19:36 by JimmyClif
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
Posted on 2003-07-15 10:50:50 by donkey
Posted on 2003-07-15 10:55:54 by greenant
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

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!
;code here
Posted on 2003-07-15 11:52:40 by Qages
Mutex uses system memory but only a very SMALL PIECE of system memory
Posted on 2003-07-15 13:19:07 by greenant
yes but if your program abnormally termanates without relasing the mutex, its stuck there and will prevent the program from being opend again.
Posted on 2003-07-15 14:55:46 by Qages
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
Posted on 2003-07-15 15:04:06 by greenant
not from my experance. if i didnt close it by my self i couldnt use it any more.
Posted on 2003-07-15 15:19:34 by Qages
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.
Posted on 2003-07-15 15:20:32 by Sephiroth3
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

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.
Posted on 2003-07-15 15:27:16 by donkey
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.
Posted on 2003-07-15 15:31:06 by Qages
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.
Posted on 2003-07-15 15:56:22 by donkey
Posted on 2003-07-15 15:58:56 by lingo12
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...)

Posted on 2003-07-15 16:37:56 by RobotBob
Qages, what kind of special hard drive do you have, which can load and transfer a sector in less than 10 clocks?
Posted on 2003-07-15 18:02:01 by Sephiroth3
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.
Posted on 2003-07-15 18:25:57 by Qages
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:

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
Use the FindWindow method, it seems more reliable than the CreateMutex and CreateSemaphore techniques:

SingleInstance PROC pszAppName: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
mov eax, TRUE
SingleInstance ENDP
Posted on 2003-07-15 22:28:55 by Poimander
poimander, did you read the article lingo posted? It's quite a good read. Hint: race conditions.
Posted on 2003-07-16 02:40:16 by f0dder