CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Location
    CA , USA
    Posts
    83

    opening many docs in the same application

    Hi i have an application that is a singleton. Only one instance of the application exists. Im achieving this by putting following code in the initinstance()


    [/code]

    char m_pszOneInstance[MAX_PATH];
    strcpy(m_pszOneInstance,"Mutex for Avoiding multiple copies of the same appliaction");
    //
    m_hMutexHandle = CreateMutex(NULL,TRUE,m_pszOneInstance);
    //
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
    // find previous apps main window
    CMainFrame *pPrevWnd =(CMainFrame*) CWnd::GetDesktopWindow()->GetWindow(GW_CHILD);
    while (pPrevWnd)
    {
    // does this window have the "previous instance tag" set ?
    if (::GetProp(pPrevWnd->GetSafeHwnd(),m_pszOneInstance))
    {
    // found the window, now restore if minimized
    if (pPrevWnd->IsIconic())
    pPrevWnd->ShowWindow(SW_RESTORE);
    // set focus
    pPrevWnd->SetForegroundWindow();
    // if window is a pop-up window, set focus to pop-up
    pPrevWnd->GetLastActivePopup()->SetForegroundWindow();
    // finally, exit from this instance of the app
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);
    if(cmdInfo.m_strFileName!="")
    {
    if(!obUtility.SetFileToOpenInRegistry(cmdInfo.m_strFileName))
    {
    AfxMessageBox("Problems in accessing registry");
    return TRUE;
    }
    SendMessage(pPrevWnd->GetSafeHwnd(),WM_FILEOPEN,0,0);
    }
    return TRUE;
    }
    // get next window in the list
    pPrevWnd = (CMainFrame*)pPrevWnd->GetWindow(GW_HWNDNEXT);
    }

    return TRUE;
    }

    [/code]

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    Whats the question....

    BTW: take the / off the first code tag. code starts a code section and /code ends it.

  3. #3
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939
    Everytime you detect a new instance starting, you need to also check your command line. This should have the filename of the file the user is wanting to open. When you switch to the other instance and close yourself down you need to copy the command line into a buffer that is shared between both instances and post a message to the other instance so that it can open the file for you.
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

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