-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcess.cpp
More file actions
64 lines (58 loc) · 1.88 KB
/
Process.cpp
File metadata and controls
64 lines (58 loc) · 1.88 KB
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
#include "Process.h"
ProcessViaCreateProcess::ProcessViaCreateProcess( std::string commandline, HANDLE inhandle, HANDLE outhandle )
{
STARTUPINFO startupInfo;
ZeroMemory( &_process, sizeof( PROCESS_INFORMATION ) );
ZeroMemory( &startupInfo, sizeof( STARTUPINFO ) );
startupInfo.cb = sizeof( STARTUPINFO );
startupInfo.hStdError = outhandle;
startupInfo.hStdOutput = outhandle;
startupInfo.hStdInput = inhandle;
startupInfo.dwFlags = STARTF_USESTDHANDLES;
BOOL bSuccess = CreateProcess( NULL,
(char*)commandline.c_str(),
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&startupInfo,
&_process );
if( !bSuccess )
{
throw std::exception( std::string("CreateProcess failed with error: " + std::to_string( (long long)GetLastError() ) ).c_str() );
}
}
void ProcessViaCreateProcess::waitUntilTerminated()
{
WaitForSingleObject( _process.hProcess, INFINITE );
}
ProcessViaCreateProcess::~ProcessViaCreateProcess()
{
CloseHandle( _process.hProcess );
CloseHandle( _process.hThread );
}
ProcessViaShellExec::ProcessViaShellExec( std::string program, std::string params )
{
ZeroMemory( &_info, sizeof( SHELLEXECUTEINFOA ) );
_info.cbSize = sizeof( SHELLEXECUTEINFOA );
_info.lpVerb = "runas";
_info.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_NOASYNC | SEE_MASK_FLAG_NO_UI;
_info.nShow = 0;
_info.lpFile = program.c_str();
_info.lpParameters = params.c_str();
BOOL success = ShellExecuteEx( &_info );
if( !success )
{
throw std::exception( std::string( "ShellExecuteEx failed with error: " + std::to_string( (long long)GetLastError() ) ).c_str() );
}
}
void ProcessViaShellExec::waitUntilTerminated()
{
WaitForSingleObject( _info.hProcess, INFINITE );
}
ProcessViaShellExec::~ProcessViaShellExec()
{
CloseHandle( _info.hProcess );
}