RunApplication

Runs an application and waits for it to return.

Archived content: This source code is currently inactive and may be outdated or no longer maintained or functional.

Ein Prozess ist schnell gestartet. Aber dann auch noch auf dessen Beendigung zu warten und den Rückgabewert auszulesen erfordert im Windows-API schon ein paar weitere Zeilen. Die sind in der RunApplication-Funktion dargestellt.

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

RunApplication.cpp850 BQuelltext der RunApplication-Funktion

Contents of the file RunApplication.cpp:

// Runs an application and waits for it to return.
//
// Dependencies: CString
// Status: unchecked
//
// Web: http://unclassified.software/source/runapplication
//
int RunApplication(CString strPath, CString strArgs)
{
    BOOL                rc;
    STARTUPINFO         si;
    PROCESS_INFORMATION pi;
    CString             strCmdLine;

    strCmdLine.Format("%s %s", strPath, strArgs);
    // TODO: why don't we use CreateProcess' 1st parameter?
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));
    rc = CreateProcess(NULL,
                strCmdLine.GetBuffer(strCmdLine.GetLength() + 1),
                NULL,
                NULL,
                FALSE,
                0,
                NULL,
                NULL,
                &si,
                &pi);
    if (rc)
    {
        WaitForSingleObject(pi.hProcess, INFINITE); // Wait until child process exits.
        CloseHandle(pi.hProcess);                   // Close process and thread handles.
        CloseHandle(pi.hThread);
    }
    return rc;
}

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02.