Launch and wait for application to end | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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;
}

Advertisement

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.