🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #12015

Stopping multiple instances of an application

#12015
From: Andrew
Date: Tue, 06 Jan 2004 16:35
75 lines
3470 bytes
Hi all,

I am trying to stop more than one instance of my app running at any one
time, normally I use a mutex and this works fine, most of the applications
we have developed have used Win 9x but now we have moved to win 2k and the
mutex doesn't seem to work any more, I have also tried semaphores and the
API function FindWindow, please see the following

Using a mutex:

        HANDLE mutex;
        String mutexname = "Concierge.Server.mutex";
        mutex = OpenMutex(NULL,false,mutexname.c_str());

        if (mutex == NULL)
            mutex = CreateMutex(NULL,true,mutexname.c_str());
        else
        {
            MessageBox(0,"This application is already running.\n"
                  "You may only run one instance at any one
time","Error",MB_OK);
            return 0;
        }

   Using FindWindow():

      Application->Title = "";
      HWND hPrevApp = ::FindWindow(NULL, "Sentinel Server V5 - Beta");

     // FindWindow returns a non-zero HWND if it finds the window
     // If the window was found, restore it and return without running
     if(hPrevApp)
     {
             MessageBox(0,"This application is already running.\n"
                  "You may only run one instance at any one
time","Error",MB_OK);
         return 0;
     }

Using Semaphore:

        HANDLE mutex;
        String mutexname = "Concierge.Server.mutex";
        mutex = OpenSemaphore(NULL,false,mutexname.c_str());
        if(mutex == NULL)
             mutex = CreateSemaphore(NULL,0,1,mutexname.c_str());
        else
        {
                MessageBox(0,"This application is already running.\n"
                  "You may only run one instance at any one
time","Error",MB_OK);
            return 0;
        }


None of the above seem to work, the mutex and semaphore always return a NULL
value in the variable mutex.

The FindWindow() function always finds the window even if the concierge
server is not running.

All the documentation I have read seems to indicate that the above three
methods should work in Win 2k, so I must be doing something wrong. Does any
one have any ideas or and alternate method for stopping two instances of an
app running at once.

I know could create an empty file at startup, when a second instance tries
to run if it finds this file it terminates instantly, therefore only one
instance can run, when the app is terminated the file is deleted. I don't
think this is an ideal solution though, ie the app crashes and the file does
not get deleted, could cause problems.

Any Ideas

TIA Andrew
Technical Details
Message-ID: <3ffae3bc@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,557b0e7a5128b725,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-01-06 08:40:16 PST
From: "Andrew" <alamb@tynetec.co.uk>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Stopping multiple instances of an application
Date: Tue, 6 Jan 2004 16:35:06 -0000
Lines: 77
Organization: Tynetec Ltd
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 80.177.44.179
Message-ID: <3ffae3bc@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1073406908 80.177.44.179 (6 Jan 2004 08:35:08 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail