-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.h
100 lines (82 loc) · 2.74 KB
/
Process.h
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
#pragma once
#ifndef _SCRIPT_PAD_PROCESS_H
#define _SCRIPT_PAD_PROCESS_H
#define BUFSIZE 4096
#include "resource.h"
#include "Pipe.h"
#include "Error.h"
#include "Stopwatch.h"
#include <Windows.h>
#include <tchar.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <functional>
namespace querier {
struct ProcessStartInfo {
std::string FileName;
std::wstring wFileName;
std::string WorkingDirectory;
std::wstring wWorkingDirectory;
std::vector<std::string> CommandArguments;
std::vector<std::wstring> vwCommandArguments;
std::string CommandLine;
std::wstring wCommandLine;
std::string Environment;
std::wstring wEnvironment;
bool RedirectStdInput = false;
bool RedirectStdOutput = false;
bool RedirectStdError = false;
bool RunInCmd = false;
};
class Process : public Pipe {
public:
ProcessStartInfo StartInfo;
bool ProcessHasExited{ 0 };
Process();
Process(std::wstring FileName);
Process(ProcessStartInfo startInfo);
virtual ~Process();
void InitProcess();
void SetFileName(std::wstring file);
void SetCommandLine(std::wstring s);
void AddCommandLineArgument(std::wstring key, std::wstring value);
bool Start();
void WaitForExit();
void Write(std::string data);
bool Read(std::wstring* ret);
bool Read(std::string* ret);
double get_ElapsedTimeInMilliseconds();
double get_ElapsedTimeInSeconds();
static Process* Run(const ProcessStartInfo& startInfo);
std::function<void()> OnProcessExited{ 0 };
private:
Stopwatch stopWatch;
SECURITY_ATTRIBUTES m_secAttr;
PROCESS_INFORMATION m_procInfo;
STARTUPINFO m_startupInfo;
STARTUPINFOA m_startupInfoA;
bool m_bSuccess = false,
m_hasRedirectedIO = false,
m_isRunning = false,
m_isMembersInitialized = false,
m_isPipeInitialized = false,
m_isExecInfoInitialized = false;
static std::vector<Process*> _processes;
static void CALLBACK OnWaitCallback(void* ctx, BOOLEAN timerOrWait);
void InitMembers();
void InitPipe();
bool RegisterWaitCallback();
bool _BeforeCreateProcess(bool);
bool _CreateProcess(const char*, char*, const char*, const char*);
bool _CreateProcess(const wchar_t*, wchar_t*, const wchar_t*, const wchar_t*);
void CreateProcessSuccess();
struct WAIT_CONTEXT {
HANDLE hEv;
HANDLE hProc;
Process* proc;
Stopwatch sw;
};
};
} //namespace scriptpad
#endif //_SCRIPT_PAD_PROCESS_H