コマンドプロンプト or バッチファイル (=cmd.exe) 上:
> foobar.exe ... ... > echo %ERRORLEVEL%
Win32APIで取得するなら、WaitForSingleObject()でプロセスの終了を待ち、GetExitCodeProcess()で取得する。
例(invoker.c):
#include <windows.h> #include <stdio.h> int main(int argc, char *argv[]) { PROCESS_INFORMATION pi; STARTUPINFO si; BOOL r; DWORD rc; if (2 != argc) { fprintf(stderr, "usage: %s target_exe\n", argv[0]); return 1; } ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); if (!CreateProcess( NULL, // LPCTSTR lpApplicationName argv[1], // LPTSTR lpCommandLine NULL, // LPSECURITY_ATTRIBUTES lpProcessAttributes NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes FALSE, // BOOL bInheritHandles 0, // DWORD dwCreationFlags NULL, // LPVOID lpEnvironment NULL, // LPCTSTR lpCurrentDirectory &si, // LPSTARTUPINFO lpStartupInfo &pi // LPPROCESS_INFORMATION lpProcessInformation )) { fprintf(stderr, "CreateProcess(%s) failed, gle = %d\n", argv[1], GetLastError()); return 2; } if (WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess, INFINITE)) { GetExitCodeProcess(pi.hProcess, &rc); printf("terminate code of %s = %lu\n", argv[1], rc); } CloseHandle(pi.hThread); CloseHandle(pi.hProcess); return 0; }