/* * MyProces.cpp * Benjamin Bertka - 23306103 * bbertka@mss.icics.ubc.ca * * CICS505 - os6 partA */ #include "MyProcess.h" #include "windows.h" #include #include using namespace std; /* * This is the constructor for the MyProcess object * It prepares the STARTUP and PROCESS_INFO, and sets * default values to the process data * PRE: None * POST: a new MyProcess is initialized */ MyProcess::MyProcess(){ ZeroMemory( &this->si, sizeof(this->si) ); this->si.cb = sizeof(this->si); ZeroMemory( &this->pi, sizeof(this->pi) ); this->lpApplicationName = NULL; this->lpCommandLine = NULL; this->lpProcessAttributes = NULL; this->lpThreadAttributes = NULL; this->bInheritHandles = FALSE; this->dwCreationFlags = CREATE_NEW_CONSOLE; this->lpEnvironment = NULL; this->lpCurrentDirectory = NULL; this->lpStartupInfo = &si; this->lpProcessInformation = π } /* * Sets a custom lpTitle for this process * @param *in - a character sting * PRE: input string is not null * POST: lpTitle is set */ void MyProcess::setTitle(char *in){ this->si.lpTitle = LPTSTR(in); } /* * Sets the command to execute for this process * @param *in - a character string * PRE: input string is not null * POST: lpCommandLine is set to executable command */ void MyProcess::setCommand(char *in){ this->lpCommandLine = LPTSTR(in); } /* * Waits for this process to finish * PRE: None * POST: parent process is in wait state for this process to finish */ void MyProcess::wait(){ WaitForSingleObject( this->pi.hProcess, INFINITE ); } /* * Closes this process * PRE: None * POST: process is closed */ void MyProcess::close(){ CloseHandle( this->pi.hProcess ); CloseHandle( this->pi.hThread ); } /* * prints useful information */ void MyProcess::print(){ cout << "command: " << this->lpCommandLine << endl; cout << "process id: " << this->pi.dwProcessId << endl; } /* * A wrapper for the CreateProcess() function * uses This private data to execute command in lpCommandline */ bool MyProcess::create(){ return CreateProcess( this->lpApplicationName, this->lpCommandLine, this->lpProcessAttributes, this->lpThreadAttributes, this->bInheritHandles, this->dwCreationFlags, this->lpEnvironment, this->lpCurrentDirectory, this->lpStartupInfo, this->lpProcessInformation); }