Search code examples
c++winapiprocessexit-code

How to get the returned exit value of an invoked program to store it internally?


This question is related to a previously asked question found here:

I'm able to invoke another executable within my runProgram() function. However, before this runProgram() function returns back to main() and closes the handle for the process. I need to retrieve the value that the executable returns when it exits...

Here is my current application:

#include <Windows.h>
#include <exception>
#include <stdio.h>
#include <tchar.h>
#include <cstdint>
#include <iostream>

uint32_t runProgram(LPCSTR lpApplicationName) {
    STARTUPINFOA si;
    PROCESS_INFORMATION pi;

    // Set the size of the structures
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // Run the program
    CreateProcessA(
        lpApplicationName,  // the path
        NULL,               // Command line
        NULL,               // Process handle not inheritable
        NULL,               // Thread handle not inheritable
        FALSE,              // Set handle inheritance to FALSE
        CREATE_NEW_CONSOLE, // Opens file in seperate console
        NULL,               // Use parent's environment block
        NULL,               // Use parent's starting directory
        &si,                // Pointer to STARTUPINFO structure
        &pi                 // Pointer to PROCESS_INFORMATION structure
    );

    uint32_t error = GetLastError();

    if ( error != 0)
        std::cerr << error << "\n";

    // How to retrieve and store the result from the exiting program above...?
    uint32_t cache_size = 0;

    WaitForSingleObject(pi.hProcess, INFINITE);

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return cache_size;
}

int main() {
    try {
        const uint32_t cache_size = runProgram("CacheQuerry.exe");
        std::cout << cache_size << '\n';
    }
    catch (const std::exception& e) {
        std::cerr << e.what() << "\n\n";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

And I want to store the return value from this executable into runProgram()'s local variable: cache_size.

Here is the main.cpp from the invoked program:

#include "CacheQuerry.h"

int main() {
    int error = cache_info();
    if (error != 0)
        std::cout << error << '\n';
    else
        std::cout << '\n';
    std::cout << "l1d_cache_size = " << l1d_cache_size() << std::endl;
    std::cout << "cache line size = " << cache_line_size() << '\n';
    return l1d_cache_size();
}

The invoked program will return the value produced by l1d_cache_size() when this program exits. This is the value that I want to store within runProgram()'s cache_size variable. How do I get this return value after the program exits? The implementation of the function call found in "CacheQuerry.h" shouldn't be relevant here, but if you need to see it's implementation don't hesitate to ask. These are two separate projects within the same solution. The 1st main.cpp which is in its own project relies on the 2nd main.cpp within its own project.


Solution

  • You can get it by calling GetExitCodeProcess (pi.hProcess), after waiting on the handle but before you close it.

    The exact code you need is:

    DWORD exit_code;
    BOOL ok = GetExitCodeProcess (pi.hProcess, &exit_code);
    

    Strange this is not mentioned in the Remarks section for CreateProcess at all.