🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Activating the first instance of a program when it's started multiple times

#6668
From: PaoloItaly
Date: Fri, 22 Apr 2005 10:01
39 lines
2024 bytes
HANDLE mutex;
WINAPI WinMain(HINSTANCE hCurInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
  try
  {
    mutex = ::CreateMutex(&sa, true, "sirconv.mutex");
    if (::GetLastError() == ERROR_ALREADY_EXISTS)
    {
      // Find the HWND of the hidden application window of the previous
      // instance. Note that we are not looking for the HWND of the main
      // form. This code fails miserably if you pass it the main form
      // HWND instead of the Application's HWND
      Application->Title = "";
      HWND hPrevApp = ::FindWindow("TSirconvMainForm", NULL);
      if(hPrevApp)
      {
        ::SendMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0);
        ::SetForegroundWindow(hPrevApp);
      }
    }
    else
    {
      Application->Initialize();
      Application->CreateForm(__classid(TSirconvMainForm),
&SirconvMainForm);
       Application->Run();
    }
  }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }

  if(mutex)
    ::CloseHandle(mutex);

  return 0;
}
Technical Details
Message-ID: <4268cbb1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!nntp.abs.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <42688891@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3c4287e4044a862e
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "PaoloItaly" <paolot@FAKETHESPAMMERsirius-es.it>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <42688891@newsgroups.borland.com>
Subject: Re: Activating the first instance of a program when it's started multiple times
Date: Fri, 22 Apr 2005 12:01:33 +0200
Lines: 41
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 85.32.246.173
Message-ID: <4268cbb1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1114164145 85.32.246.173 (22 Apr 2005 03:02:25 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!nntp.abs.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail