Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AcnodeLabs/377a52851591b67a0b00151b99abe3f0 to your computer and use it in GitHub Desktop.
Save AcnodeLabs/377a52851591b67a0b00151b99abe3f0 to your computer and use it in GitHub Desktop.
Spawner of Sub Process in a Windows Application
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;
}
}
};
@AcnodeLabs
Copy link
Author

Usage Example:

Spawner* EXE1 = new Spawner(string("gnatsd.exe")); //allocate
EXE1->Activate(TRUE); //Launch
EXE1->Activate(FALSE); //Terminate
delete EXE1; //de-allocate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment