CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Posts
    13

    ShellExecute - only one instance???

    Hi,
    I want to execute/view a program with shellexecute, but when there's already an instance of this program this instance should be activated (as in, restore/focus regain/etc).

    The program im building is called 'open.exe'

    When you run 'open test.txt', a shellexecute 'test.txt' is done, wich results in notepad beeing executed with test.txt as parameter (?).

    when you run 'open test.txt' a second time, a new notepad instance will be created with test.txt as param.

    I want to skip this new instance the second time , but only activate the first instance.

    Ps. when a instance of 'test.txt - Notepad' is opened, a call like 'open test2.txt' should result in a new notepad instance with test2.txt as param.

    I'm wrestiling with this problem for about 2 weeks now, and i'm getting very depressed... Please help...




  2. #2
    Join Date
    May 1999
    Posts
    26

    Re: ShellExecute - only one instance???

    Hi!

    I would add the following code in the InitInstance of my program to achieve what you want. If you have any quetions, mail me... Lemme see how it goes. (This is not certainly the best way to do this.) Hope this helps!!

    Santhosh




    // Assuming that you have given the file to be opened
    // in the command line (and nothing else)
    CString strFile = (CString)m_lpCmdLine;

    BOOL bRunning = FALSE;

    char chWndName[50];
    CString strWnd("");

    HWND hParentWnd = ::GetDesktopWindow();
    HWND hWnd = ::GetWindow(hParentWnd, GW_CHILD);
    ::GetWindowText(hWnd, chWndName, 50);

    strWnd = CString(chWndName);

    if(!strWnd.IsEmpty())
    {
    if(strWnd.Find("Notepad") != -1)
    {
    int nPos = strWnd.ReverseFind ('-');
    CString strFileName = strWnd.Left (nPos - 1);

    if (!strFile.CompareNoCase (strFileName))
    {
    SetForegroundWindow (hWnd);
    bRunning = TRUE;
    }
    }
    }

    while(hWnd && !bRunning)
    {
    hWnd = ::GetWindow(hWnd, GW_HWNDNEXT);

    if(hWnd)
    {
    ::GetWindowText(hWnd, chWndName, 50);
    strWnd = CString(chWndName);

    if(!strWnd.IsEmpty())
    {
    if(strWnd.Find("Notepad") != -1)
    {
    int nPos = strWnd.ReverseFind ('-');
    CString strFileName = strWnd.Left (nPos -1);

    if (!strFile.CompareNoCase (strFileName))
    {
    SetForegroundWindow (hWnd);
    bRunning = TRUE;
    }
    }
    }
    }
    }


    if (!bRunning)
    {
    ShellExecute (NULL,
    "open",
    "Notepad.exe",
    strFile,
    "C:\\",
    SW_RESTORE);
    }




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