TNB Library
TnbProcessHandle.h
[詳解]
1#pragma once
18#define __TNBPROCESSHANDLE_H__
19#include "TnbHandleHandle.h"
20#ifdef _TnbPROCESS_PipeInOut_ENABLE
21 #include "TnbPipe.h"
22#endif
23#ifdef _TnbPROCESS_EnumProcesses_ENABLE
24 #include "TnbVector.h"
25 #include <Tlhelp32.h>
26 #include <Psapi.h>
27 #pragma comment( lib, "Psapi.lib" )
28#endif
29#ifdef _DEBUG
30 #include <TnbTime.h>
31#endif
32
33
34
35//TNB Library
36namespace TNB
37{
38
39
40
55{
56public:
57
60 {
61 }
62
70 bool Terminate(UINT uExitCode = 0)
71 {
72 bool r = false;
73 if ( ! m_hhProcess.IsNull() )
74 {
75 r = !! ::TerminateProcess(m_hhProcess, uExitCode);
76 }
77 return r;
78 }
79
88 DWORD WaitForExit(DWORD dwWait = INFINITE)
89 {
90 DWORD r = WAIT_ABANDONED;
91 if ( ! m_hhProcess.IsNull() )
92 {
93 r = ::WaitForSingleObject(m_hhProcess, dwWait); //プロセス待機
94 }
95 return r;
96 }
97
104 bool IsAlive(void) const
105 {
106 DWORD r = WAIT_ABANDONED;
107 if ( ! m_hhProcess.IsNull() )
108 {
109 r = ::WaitForSingleObject(m_hhProcess, 0);
110 }
111 return r == WAIT_TIMEOUT;
112 }
113
119 operator HANDLE(void) const
120 {
121 return m_hhProcess;
122 }
123
129 HANDLE GetProcessHandle(void) const
130 {
131 return m_hhProcess;
132 }
133
139 HANDLE GetThreadHandle(void) const
140 {
141 return m_hhThread;
142 }
143
150 DWORD GetProcessId(void) const
151 {
152 return m_processId;
153 }
154
160 LONGLONG GetExitCode(void) const
161 {
162 DWORD dw;
163 if ( ! ::GetExitCodeProcess(m_hhProcess, &dw) )
164 {
165 return -1;
166 }
167 return dw;
168 }
169
177 void Attach(HANDLE hProcess, HANDLE hThread)
178 {
179 m_hhProcess = hProcess;
180 m_hhThread = hThread;
181 m_processId = 0;
182 }
183
188 void Detach(void)
189 {
190 m_hhProcess.Null();
191 m_hhThread.Null();
192 m_processId = 0;
193 }
194
195#ifndef _WIN32_WCE
204 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, STARTUPINFO& si)
205 {
206 Detach();
207 //
208 PROCESS_INFORMATION pi;
209 CSimpleStr s = lpszCommand;
210 if ( ::CreateProcess(lpszExecName, s, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) )
211 {
212 ::WaitForInputIdle(pi.hProcess, INFINITE);
213 m_hhProcess = pi.hProcess;
214 m_hhThread = pi.hThread;
215 m_processId = pi.dwProcessId;
216 return true;
217 }
218 _GetLastError("CreateProcess");
219 return false;
220 }
221
230 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, bool boIsShowWindow = true)
231 {
232 STARTUPINFO si;
233 memset(&si, 0, sizeof(STARTUPINFO));
234 si.cb = sizeof(STARTUPINFO);
235 si.dwFlags = STARTF_USESHOWWINDOW;
236 si.wShowWindow = static_cast<WORD>(boIsShowWindow ? SW_SHOWDEFAULT : SW_HIDE);
237 return Create(lpszExecName, lpszCommand, si);
238 }
239#else
248 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, bool boDummy = true)
249 {
250 Detach();
251 //
252 PROCESS_INFORMATION pi;
253 if ( ::CreateProcess(lpszExecName, lpszCommand, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi) )
254 {
255 m_hhProcess = pi.hProcess;
256 m_hhThread = pi.hThread;
257 m_processId = pi.dwProcessId;
258 return true;
259 }
260 return false;
261 }
262#endif // _WIN32_WCE
263
264 #ifdef _TnbPROCESS_PipeInOut_ENABLE
283 bool Create(LPCTSTR lpszCommand, CPipe* pInputPipe, CPipe* pOutputPipe,
284 CPipe* pErrorPipe = NULL, bool boIsShowWindow = true)
285 {
286 Detach();
287 //
288 if ( pErrorPipe == NULL ) { pErrorPipe = pOutputPipe; }
289 if ( pInputPipe == NULL || ! pInputPipe->IsOpened() ) { return false; }
290 if ( pOutputPipe == NULL || ! pOutputPipe->IsOpened() ) { return false; }
291 if ( ! pErrorPipe->IsOpened() ) { return false; }
292 //
293 STARTUPINFO si;
294 memset(&si, 0, sizeof(STARTUPINFO));
295 si.cb = sizeof(STARTUPINFO);
296 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
297 si.hStdInput = pInputPipe->GetReadHandle();
298 si.hStdOutput = pOutputPipe->GetWriteHandle();
299 si.hStdError = pErrorPipe->GetWriteHandle();
300 si.wShowWindow = static_cast<WORD>(boIsShowWindow ? SW_SHOWDEFAULT : SW_HIDE);
301 //
302 PROCESS_INFORMATION pi;
303 CSimpleStr s = lpszCommand;
304 if ( ::CreateProcess(NULL, s, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) )
305 {
306 ::WaitForInputIdle(pi.hProcess, INFINITE);
307 m_hhProcess = pi.hProcess;
308 m_hhThread = pi.hThread;
309 m_processId = pi.dwProcessId;
310 return true;
311 }
312 _GetLastError("CreateProcess");
313 return false;
314 }
315 #endif
316
325 static bool TerminateProcessId(DWORD processId, UINT uExitCode = 0)
326 {
327 CHandleHandle hhProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, processId);
328 if ( hhProcess != INVALID_HANDLE_VALUE )
329 {
330 return !! ::TerminateProcess(hhProcess, uExitCode);
331 }
332 return false;
333 }
334
335 #ifdef _TnbPROCESS_EnumProcesses_ENABLE
345 static bool GetProcessName(CSimpleStr& _name, DWORD processId, ::FILETIME* pCreateTime = NULL)
346 {
347 CHandleHandle hhProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
348 if ( hhProcess != INVALID_HANDLE_VALUE )
349 {
350 HMODULE hMod;
351 DWORD cbNeeded;
352 if ( ::EnumProcessModules(hhProcess, &hMod, sizeof(hMod), &cbNeeded) )
353 {
354 CWorkMemT<TCHAR> s(MAX_PATH);
355 if ( ::GetModuleBaseName(hhProcess, hMod, s, MAX_PATH) )
356 {
357 _name = s;
358 if ( pCreateTime != NULL )
359 {
360 ::FILETIME ft;
361 ::GetProcessTimes(hhProcess, pCreateTime, &ft, &ft, &ft);
362 }
363 return true;
364 }
365 }
366 }
367 return false;
368 }
369
378 {
379 const int MAX_CNT = 1024;
380 CWorkMemT<DWORD> procs(MAX_CNT);
381 DWORD cbNeeded = 0;
382 if ( ! ::EnumProcesses(procs.Ref(), MAX_CNT * sizeof(DWORD), &cbNeeded) )
383 {
384 return false;
385 }
386 int len = cbNeeded / sizeof(DWORD);
387 _ids.Reset(len, procs.Ref());
388 return true;
389 }
390
400 static bool EnumProcesses(CWorkMemT<DWORD>& _ids, LPCTSTR lpszProcessName)
401 {
402 const int MAX_CNT = 1024;
403 INDEX cnt = 0;
404 ::FILETIME ft;
406 w1.Resize(MAX_CNT);
408 if ( EnumProcesses(w2) )
409 {
410 CSimpleStr s;
411 loop ( i, w2.GetSize() )
412 {
413 if ( GetProcessName(s, w2[i], &ft) )
414 {
415 if ( STRLIB::Compare(s, lpszProcessName, -1, NORM_IGNORECASE) == 0 )
416 {
417 w1[cnt].processId = w2[i];
418 w1[cnt].createTime = ft;
419 cnt++;
420 if ( cnt >= MAX_CNT )
421 {
422 break;
423 }
424 }
425 }
426 }
427 ::qsort(w1, cnt, sizeof(TParam), ms_CompareParam);
428 _ids.Resize(cnt);
429 loop ( i, cnt )
430 {
431 TRACE2("found %d, %s\n", w1[i].processId, CSystemTime(w1[i].createTime).Format());
432 _ids[i] = w1[i].processId;
433 }
434 return true;
435 }
436 return false;
437 }
438
445 {
447 HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
448 PROCESSENTRY32 entry;
449 entry.dwSize = sizeof(PROCESSENTRY32);
450 if ( ::Process32First(hSnapshot, &entry) )
451 {
452 do {
453 v.Add(entry);
454 } while( ::Process32Next(hSnapshot, &entry) );
455 }
456 ::CloseHandle(hSnapshot);
457 return v;
458 }
459
460private:
462 struct TParam
463 {
464 DWORD processId;
465 ::FILETIME createTime;
466 };
468 static int ms_CompareParam(const void* P1, const void* P2)
469 {
470 const TParam* Q1 = static_cast<const TParam*>(P1);
471 const TParam* Q2 = static_cast<const TParam*>(P2);
472 return CompareFileTime(&Q2->createTime, &Q1->createTime);
473 }
474 #endif // _TnbPROCESS_EnumProcesses_ENABLE
475
476private:
477 CHandleHandle m_hhProcess;
478 CHandleHandle m_hhThread;
479 DWORD m_processId;
480};
481
482
483
484}; //TNB
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
ハンドルハンドル関係のヘッダ
パイプ関係のヘッダ
時間管理関係のヘッダ
配列型情報管理関係のヘッダ
HANDLE型ハンドルハンドル
パイプアクセスクラス
Definition: TnbPipe.h:38
HANDLE GetReadHandle(void) const
[取得] 読み込みハンドル取得
Definition: TnbPipe.h:271
HANDLE GetWriteHandle(void) const
[取得] 書き込みハンドル取得
Definition: TnbPipe.h:280
virtual bool IsOpened(void) const
[確認] オープン確認.
Definition: TnbPipe.h:162
bool IsNull(void) const
[確認] NULLチェック
void Null(void)
[設定] 開放.
プロセスハンドル管理クラス
bool Terminate(UINT uExitCode=0)
[通知] 強制終了.
static bool EnumProcesses(CWorkMemT< DWORD > &_ids)
[取得] プロセスID一覧取得
LONGLONG GetExitCode(void) const
[取得] プロセス終了コード取得
bool Create(LPCTSTR lpszCommand, CPipe *pInputPipe, CPipe *pOutputPipe, CPipe *pErrorPipe=NULL, bool boIsShowWindow=true)
[実行] パイプ指定プロセス実行.
static bool TerminateProcessId(DWORD processId, UINT uExitCode=0)
[処理] プロセス終了.
DWORD GetProcessId(void) const
[取得] プロセスID取得
static CVectorT< PROCESSENTRY32 > EnumProcessesEntry(void)
[取得] プロセス情報一覧取得
bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, STARTUPINFO &si)
[実行] プロセス実行
bool IsAlive(void) const
[確認] 起動確認.
static bool EnumProcesses(CWorkMemT< DWORD > &_ids, LPCTSTR lpszProcessName)
[取得] プロセスID一覧取得 指定のプロセス名のプロセスIDを起動が新しい順に一覧します。
void Detach(void)
[設定] デタッチ.
DWORD WaitForExit(DWORD dwWait=INFINITE)
[処理] 終了待ち.
void Attach(HANDLE hProcess, HANDLE hThread)
[設定] アタッチ.
CProcessHandle(void)
コンストラクタ
HANDLE GetProcessHandle(void) const
[取得] プロセスハンドル取得
bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, bool boIsShowWindow=true)
[実行] プロセス実行
HANDLE GetThreadHandle(void) const
[取得] スレッドハンドル取得
static bool GetProcessName(CSimpleStr &_name, DWORD processId, ::FILETIME *pCreateTime=NULL)
[取得] プロセス名取得
簡易文字列管理クラス.
Definition: TnbDef.h:772
システムタイム管理クラス
Definition: TnbTime.h:294
配列型情報管理テンプレート
Definition: TnbVector.h:75
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
void Reset(size_t l, const TYP *P)
[設定] 再設定
Definition: TnbDef.h:690
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
Definition: TnbStrLib.h:135
TNB Library
Definition: TnbDoxyTitle.txt:2
ファイルタイム型.
Definition: TnbTime.h:893