Well, uh um.. I'll go away now and fall on my own sword!
Posted on 2003-07-16 17:33:00 by Poimander
Poimander, stay here so we can poke you with toothpicks. :grin:
Posted on 2003-07-16 17:46:31 by bitRAKE
One of the things about the Mutex method is you can place the Mutex in either the Global or Local namespace so you can force only one instance of the application on a Terminal Services machine (or Fast User switching in XP).

How does the shared section work with Terminal Services?
Posted on 2003-07-16 20:53:06 by Mecurius

How does the shared section work with Terminal Services?
Memory in Shared Code Sections Is Not Shared Across Terminal Server Sessions (Q251045)
Posted on 2003-07-16 22:04:28 by bitRAKE
Are there any provisos against employing the GlobalAddAtom and GlobalFindAtom apis (provided they are necessarily atomic operations) instead of the CreateMutex api?
Posted on 2003-07-18 21:59:14 by Poimander
i use a macro to keep from opening multiple instances of my programs, works quit nice
Posted on 2003-07-21 08:40:13 by sTonER_DeVIOUS
is it hutch's macro? :)



SingleInstanceOnly MACRO lpClassName
invoke FindWindow,lpClassName,NULL
cmp eax, 0
je @F
push eax
invoke ShowWindow,eax,SW_RESTORE
pop eax
invoke SetForegroundWindow,eax
mov eax, 0
ret
@@:
ENDM


Which is the findow solution.
Posted on 2003-07-21 12:32:34 by RobotBob
hmm yeah that looks familiar.. been a long time though
Posted on 2003-07-22 07:16:38 by sTonER_DeVIOUS
Remember that these macros works only of I have a Window.
But if my app is a console only application?
I think the best way is synchronization (Mutex, Semaphores, ...)
Posted on 2003-08-03 03:39:07 by greenant
Hi Greenant,

I stick by the Mutex, it has never failed me and never failed to be released after a program crash. It works and doesn't require special linking or a window.
Posted on 2003-08-03 03:58:35 by donkey
NAMED PIPE!
crate a named pipe in the first app a name unique like \pipe\97836490823-MYapp
a nother instanstance is opend, createfile to that pipe if it succeeds in obtaining a handle, the app is already open!
Posted on 2003-08-03 15:41:42 by Qages

NAMED PIPE!
crate a named pipe in the first app a name unique like \pipe836490823-MYapp
a nother instanstance is opend, createfile to that pipe if it succeeds in obtaining a handle, the app is already open!

Original, but I don't see what advantages it has over the mutex approach. MS uses the mutex approach and documents it as the method for limiting an application to a single instance:
Your WinMain function can also limit your application to a single instance. Create a named mutex using the CreateMutex function. If GetLastError returns ERROR_ALREADY_EXISTS, another instance of your application exists (it created the mutex) and you should exit WinMain.

http://www.msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/aboutwindows.asp
Posted on 2003-08-03 15:53:25 by donkey
M$ uses the mutex approach and documents it as the method for limiting an application to a single instance:

best reason why you shoudlnt use mutex. M$ probly knows who you are and where you live and what sites you like to visit, They are not to be trusted. but really there isnt much advantage in all these diffrent ways to 'Force one instance', only one is you have a choice! not what M$ tells you to do.
Posted on 2003-08-03 16:58:10 by Qages


best reason why you shoudlnt use mutex. M$ probly knows who you are and where you live and what sites you like to visit, They are not to be trusted. but really there isnt much advantage in all these diffrent ways to 'Force one instance', only one is you have a choice! not what M$ tells you to do.

Oh, sorry I didn't realize Mutexes were a politcial, socio-economic trojan horse designed to undermine my freedom of choice, I'll look more carefully next time I use CreateWindowEx as well :rolleyes:
Posted on 2003-08-03 17:11:05 by donkey

NAMED PIPE!
crate a named pipe in the first app a name unique like \pipe836490823-MYapp
a nother instanstance is opend, createfile to that pipe if it succeeds in obtaining a handle, the app is already open!


I have had problems with named pipes staying open after an application has crashed, so I would highly recommend NOT using named pipes for forcing only one instance. Also, I do not think it is documented on whether CreateNamedPipe can cause a race condition if it is called from two applications at the same time. CreateMutex does not have this problem.
Posted on 2003-08-03 17:30:32 by Mecurius
Ok, I like this.

http://www.flounder.com/nomultiples.htm

Has anyone done a port to MASM32 of his recommended Solution?

Regards, P1 :cool:
Posted on 2003-08-04 11:18:56 by Pone
I have always used CreateEventA with a named EVENT_OBEJCT and then I call GetLastError and check if error is ERROR_ALREADY_EXIST ( 000000B7h ).

By the way, I have recently seen this approach in comercial soft too ( it should be very similar to using ATOMs, MUTEXes, SEMAPHOREs... and this EVENT stuff is the same ).


MyEventObjectName db 'PROGRAM_NAME_vX.X',0 ; just an example...


...


push MyEventObjectName ; MASM/TASM users use push offset MyEventObjectName
push FALSE ; it could be TRUE or nonzero, we dont care
push FALSE ; it could be TRUE or nonzero, we dont care
push NULL ; dont need to care about SECURITY_ATTRIBUTES...
call ; MASM/TASM users use invoke/call CreateEventA

call

cmp eax,ERROR_ALREADY_EXISTS
jz WeAreAlreadyRunning

...

WeAreAlreadyRunning:
push 0
call

Bye :D
Posted on 2003-08-09 06:51:43 by sheroc