Created
June 8, 2018 18:05
-
-
Save AcnodeLabs/377a52851591b67a0b00151b99abe3f0 to your computer and use it in GitHub Desktop.
Spawner of Sub Process in a Windows Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Spawner { | |
STARTUPINFOA si; | |
PROCESS_INFORMATION pri; | |
bool wasOk; | |
string _name; | |
public: | |
Spawner(string name) { _name = name; wasOk = FALSE; } | |
void Activate(bool status) { | |
if (status == TRUE) { | |
ZeroMemory(&si, sizeof(si)); | |
si.cb = sizeof(si); | |
ZeroMemory(&pri, sizeof(pri)); | |
// Start the child process. | |
if (!CreateProcessA(NULL, // No module name (use command line) | |
(LPSTR)_name.c_str(), // 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 | |
&pri) // Pointer to PROCESS_INFORMATION structure | |
) | |
{ | |
MessageBoxA(NULL, "Spawner Error", _name.c_str(), MB_ICONINFORMATION); | |
return; | |
} | |
wasOk = TRUE; | |
return; | |
} | |
if (status == FALSE) { | |
// Close process and thread handles. | |
if (wasOk) { | |
TerminateProcess(pri.hProcess, 1); | |
} | |
CloseHandle(pri.hProcess); | |
CloseHandle(pri.hThread); | |
return; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example:
Spawner* EXE1 = new Spawner(string("gnatsd.exe")); //allocate
EXE1->Activate(TRUE); //Launch
EXE1->Activate(FALSE); //Terminate
delete EXE1; //de-allocate