尝试这样的事情:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
void system_error(char *name) {
// Retrieve, format, and print out a message from the last errror.
// The `name' that's passed should be in the form of a present tense
// noun (phrase) such as "opening file".
//
char *ptr = NULL;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0, GetLastError(), 0, (char *)&ptr, 1024, NULL);
printf("\nError %s: %s\n", name, ptr);
LocalFree(ptr);
}
PROCESS_INFORMATION p;
BOOL WINAPI die(DWORD reason) {
TerminateProcess(p.hProcess, 1);
return TRUE;
}
int main(int argc, char **argv) {
STARTUPINFO s;
memset(&s, 0, sizeof s);
s.cb = sizeof(s);
if (!CreateProcess(argv[1], argv[2], NULL, NULL, TRUE,
DETACHED_PROCESS, NULL, NULL, &s, &p))
{
system_error("Spawning program");
return 1;
}
SetConsoleCtrlHandler(die, TRUE);
WaitForSingleObject(p.hProcess, INFINITE);
return 0;
}