Article View: comp.os.ms-windows.programmer.misc
Article #2924Re: How to avoid multiple instances of a program in NT and 95?
From: Richard Baldwin
Date: Mon, 03 Feb 1997 00:00
Date: Mon, 03 Feb 1997 00:00
100 lines
4300 bytes
4300 bytes
In article <5d4eeb$n6u$1@euler.space.net>, Alois Buecherl
<Alois.Buecherl@terraSat.de> writes
>Hello,
>I've been striving for so long now to find a SAVE way to start only one
>instance of my C++ program under Windows NT and 95. As you might recall, this
>used to be that easy in Windows 3.1 using the prevInstance variable. Now this
>option is gone.
>I've tried using a semaphore that get's locked when the programm's up. But
>it still can happen, that the program starts up more than once when multiple
>prgram spawns occur very rapidly. The program then did not get enough
>startup-time to create and lock the semaphore.
>Is there ANY solution to this problem?
>
>Any hint into a possibly right direction would be greatly appreciated!
>
>Alois
This is how to do it. This method uses the application class name to
prevent multiple instances. If you want to change the name of your class
to something other than the app name, then replace all three items with
^^^^^^ under them to the same variable/literal. If you have problems
with this code then email me.
In CMainFrame
=============
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass=AfxGetAppName();
^^^^^^^^^^^^^^^
return CFrameWnd::PreCreateWindow(cs);
}
In CYourApp
===========
static BOOL bReg = FALSE;
BOOL CYourApp::InitInstance()
{
// Only allow one instance - if one already exists bring
// it to the top and make it active.
if (!CheckFirst()) return FALSE;
// Register my class using my name
WNDCLASS wndclass;
memset(&wndclass,0,sizeof(WNDCLASS));
*****YOUR STYLES MAY REPLACE THOSE BELOW*******
wndclass.style=CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc= ::DefWindowProc;
wndclass.hInstance=AfxGetInstanceHandle();
wndclass.hIcon=LoadIcon(IDR_MAINFRAME);
wndclass.hCursor=LoadCursor(IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszClassName=m_pszAppName;
^^^^^^^^^^^^
if (!AfxRegisterClass(&wndclass)) return FALSE;
// Set a flag to show we've done it
bReg=TRUE;
******THE REST OF INITINSTANCE GOES HERE*********
}
int CYourApp::ExitInstance()
{
// If I registered my class, unregister it
if (bReg) UnregisterClass(m_pszAppName,AfxGetInstanceHandle());
^^^^^^^^^^^^
return CWinApp::ExitInstance();
}
// This function check's to see if there is another window of my
// class. If there is, bring it and any children or popups to the top
// and activate it. Returns TRUE if this instance is the first.
BOOL CYourApp::CheckFirst()
{
CWnd* wndPrev;
CWnd* wndChild;
if (wndPrev=CWnd::FindWindow(m_pszAppName,NULL))
{
wndChild=wndPrev->GetLastActivePopup();
if (wndPrev->IsIconic()) wndPrev-ShowWindow(SW_RESTORE);
wndChild->SetForegroundWindow();
return FALSE;
}
return TRUE;
}
Add to the header file for CYourApp
===================================
BOOL CheckFirst();
--
Richard Baldwin
Technical Details
Message-ID:
<aFwHpCAU3e9yEwa8@ormesci.demon.co.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<5d4eeb$n6u$1@euler.space.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1124cf,3403ef0501dd273 X-Google-Attributes: gid1124cf,public X-Google-Thread: 113dfb,3403ef0501dd273 X-Google-Attributes: gid113dfb,public X-Google-Thread: f6ef4,3403ef0501dd273 X-Google-Attributes: gidf6ef4,public X-Google-Thread: 105df1,3403ef0501dd273 X-Google-Attributes: gid105df1,public From: Richard Baldwin <richard@ormesci.demon.co.uk> Subject: Re: How to avoid multiple instances of a program in NT and 95? Date: 1997/02/03 Message-ID: <aFwHpCAU3e9yEwa8@ormesci.demon.co.uk> X-Deja-AN: 214121667 distribution: world x-nntp-posting-host: ormesci.demon.co.uk references: <5d4eeb$n6u$1@euler.space.net> organization: Orme Scientific mime-version: 1.0 newsgroups: comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.win32,de.comp.os.ms-windows.programmer,fido.ger.windows.prog