🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: mutex won't release

#9194
From: JD
Date: Sat, 24 Jul 2004 01:14
50 lines
3020 bytes
<David> wrote:
>Can you show us the working version?

That's only 2 lines that have already been posted (modified):

    HANDLE hMutex = ::CreateMutex( NULL, TRUE, "MutexName" );
    if( hMutex ) ::CloseHandle( hMutex );

To put it into a meaningful context, the following code (from
a component) allows only a single instance of an application
to be loaded. The form that it references is the main form and
the class name of the main form needs to be unique for it to
work. Naming the main form something like CompanyNameApplicationName
provides enough assurance.

Note that modifying the WinMain is much simpler (than making
the code work in a component) and there are several examples
using the WinMain that can be googled up.

    char ThisClassName[ 256 ] = { 0 };
    ::GetClassName( pParentForm->Handle, ThisClassName, 255 );
    FCustomMessage = (AnsiString)ThisClassName + "CustomMessage";
    FMutexName = (AnsiString)ThisClassName + "UniqueMutexString";

    SINGLE_INSTANCE_RESTORE_MESSAGE = ::RegisterWindowMessage( FCustomMessage.c_str() );
    hMutex = ::CreateMutex( NULL, TRUE, FMutexName.c_str() );
    if( GetLastError() == ERROR_ALREADY_EXISTS )
      {
          // if we get here, an instance is already loaded
          // prevent this instance from showing
          Application->ShowMainForm = false;
          // find both instances
          hFirstInstance = hSecondInstance = NULL;
          ::EnumWindows( (WNDENUMPROC)EnumWindowsCallBack, (LPARAM)ThisClassName );
          // make sure the first handle point to the correct instance
          if( hFirstInstance == pParentForm->Handle ) hFirstInstance = hSecondInstance;
          // tell the first instance to restore itself
          ::SendMessage( hFirstInstance, SINGLE_INSTANCE_RESTORE_MESSAGE, 0, 0 );
          // bring the first instance to front incase it wasn't minimized
          ::SetForegroundWindow( hFirstInstance );
          // abort this instance
          Application->Terminate();
      }

Then in the destructor of the component:

    if( hMutex ) ::CloseHandle( hMutex );

~ JD
Technical Details
Message-ID: <4101b7f0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40feb982$1@newsgroups.borland.com> <40febcc2$1@newsgroups.borland.com> <41003d6b@newsgroups.borland.com> <410103c8@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ad55a220cec49a77
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: mutex won't release
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.151.77.158     
References: <40feb982$1@newsgroups.borland.com> <40febcc2$1@newsgroups.borland.com> <41003d6b@newsgroups.borland.com> <410103c8@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <4101b7f0$1@newsgroups.borland.com>
Date: 23 Jul 2004 18:14:24 -0700
X-Trace: newsgroups.borland.com 1090631664 207.105.83.66 (23 Jul 2004 18:14:24 -0700)
Lines: 51
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail