🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Mutex troubles?

#4378
From: Remy Lebeau (TeamB)
Date: Fri, 24 Feb 2006 23:29
91 lines
3260 bytes
"Nate Lockwood" <RNLockwoodREMOVE@attglobal.net> wrote in message
news:43ff726e$1@newsgroups.borland.com...

> It appears that I always get a non null mutex

As you should be.  That is what CreateMutex() does.  You are not taking into
account that the returned pointer is always non-NULL even if the pointer
belongs to another existing mutex.  The only time CreateMutex() returns NULL
is when a real error occurs (out of resources, etc).

> Here's some code.

Use something more like the following code instead:

    --- Project .cpp---

    UINT WM_MYMSG = 0;

    WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
        WM_MYMSG =
RegisterWindowMessage("USDA.FS.FireScience.GooleEarthTool.Awake");

        HANDLE mutex = ::CreateMutex(NULL, TRUE,
"USDA.FS.FireScience.GooleEarthTool");
        if( m_Handle == NULL )
            return 0;

        if( ::GetLastError() == ERROR_ALREADY_EXISTS )
        {
            if( WM_MYMSG != 0 )
            {
                Application->Title = "";
                HWND hWnd = ::FindWindow("TApplication", "Google Earth (C)
Tool");
                if( hWnd )
                    ::PostMessage(hWnd, WM_MYMSG, NULL, NULL);
            }
        }
        else
        {
            try
            {
                Application->Title = "Google Earth (C) Tool";
                Application->Initialize();
                Application->CreateForm(__classid(TFormMain), &FormMain);
                Application->CreateForm(__classid(TAboutBox), &AboutBox);
                Application->Run();
            }
            catch(Exception &exception)
            {
                SysUtils::ShowException(&exception);
            }
        }

        if( mutex )
            ::CloseHandle(mutex);

        return 0;
    }


    --- Form.cpp ---

    extern UINT WM_MYMSG;

    __fastcall TFormMain::TFormMain(TComponent *Owner)
        : TForm(Owner)
    {
        Application->OnMessage = AppMessage;
    }

    __fastcall TFormMain::~TFormMain()
    {
        Application->OnMessage = NULL;
    }

    void __fastcall TFormMain::AppMessage(MSG &Msg, bool &Handled)
    {
        if( (Msg.message == WM_MYMSG) && (WM_MYMSG != 0) )
        {
            Handled = true;
            Show();
        }
        else
            Handled = false;
    }


Gambit
Technical Details
Message-ID: <43ff96a0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <43ff726e$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7b6bdf1fac3d7d96
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <43ff726e$1@newsgroups.borland.com>
Subject: Re: Mutex troubles?
Date: Fri, 24 Feb 2006 15:29:15 -0800
Lines: 93
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43ff96a0$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1140823712 68.69.17.41 (24 Feb 2006 15:28:32 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!news.glorb.com!newsfeed.cw.net!cw.net!news-FFM2.ecrc.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail