majesticdawn
March 8th, 2008, 08:32 PM
I have been trying for HOURS to get my program to launch an external program and then wait until it closes before it launches the next external program. Here's my code. Please point out my error if you can find it. Thanks for trying.
void exeCommand(char* command){
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
DWORD WINAPI StartThread1(LPVOID iValue){
exeCommand("notepad.exe \"c:\\test.txt\"");
return(0);
}
DWORD WINAPI StartThread2(LPVOID iValue){
exeCommand("calc.exe");
return(0);
}
HANDLE hThread1;
DWORD dwGenericThread;
char lszThreadParam[3];
strcpy(lszThreadParam,"3");
hThread1 = CreateThread(NULL,0,StartThread1,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,20000);
hThread1 = CreateThread(NULL,0,StartThread2,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,INFINITE);
void exeCommand(char* command){
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
command, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
DWORD WINAPI StartThread1(LPVOID iValue){
exeCommand("notepad.exe \"c:\\test.txt\"");
return(0);
}
DWORD WINAPI StartThread2(LPVOID iValue){
exeCommand("calc.exe");
return(0);
}
HANDLE hThread1;
DWORD dwGenericThread;
char lszThreadParam[3];
strcpy(lszThreadParam,"3");
hThread1 = CreateThread(NULL,0,StartThread1,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,20000);
hThread1 = CreateThread(NULL,0,StartThread2,&lszThreadParam,0,&dwGenericThread);
if(hThread1 == NULL){
DWORD dwError = GetLastError();
//yeah, do something... or just set a breakpoint
}
WaitForSingleObject(hThread1,INFINITE);