Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
Process.cpp
[詳解]
1// ----------------------------------------------------------------------------
6
7#include "pch.h"
8#include "Process.h"
9
10using namespace alt;
11
13{
14 ZeroMemory (&_pi, sizeof (_pi));
15 ZeroMemory (&_si, sizeof (_si));
16 _si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
17 _si.wShowWindow = SW_HIDE;
18 _dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT;
19}
20
22{
23 ::CloseHandle (_pi.hThread);
24 ::CloseHandle (_pi.hProcess);
25}
26
28 LPCTSTR lpctszApplicationName,
29 LPTSTR lptszCommandLine,
30 LPCTSTR lpctszCurrentDirectory)
31{
32 BOOL ret = FALSE;
33
34 _si.cb = sizeof (STARTUPINFO);
35 _si.lpReserved = NULL;
36 _si.cbReserved2 = 0;
37 _si.lpReserved2 = NULL;
38
39 do
40 {
41 ret = ConnectHandles ();
42 if (!ret) break;
43
44 ret = ::CreateProcess (
45 lpctszApplicationName,
46 lptszCommandLine,
47 NULL,
48 NULL,
49 TRUE,
51 NULL,
52 lpctszCurrentDirectory,
53 &_si,
54 &_pi);
55 if (!ret) break;
56
57 ret = TRUE;
58 } while (false);
59
60 return ret;
61}
62
63DWORD Process::Wait (DWORD dwTime) const
64{
65 return ::WaitForSingleObject (_pi.hProcess, dwTime);
66}
67
69{
70 // TODO:CreateProcessAsUser()
71 return FALSE;
72}
73
75{
76 // TODO:CreateProcessWithLogon()
77 return FALSE;
78}
79
81{
82 DWORD dwRet;
83 BOOL ret = ::GetExitCodeProcess (_pi.hProcess, &dwRet);
84
85 return ret == TRUE ? dwRet : -1;
86}
87
88BOOL Process::Terminate () const
89{
90 return ::TerminateProcess (_pi.hProcess, 0);
91}
92
94 DWORD dwX, DWORD dwY, DWORD dwWidth, DWORD dwHeight, WORD wShowWindow)
95{
96 _si.dwX = dwX;
97 _si.dwY = dwY;
98 _si.dwXSize = dwWidth;
99 _si.dwYSize = dwHeight;
100 _si.dwFlags = STARTF_USEPOSITION;
101 _si.dwFlags |= STARTF_USESIZE;
102 _si.dwFlags |= STARTF_USESHOWWINDOW;
103 _si.wShowWindow = wShowWindow;
104}
105
107 DWORD dwX, DWORD dwY, DWORD dwWidth, DWORD dwHeight, DWORD dwXChars,
108 DWORD dwYChars)
109{
110 _si.dwX = dwX;
111 _si.dwY = dwY;
112 _si.dwXSize = dwWidth;
113 _si.dwYSize = dwHeight;
114 _si.dwXCountChars = dwXChars;
115 _si.dwYCountChars = dwYChars;
116 _si.dwFlags = STARTF_USECOUNTCHARS;
117 _si.dwFlags |= STARTF_USESTDHANDLES;
118 _si.dwFlags |= STARTF_FORCEONFEEDBACK;
119 _dwCreationFlags |= CREATE_NEW_CONSOLE;
120}
121
122VOID Process::SetTitle (LPTSTR lptszConsoleTitle)
123{
124 _si.lpTitle = lptszConsoleTitle;
125}
126
127BOOL Process::SetPriority (Priority priority) const
128{
129 return ::SetPriorityClass (GetProcessHandle (), (DWORD)priority);
130}
131
132DWORD Process::Peek (LPVOID lpvBuf, DWORD dwSize) const
133{
134 return _pipeRead.Peek (lpvBuf, dwSize);
135}
136
137DWORD Process::Read (LPVOID lpvBuf, DWORD dwSize) const
138{
139 return _pipeRead.Read (lpvBuf, dwSize);
140}
141
142DWORD Process::Write (LPVOID lpvBuf, DWORD dwSize) const
143{
144 return _pipeWrite.Write (lpvBuf, dwSize);
145}
146
147// private functions --------------------------------------------------
148
150{
151 BOOL ret = FALSE;
152
153 do
154 {
155 ret = _pipeRead.Create ();
156 if (!ret) break;
157
158 ret = _pipeWrite.Create ();
159 if (!ret) break;
160
161 ret = _pipeError.Create ();
162 if (!ret) break;
163
164 _si.hStdInput = _pipeWrite.GetReadHandle ();
165 _si.hStdOutput = _pipeRead.GetWriteHandle ();
166 _si.hStdError = _pipeRead.GetWriteHandle ();
167
168 ret = TRUE;
169 } while (false);
170
171 return ret;
172}
プロセスに関するWindowsAPIを集約したクラス
プリコンパイル済みヘッダー ファイルです。
HANDLE APIENTRY GetReadHandle() const
読み込み用ハンドルの取得
Definition: Pipe.h:62
DWORD APIENTRY Peek(LPVOID lpvBuf, DWORD dwSize) const
メッセージの確認
Definition: Pipe.cpp:33
HANDLE APIENTRY GetWriteHandle() const
書き込み用ハンドルの取得
Definition: Pipe.h:66
DWORD APIENTRY Read(LPVOID lpvBuf, DWORD dwSize) const
メッセージの読み込み
Definition: Pipe.cpp:45
BOOL APIENTRY Create()
パイプの作成
Definition: Pipe.cpp:23
BOOL APIENTRY Write(LPVOID lpvBuf, DWORD dwSize) const
メッセージの書き込み
Definition: Pipe.cpp:54
BOOL APIENTRY CreateAsUser()
Definition: Process.cpp:68
STARTUPINFO _si
Definition: Process.h:151
BOOL APIENTRY CreateWithLogon()
Definition: Process.cpp:74
BOOL APIENTRY Create(LPCTSTR lpctszApplicationName, LPTSTR lptszCommandLine, LPCTSTR lpctszCurrentDirectory=NULL)
プロセスの作成
Definition: Process.cpp:27
HANDLE APIENTRY GetProcessHandle() const
プロセスハンドルの取得
Definition: Process.h:108
VOID APIENTRY SetConsoleInfo(DWORD dwX, DWORD dwY, DWORD dwWidth, DWORD dwHeight, DWORD dwXChars, DWORD dwYChars)
起動するプロセスのコンソール位置を設定
Definition: Process.cpp:106
DWORD APIENTRY Peek(LPVOID lpvBuf, DWORD dwSize) const
プロセス出力内容の読み込み
Definition: Process.cpp:132
DWORD APIENTRY Write(LPVOID lpvBuf, DWORD dwSize) const
プロセス入力への書き込み
Definition: Process.cpp:142
BOOL APIENTRY Terminate() const
プロセスの強制終了
Definition: Process.cpp:88
APIENTRY ~Process()
デストラクタ
Definition: Process.cpp:21
APIENTRY Process()
コンストラクタ
Definition: Process.cpp:12
VOID APIENTRY SetTitle(LPTSTR lptszConsoleTitle)
コンソールタイトルの設定
Definition: Process.cpp:122
BOOL APIENTRY SetPriority(Priority priority) const
優先度の設定
Definition: Process.cpp:127
PROCESS_INFORMATION _pi
Definition: Process.h:150
BOOL ConnectHandles()
Definition: Process.cpp:149
DWORD APIENTRY Read(LPVOID lpvBuf, DWORD dwSize) const
プロセス出力内容の読み込み
Definition: Process.cpp:137
Pipe _pipeError
Definition: Process.h:154
Pipe _pipeRead
Definition: Process.h:152
DWORD APIENTRY Wait(DWORD dwTime=INFINITE) const
プロセス終了の待機
Definition: Process.cpp:63
VOID APIENTRY SetWindowInfo(DWORD dwX, DWORD dwY, DWORD dwWidth, DWORD dwHeight, WORD wShowWindow=SW_NORMAL)
起動するプロセスのウィンドウ位置を設定
Definition: Process.cpp:93
DWORD _dwCreationFlags
Definition: Process.h:155
DWORD APIENTRY GetExitCode() const
プロセス終了時の終了コード取得
Definition: Process.cpp:80
Pipe _pipeWrite
Definition: Process.h:153
Definition: DBLibrary.h:12
Priority
プロセスの優先度設定値
Definition: Process.h:17