CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2003
    Posts
    402

    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!!

  2. #2
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    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 ...

  4. #4
    Join Date
    Mar 2003
    Posts
    402
    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!

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    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 !!

  6. #6
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378
    BOOL CMyApp::InitInstance()
    {
    HANDLE hMutex = NULL;
    hMutex = CreateMutex (NULL, TRUE, "Exists");
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
    return false; // if exist then exit
    }

  7. #7
    Join Date
    Mar 2004
    Posts
    5
    CDialog has the "PreCreateWindow(CREATESTRUCT& cs)" method. You can insert it from the ClassWizard.
    Please change parameters of your window in this method.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Take a look at the following FAQ...

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430
    ... 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

  10. #10
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Try this...
    Code:
    if( IsWindow(m_hDlg) )
    {
        // Make it ShowWindow, Foreground etc
        return 1;
    }
    // Create a new dialog
    Hope this helps,
    Regards,
    Usman.

  11. #11
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    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

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    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.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured