🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Run Once

#10009
From: JD
Date: Sun, 30 May 2004 12:13
99 lines
4734 bytes
I implemented code in the WinMain to prevent multiple instances
of the application from running. I have a problem and a couple
questions as noted in the code:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
        HANDLE hMutex = ::CreateMutex( NULL, TRUE, "MySpecialMutexName" );
        if( GetLastError() == ERROR_ALREADY_EXISTS )
          {
              Application->Title = "";
              // If the Main form is an MDIParent, doesn't that mean
              // that FindWindow can fail here because the user can
              // maximize a Child form, changing the MDIParent's caption?
              HWND hWnd = ::FindWindow( NULL, "My Window Caption" );
              if( hWnd )
                {
                    if( ::IsIconic(hWnd) )
                      {
                          // This block never executes no matter what the state
                          ShowWindow( hWnd, SC_RESTORE );
                      }
                    else
                      {
                          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 );
                }
          }
        // Damon's code has the remaining lines as such but Gambit
        // said not to call ReleaseMutex. Does CloseHandle also release it?
        ReleaseMutex( hMutex );
        if( hMutex ) CloseHandle( hMutex );
        return 0;
}
//--------------------------------------------------------------

As for the problem that I'm having, it's restoring the window
found using FindWindow and the result of restoring it.

First, IsIconic always returns false no matter what the state
so I can't determine the window state of the HWND and I have no
clue why. I confirmed that the correct window was being found
by overriding the WndProc method and posting a message to the
first instance window. It is the correct HWND.

I don't know if this sheds any light on the problem but one of
the things that I did in the WndProc was to evaluate the form's
WindowState. I found that if it was wsNormal before I minimized
the form, the WndProc reported that the FormState was wsNormal.
The same happened if I maximized the form before I minimized
it - the WndProc reported the state as wsMaximize but never
wsMinimize.

The other problem seems to be related to the one above and is
the result of restoring the HWND. I changed the code to assume
that the HWND was minimized and used a number of different methods
to restore the window. All of them worked to restore the window but they all also had the same side effect.

The restored window could not be minimized again. While the minimize
button was still enabled (you can see the mouse over effects, hint
was displayed and it annimated when clicked), clicking it had no effect.

A clue to what this problem might be is that I could 'fix' this at
runtime by right clicking the application title in the taskbar and
selecting Restore from the popup menu. You could see the animation
for the form restoring and another right click in the taskbar showed
the Restore option greyed.

For what it's worth, while the minimize button problem was active,
the system menu (displayed by clicking the form's icon in the title
bar) always showed Restore as greyed.

BCB6 - XP

~ JD
Technical Details
Message-ID: <40b9cff4$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,55fa8aeaf57fcad1,start
X-Google-Attributes: gid10cc67,public
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Run Once
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 172.138.8.181     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <40b9cff4$1@newsgroups.borland.com>
Date: 30 May 2004 05:13:40 -0700
X-Trace: newsgroups.borland.com 1085919220 207.105.83.66 (30 May 2004 05:13:40 -0700)
Lines: 100
Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail