|
-
December 9th, 2003, 02:09 PM
#1
Creating Only One Instance
Here is what I am trying to do: I have a Dialog Application and I only want to have one instance running at a time.
Here is the code that I have, but it still allows for mulitple instances.
LPCTSTR lpszUniqueClass = _T("MsdSaveData");
BOOL CGSaveApp::InitInstance()
{
// InitCommonControls() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
// check for previous instance of Software
if(FirstInstance()== FALSE)
{
return(FALSE);
}
InitCommonControls();
CWinApp::InitInstance();
AfxEnableControlContainer();
// Register our unique class name that we wish to use
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS)); // start with NULL defaults
wndcls.style = CS_DBLCLKS |CS_HREDRAW |CS_VREDRAW;
wndcls.lpfnWndProc = : efWindowProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = NULL;
wndcls.hCursor = LoadCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.cbWndExtra = DLGWINDOWEXTRA;
wndcls.lpszClassName = lpszUniqueClass;
// register MsdSaveData class name
if(!AfxRegisterClass(&wndcls))
{
return FALSE;
}
CGSaveDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
BOOL CGSaveApp::FirstInstance(void)
{
CWnd *pWndPrev, *pWndParent, *pWndChild;
// check for another copy of wnd class running
pWndPrev = CWnd::FindWindow(lpszUniqueClass,NULL);
if(pWndPrev)
{
pWndPrev->SetWindowText("TRUE");
pWndParent = pWndPrev->GetParent();
// find child popup
pWndChild = pWndParent->GetLastActivePopup();
// pWndParent->BringWindowToTop();
// activate previous copy
if(pWndParent->IsIconic())
{
pWndParent->ShowWindow(SW_SHOWNORMAL);
}
pWndChild->SetForegroundWindow();
return(FALSE);
}
// first instance
return(TRUE);
}
Thanks I have not been able to figure this out!!
-
December 9th, 2003, 02:11 PM
#2
-
December 9th, 2003, 02:13 PM
#3
What do you mean by still allows multiple instances ?
Does it mean that multiple dialogs are shown ?
How fast are you invoking these multiple apps? There is a chance that the FindWindow may fail to look for certain classes if you are doing it too fast.
A named mutex might me a better option ...
-
December 9th, 2003, 02:20 PM
#4
I guess for instance, if I have the application Icon on the Desktop and I start the first application, let it run, then minimize it.
Next I double click the same application and another instance begins as opposed to just Maximizing the previous instance.
I just want to make it to where I only have One instance running at a time to keep people from constantly Double Clicking on the application Icon!
-
December 9th, 2003, 02:22 PM
#5
I think I see the problem. You are registering a class but you are not creating a window using that class. So FindWindow will naturally fail !!
-
December 9th, 2003, 02:29 PM
#6
BOOL CMyApp::InitInstance()
{
HANDLE hMutex = NULL;
hMutex = CreateMutex (NULL, TRUE, "Exists");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
return false; // if exist then exit
}
-
March 12th, 2004, 02:06 AM
#7
CDialog has the "PreCreateWindow(CREATESTRUCT& cs)" method. You can insert it from the ClassWizard.
Please change parameters of your window in this method.
-
March 12th, 2004, 03:55 AM
#8
Take a look at the following FAQ...
-
March 12th, 2004, 04:45 AM
#9
... I only want to have one instance running at a time.
The best article describing "HOW TO DO IT" I ever seen is:
Avoiding Multiple Instances of an Application
-
March 12th, 2004, 05:57 AM
#10
Try this...
Code:
if( IsWindow(m_hDlg) )
{
// Make it ShowWindow, Foreground etc
return 1;
}
// Create a new dialog
Hope this helps,
Regards,
Usman.
-
March 12th, 2004, 01:33 PM
#11
I think most of the other suggestions should have solved the problem, but here is another link for completeness:
http://support.microsoft.com/default...NoWebContent=1
-
March 12th, 2004, 09:00 PM
#12
Originally posted by Valery B.
CDialog has the "PreCreateWindow(CREATESTRUCT& cs)" method. You can insert it from the ClassWizard.
Please change parameters of your window in this method.
Have you done that? As far as I know, that won't work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|