Reputation: 4264
if I am using CreateProcess()
multiple times, is okay to share the PROCESS_INFORMATION
and STARTUPINFO
variables? Or is it really bad practice? I've read quite a bit of documentation, but I can't find any examples about handling CreateProcess()
calls more than once.
As an example, say I have the fake function below:
int SampleClass::sampleFn1(){
//Variables
STARTUPINFOW siStartInfo;
PROCESS_INFORMATION piProcInfo;
memset(&siStartInfo, 0, sizeof(siStartInfo));
memset(&piProcInfo, 0, sizeof(piProcInfo));
siStartInfo.cb = sizeof(siStartInfo);
//let us assume cmdPath = cmd.exe directory, and cmdTxtPtr has correct text
if(!CreateProcess(cmdPath, cmdTxtPtr, NULL, NULL, false, 0,
NULL, NULL, &siStartInfo, &piProcInfo)){
return 1; //failed at step 1
}
if(!CreateProcess(cmdPath,_T("/C ant debug"),NULL,NULL,false,0,NULL,
(LPCTSTR)directory,&siStartInfo,&piProcInfo)){
return 2; //failed at debug
}
WaitForSingleObject(piProcInfo.hProcess,10000);
result = GetExitCodeProcess(piProcInfo.hProcess,&exitCode);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return 0;//finished
}
A similar function happens to work in my program, but I'd like to make it as safe as possible.
Or... Should I do something like the code below instead:
int SampleClass::sampleFn2(){
//Variables
STARTUPINFOW siStartInfo;
PROCESS_INFORMATION piProcInfo;
memset(&siStartInfo, 0, sizeof(siStartInfo));
memset(&piProcInfo, 0, sizeof(piProcInfo));
siStartInfo.cb = sizeof(siStartInfo);
//let us assume cmdPath = cmd.exe directory, and cmdTxtPtr has correct text
if(!CreateProcess(cmdPath, cmdTxtPtr, NULL, NULL, false,
0, NULL, NULL, &siStartInfo, &piProcInfo)){
return 1; //failed at update project
}
WaitForSingleObject(piProcInfo.hProcess,10000);
result = GetExitCodeProcess(piProcInfo.hProcess,&exitCode);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
memset(&siStartInfo, 0, sizeof(siStartInfo));
memset(&piProcInfo, 0, sizeof(piProcInfo));
siStartInfo.cb = sizeof(siStartInfo);
if(!CreateProcess(cmdPath,_T("/C ant debug"),NULL,NULL,
false,0,NULL,(LPCTSTR)directory,&siStartInfo,&piProcInfo)){
return 2; //failed at debug
}
WaitForSingleObject(piProcInfo.hProcess,10000);
result = GetExitCodeProcess(piProcInfo.hProcess,&exitCode);
CloseHandle(piProcInfo.hProcess);
CloseHandle(piProcInfo.hThread);
return 0;//finished
}
Or do they both being handled poorly? Thank you.
Upvotes: 0
Views: 788
Reputation: 63190
AFAIK the CreateProcess
function writes into both the STARTUPINFO and PROCESSINFO structure, so unless you don't care about any of that info, which I think you should, you could do the second example you give.
By doing doing memset with 0, you reset all data in the structs to 0.
I'm not sure that this is very good practice, but maybe someone else can give more insight.
Upvotes: 1