🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Run Once

#9931
From: JD
Date: Mon, 31 May 2004 15:51
70 lines
3271 bytes
I don't understand why others haven't encountered this problem
before. Hans tested the code I posted with BCB3/Win98SE and
had the same problems as me.

I'm just posting my solution for future reference for others.

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        HANDLE hMutex = CreateMutex( NULL, TRUE, "MyUniqueMutexName" );
        if( GetLastError() == ERROR_ALREADY_EXISTS )
          {
              Application->Title = "";
              HWND hWnd = ::FindWindow( NULL, "My Form Caption" );
              if( hWnd )
                {
                    ::SendMessage( hWnd, APP_RESTOREFIRST, 0, 0 );
                    ::SetForegroundWindow( hWnd );
                }
              if( hMutex ) CloseHandle( hMutex );
              return 0;
          }
        try
          {
              Application->Initialize();
              Application->Title = "My Application Title";
              Application->CreateForm(__classid(TForm1), &Form1);
              Application->Run();
          }
        catch( Exception &exception )
          {
              Application->ShowException( &exception );
          }
        catch( ... )
          {
              try
                {
                    throw Exception( "" );
                }
              catch( Exception &exception )
                {
                    Application->ShowException( &exception );
                }
          }
        ReleaseMutex( hMutex );
        if( hMutex ) CloseHandle( hMutex );
        return 0;
}
//-------------------------------------------------------------
void __fastcall TForm1::WndProc(TMessage &Message)
{
    static bool IsMinimized = false;
    if( Message.Msg == WM_SYSCOMMAND )
      {
          switch( Message.WParam )
            {
                case  SC_RESTORE:
                case SC_MAXIMIZE: IsMinimized = false; break;
                case SC_MINIMIZE: IsMinimized = true;
            }
      }
    else if( Message.Msg == APP_RESTOREFIRST )
      {
          if( IsMinimized ) Application->Restore();
      }
    TForm::WndProc(Message);
}
//-------------------------------------------------------------

~ JD
Technical Details
Message-ID: <40bb5474$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40b9cff4$1@newsgroups.borland.com> <k5ikb0dppip15jep979bhuf7g148fsp3lj@4ax.com> <40ba78cc$1@newsgroups.borland.com> <40ba7ce2$1@newsgroups.borland.com> <40ba7f15$1@newsgroups.borland.com> <40ba9d87$1@newsgroups.borland.com> <40bb3815$1@newsgroups.borland.com> <40bb4483$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,55fa8aeaf57fcad1
X-Google-Attributes: gid10cc67,public
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: Run Once
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.144.41.186     
References: <40b9cff4$1@newsgroups.borland.com> <k5ikb0dppip15jep979bhuf7g148fsp3lj@4ax.com> <40ba78cc$1@newsgroups.borland.com> <40ba7ce2$1@newsgroups.borland.com> <40ba7f15$1@newsgroups.borland.com> <40ba9d87$1@newsgroups.borland.com> <40bb3815$1@newsgroups.borland.com> <40bb4483$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <40bb5474$1@newsgroups.borland.com>
Date: 31 May 2004 08:51:16 -0700
X-Trace: newsgroups.borland.com 1086018676 207.105.83.66 (31 May 2004 08:51:16 -0700)
Lines: 71
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!npeer.de.kpn-eurorings.net!feed.news.schlund.de!schlund.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail