how do you do this guys?? need help!
1. Mutexe
2. Memory mapped files (shared of course)
3. Shared sections
4. Window handle handling
5. Other...
A cheap (and nasty) method often used is to use "FindWindow" to find a window of your classname - before your program creates its window. If one is found, then your application must already be running, otherwise you can continue normally.
But this does only work if your program actually has window (which is normal.)
The alternative is to use a mutex..
.data
uno db "mylittlemutex",0
.data?
hMutex DWORD ?
.code
start:
invoke CreateMutex, NULL,TRUE,ADDR uno
.IF (eax)
mov hMutex,eax
invoke GetLastError
.IF (eax!=ERROR_ALREADY_EXISTS)
; ..
;your code here
; ..
.ENDIF
invoke CloseHandle, hMutex
.ENDIF
invoke ExitProcess, NULL
end start
Thanks guys! will try your sample code :thumbu :thumbu
Is this going to run on a server with RDP connections?
A mutex is ONLY good for the current NAMESPACE. WTS creates multiple NAMESPACES, one for each WTS session.
Regards, P1 :8)
The Mutex is STILL the way to go as there are no contentions between NAMESPACES. Each considers themselves to be unique. In other words, who cares if there are multiple instances running in different NAMESPACES? No problems there, they can't bump into each other. It only matters if you try to run more than one instance in the same NAMESPACE and Mutex will prevent that.
Paul
Quote from: PBrennick on April 27, 2006, 07:47:09 PMEach considers themselves to be unique. In other words, who cares if there are multiple instances running in different NAMESPACES?
I care, when I only want one instance running. Because I found out the hard way. It is still a
resource issue on a single machine running multiple NAMESPACES.
I was writing some software to do maintenance on multiple uP server. I had problems with single instance using a mutex. The maintenance event was running in all namespaces causing the resource issue. It was file processing, imagine my surprize, when one instance rename a file and the next instance crashed because of it. What other instance? Didn't I set a mutex for this reason?
Please review: It has some details to help understand what is going on.
http://www.masmforum.com/simple/index.php?topic=3608.0
Quote from: PBrennick on April 27, 2006, 07:47:09 PMThe Mutex is STILL the way to go ...
Yes, I agree, but understand it's limitation as Hardware/OS technology changes.
I had to figure this out myself, because we were too acustomed to the old way of doing things.
Regards, P1 :8)
Yeah, well maybe I am too old, also. Anyway, how many situations do you suppose will reproduce your event? Especially in a home setting? Not may people have that type of set up. I prefer to talk to the average user and not confuse them with a lot of fancy stuff, it is kind of showy, IMO, Michael.
I will admit that I did not realize that NAMESPACES shared resources, for someone in my business, that is useful knowledge. I consider that to not be a useful feature. Are you using 2K? I think I will make sure it is added to my old company's Call Desk. Of course, using Windows for us is the exception, NOT the rule. Not enough security for our tastes. Still, it might save someone's butt someday.
Paul
Quote from: PBrennick on April 27, 2006, 11:46:26 PMAre you using 2K?
Window 2003 Server, Dual Processor, and the problem was for WTS sessions. Your right, it's a networking exception. Does that mean we are exceptionally good? :lol
But with advent of dual core technology and Vista just around the corner, you never know the depth of trouble M$ will put us in.
Regards, P1 :8) ©>«
Great! its working now! Thanks a lot guys! :clap:
but wait, what is this thing you have been talking about 'not good for win2k' or dual core cpu
does this mean, this routine wont work or not a good idea for this kind of setup system??
is there a better way?? :red
Well, i share what i learned...
invoke CreateMutex, NULL,TRUE,ADDR uno
.IF (eax)
mov hMutex,eax
invoke GetLastError
.IF (eax!=ERROR_ALREADY_EXISTS)
jmp ThisAppJustRunOnce
.ELSE
invoke MessageBox, NULL,addr MsgCaptionError, addr MsgBoxText,
MB_OK or MB_ICONEXCLAMATION
.ENDIF
invoke CloseHandle, hMutex
.ENDIF
invoke ExitProcess, NULL
ThisAppJustRunOnce:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke WinMain, hInstance,NULL,NULL, SW_SHOWDEFAULT
invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,
CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
continue with the main....
Thanks! :U
Quote from: bloodhound on April 28, 2006, 02:14:35 AMbut wait, what is this thing you have been talking about 'not good for win2k' or dual core cpu does this mean, this routine wont work or not a good idea for this kind of setup system??
We are talking about the exception, not the rule, unless this software is going to be running on a server. Don't worry about it. Took me ten years of Win32 API work to run into it.
Regards, P1 :8)
Michael,
Do you see the unnecessary problem you have created here? But I do respect you and do not believe you had that intention. It is just that sometimes we can be too helpful. I have done this, also.
Paul
Quote from: PBrennick on April 27, 2006, 11:46:26 PM
Yeah, well maybe I am too old, also. Anyway, how many situations do you suppose will reproduce your event? Especially in a home setting? Not may people have that type of set up. I prefer to talk to the average user and not confuse them with a lot of fancy stuff,
You are right Paul, not many people have this sort of setup at home. But if you have any intention of running your software inside a company then you need to be aware of this, as a lot of companies run terminals either by Citrix or WTS or some other terminal management software. I also got bitten by this problem when i was new to the corporate world, and i have never forgotten it since :P
If its on a local machine and is a Window style app, this is the macro I use which is very conventional old stuff.
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
Nice macro Hutch. :)
Damn, we really need a concise list of all the macros in the package. So much useful stuff there, but some/most of it likely goes un-noticed.
Yes, that's a good idea!
try to work that out, a compilation of macros or samples/tutorials here in the forums
:U
@hutch
can you give me an example of how that code works or you just put it at the start of code?
thanks!