1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <string>
using namespace std;
#include <process.h>
#include <Windows.h>
/** System command launches via a command shell. */
void launch_using_system() {
cout << "system() launching" << endl;
//system( "dir" );
system("..\\Debug\\launchtarget 3");
cout << "system() landed" << endl;
}
/** exec() replaces the current process with the new process. */
void launch_using_exec() {
cout << "exec() launching" << endl;
intptr_t result = _execl("c:\\windows\\notepad.exe", "c:\\windows\\notepad.exe", NULL);
if (result == -1)
cout << "exec() failed" << endl;
else
cout << "exec() landing" << endl;
}
/** spawn() launches the new process synchronously with _P_WAIT, assynchronously with _P_NOWAIT */
void launch_using_spawn() {
cout << "spawn() launching" << endl;
intptr_t result = _spawnl(_P_WAIT, "..\\Debug\\launchtarget", "..\\Debug\\launchtarget", "4", NULL);
cout << "Result = " << result << endl;
cout << "spawn() landing" << endl;
}
/** CreateProcess allows full access to the Win32 OS. */
void launch_using_create_process() {
wstring application = L"\"C:/windows/notepad\"";
wstring params = L"C:/setup.log";
wstring command = application + L" " + params;
cout << "CreateProcess() launching" << endl;
STARTUPINFO sinfo = { 0 };
sinfo.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi = { 0 };
unsigned long const CP_MAX_COMMANDLINE = 32768;
try {
wchar_t* commandline = new wchar_t[CP_MAX_COMMANDLINE];
wcsncpy_s(commandline, CP_MAX_COMMANDLINE, command.c_str(), command.size() + 1);
CreateProcess(NULL, // application name is null since provided in commandline
commandline, // contains application name + params
NULL, // same security as parent
NULL,
false,
0,
NULL,
NULL, // same current directory as parent
&sinfo, // startup options
&pi // process information
);
delete[] commandline;
}
catch (std::bad_alloc&) {
wcerr << L"Insufficient memory to launch application" << endl;
return;
}
cout << "Terminate application? (y/n) ";
char ch;
cin >> ch;
DWORD exitCode = 0;
if (ch == 'y')
::TerminateProcess(pi.hProcess, exitCode);
else
{
if (WAIT_FAILED == WaitForSingleObject(pi.hProcess, INFINITE))
cerr << "Failure waiting for process to terminate" << endl;
GetExitCodeProcess(pi.hProcess, &exitCode);
}
cout << "Process terminated with exit code = " << exitCode << endl;
// output times
FILETIME creationTime, exitTime, kernelTime, userTime;
GetProcessTimes(pi.hProcess, &creationTime, &exitTime, &kernelTime, &userTime);
SYSTEMTIME kTime;
::FileTimeToSystemTime(&kernelTime, &kTime);
cout << "Kernel Time = " << kTime.wHour << ":" << kTime.wMinute << ":" << kTime.wSecond << "." << kTime.wMilliseconds << endl;
unsigned long long elapsedTicks = *reinterpret_cast<unsigned long long*>(&exitTime) -
*reinterpret_cast<unsigned long long*>(&creationTime);
cout << "Elapsed Ticks = " << elapsedTicks << endl;
cout << "Elapsed Time = " << elapsedTicks / 10000000.0 << endl;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
cout << "CreateProcess() landing" << endl;
}
int main()
{
//launch_using_system();
//launch_using_exec();
//launch_using_spawn();
launch_using_create_process();
}
|