Launch and wait for application to end

This is a dll that exports a function called Wait(). Wait() is similiar with WinExec(). The difference is that Wait()

run an application and return until the application ends.

This project consists of 3 functions:

Wait()

Create a thread and wait for an event. Use pThreadEvent to identify if the called application ends. WaitForSingleObject()

pauses the application and waits for the called application to end.

_declspec(dllexport) BOOL Wait(CString szCmdLine)
{
    LPTHREADINFO pThreadInfo = new THREADINFO;
    CEvent *pThreadEvent = new CEvent(FALSE, TRUE);
    ASSERT_VALID(pThreadEvent);
    if(pThreadInfo)
    {
        pThreadInfo->pTermThreadEvent = pThreadEvent;
        pThreadInfo->strPathName = szCmdLine;

        AfxBeginThread(LaunchAndWait, pThreadInfo);
        WaitForSingleObject(pThreadEvent->m_hObject, INFINITE);
        return TRUE;
    }
    return FALSE;
}

LaunchAndWait()

Call LaunchApplication() to run the application and wait for an event.WaitForMultiObjects() wait for either the called application to end or the main process to end.

UINT LaunchAndWait(LPVOID pParam)
{
    LPTHREADINFO pThreadInfo = (LPTHREADINFO) pParam;
    PROCESS_INFORMATION stProcessInfo;
    if(LaunchApplication(pThreadInfo->strPathName, &stProcessInfo))
    {
        HANDLE hThreads[2];
        hThreads[0] = pThreadInfo->pTermThreadEvent->m_hObject;
        hThreads[1] = stProcessInfo.hProcess;
        DWORD dwIndex = WaitForMultipleObjects(2, hThreads, FALSE, INFINITE);
        CloseHandle(stProcessInfo.hThread);
        CloseHandle(stProcessInfo.hProcess);
        pThreadInfo->pTermThreadEvent->SetEvent();
        if(pThreadInfo)
            delete pThreadInfo;
    }
    else
        pThreadInfo->pTermThreadEvent->SetEvent();
    return 0;
}

LaunchApplication()

Call CreateProcess to run the application.

BOOL LaunchApplication(LPCTSTR pCmdLine, PROCESS_INFORMATION *pProcessInfo)
{
    STARTUPINFO stStartUpInfo;

    memset(&stStartUpInfo, 0, sizeof(STARTUPINFO));

    stStartUpInfo.cb = sizeof(STARTUPINFO);

    stStartUpInfo.dwFlags = STARTF_USESHOWWINDOW;
    stStartUpInfo.wShowWindow = SW_SHOWDEFAULT;

    return CreateProcess(NULL, (LPTSTR)pCmdLine, NULL, NULL, FALSE,
        NORMAL_PRIORITY_CLASS, NULL,
        NULL, &stStartUpInfo, pProcessInfo);
}

Download source - 8KB



Comments

  • Problem with replacing WinExec by CreateProcess inside a DLL !!! HELP

    Posted by Legacy on 12/07/2000 12:00am

    Originally posted by: Bhouri

    I have replaceed WinExec by LaunchApplication (CreateProcress) successfully, when the modifications are inserted in the main application files
    But when this routine is inserted in an attached DLL
    it doesn't work
    (WinExec always works)
    Can someone help me to configure correctly CreateProcess
    when it is inserted in the DLL

    email me at bhouri@yahoo.com

    Reply
  • duhhhhhhhhhh .. but you should change your docs!

    Posted by Legacy on 05/04/1999 12:00am

    Originally posted by: Mason Deming

    usertypes.dat should be usertype.dat

    Reply
  • Can't wait two 16bit setup program.

    Posted by Legacy on 03/14/1999 12:00am

    Originally posted by: lawrence

    The wait program can't wait for two 16bit program, in details, 16bit setup program will execute the program
    as a new thread in the shared "ntvdm" process, in this case, if you wait for the process to end
    infinitely, that's impossible. If you have some idea about it, please post it on codeguru or contact via
    email directly, Many thanks.
    

    Reply
  • Posted by Legacy on 11/30/1998 12:00am

    Originally posted by:

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds