🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Avoiding multiple programinstances and restore the firstone

#6670
From: Hans Galema
Date: Fri, 22 Apr 2005 11:17
59 lines
2400 bytes
If you want to avoid multiple programinstances and/or restore
the firstone then you can use code below.

The use of a mutex and after that searching for windows
would be doing twice nearly the same.

Just add some code to WinMain.

For SubZero and PaoloItaly.

Hans.

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
   try
   {
        Application->Initialize();

        // -------------------------------------
        AnsiString OldTitle = Application->Title;

        Application->Title = "";

        HWND hwnddesktop = GetDesktopWindow();
        HWND hwnd = FindWindowEx(hwnddesktop, 0, 0, 0);

        while( hwnd )
           {
           char titlebuffer[250];

           GetWindowText(hwnd, titlebuffer, sizeof(titlebuffer));

           if ( OldTitle == AnsiString ( titlebuffer ) )
              {
              ShowMessage ( "this is not the first instance ("
                  + AnsiString ( titlebuffer )
                  + ") you will get the first one." );

              ::SendMessage( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
              ::SetForegroundWindow( hwnd );

              return 1;
              }

           hwnd = FindWindowEx(hwnddesktop, hwnd, 0, 0);
          }

       Application->Title = OldTitle;
      // -----------------------------------

      Application->CreateForm(__classid(TForm1), &Form1);
      Application->Run();
   }
   catch (Exception &exception)
   {
      Application->ShowException(&exception);
   }
   return 0;
}
Technical Details
Message-ID: <4268dde6$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,fd5c6cb9a40ec609,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Fri, 22 Apr 2005 13:17:30 +0200
From: Hans Galema <notused@notused.nl>
User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Avoiding multiple programinstances and restore the firstone
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 129.125.201.248
Message-ID: <4268dde6$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1114168806 129.125.201.248 (22 Apr 2005 04:20:06 -0700)
Lines: 59
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail