Cześć. Tak jak w temacie - nie potrafię poprawnie wyświetlić wartości które chcę dostać z GetExitCodeProcess(). Po uruchomieniu programu z argumentem 5, w ostatniej kolumnie powinny być to cyfry od 1 do 5:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
STARTUPINFO si;
PROCESS_INFORMATION pi1;
PROCESS_INFORMATION pi2;
char argline[20];
memset(&si, 0, sizeof(si));
memset(&pi1, 0, sizeof(pi1));
memset(&pi2, 0, sizeof(pi2));
unsigned int currentID = GetCurrentProcessId();
unsigned int child2ID;
unsigned int child1ID;
si.cb = sizeof(si);
int pass1 = d - 1;
int pass2 = d - 2;
sprintf(argline, "lab09.exe %d", pass1);
if (CreateProcessA(NULL, argline, NULL, NULL, 0, 0, NULL, NULL, &si, &pi1) == 0) {
fprintf(stderr, "wywolanie procesu 1 niepowiodlo sie");
}else{
child1ID = GetProcessId(pi1.hProcess);
}
sprintf(argline, "lab09.exe %d", pass2);
if (CreateProcessA(
NULL, //lpApplicationName
argline, //lpCommandLine
NULL, //lpProcessAttributes
NULL, //lpThreadAttributes
0, //bInheritHandles
0, //dwCreationFlags
NULL, //lpEnvironment
NULL, //lpCurrentDirectory
&si, //lpStartupInfo
&pi2 //lpProcessInformation
) == 0) {
fprintf(stderr, "wywolanie procesu 2 niepowiodlo sie");
}else{
child2ID = GetProcessId(pi2.hProcess);
}
DWORD exit1;
GetExitCodeProcess(pi1.hProcess, &exit1);
DWORD exit2;
GetExitCodeProcess(pi2.hProcess, &exit2);
WaitForSingleObject(pi1.hProcess, INFINITE);
CloseHandle(pi1.hProcess);
CloseHandle(pi1.hThread);
WaitForSingleObject(pi2.hProcess, INFINITE);
CloseHandle(pi2.hProcess);
CloseHandle(pi2.hThread);
fprintf(stdout, "\n%d\t%d\t%d\t%d", currentID, child1ID, pass1, exit1);
fprintf(stdout, "\n%d\t%d\t%d\t%d", currentID, child2ID, pass2, exit2);
fprintf(stdout, "\n%d\t\t\t%d\n", currentID, exit1+exit2);
exit(exit1+exit2);
}