News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

detect and allow 1 instance of a program???

Started by bloodhound, April 27, 2006, 12:59:33 PM

Previous topic - Next topic

bloodhound


asmfan

1. Mutexe
2. Memory mapped files (shared of course)
3. Shared sections
4. Window handle handling
5. Other...
Russia is a weird place

Tedd

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
No snowflake in an avalanche feels responsible.

bloodhound

Thanks guys! will try your sample code  :thumbu  :thumbu

P1

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)

PBrennick

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
The GeneSys Project is available from:
The Repository or My crappy website

P1

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)

PBrennick

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
The GeneSys Project is available from:
The Repository or My crappy website

P1

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)        ©>«

bloodhound

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



bloodhound

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

P1

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)

PBrennick

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
The GeneSys Project is available from:
The Repository or My crappy website

sluggy

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

hutch--

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
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php