18#define __TNBPROCESSHANDLE_H__
20#ifdef _TnbPROCESS_PipeInOut_ENABLE
23#ifdef _TnbPROCESS_EnumProcesses_ENABLE
27 #pragma comment( lib, "Psapi.lib" )
73 if ( ! m_hhProcess.
IsNull() )
75 r = !! ::TerminateProcess(m_hhProcess, uExitCode);
90 DWORD r = WAIT_ABANDONED;
91 if ( ! m_hhProcess.
IsNull() )
93 r = ::WaitForSingleObject(m_hhProcess, dwWait);
106 DWORD r = WAIT_ABANDONED;
107 if ( ! m_hhProcess.
IsNull() )
109 r = ::WaitForSingleObject(m_hhProcess, 0);
111 return r == WAIT_TIMEOUT;
119 operator HANDLE(
void)
const
163 if ( ! ::GetExitCodeProcess(m_hhProcess, &dw) )
177 void Attach(HANDLE hProcess, HANDLE hThread)
179 m_hhProcess = hProcess;
180 m_hhThread = hThread;
204 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, STARTUPINFO& si)
208 PROCESS_INFORMATION pi;
210 if ( ::CreateProcess(lpszExecName, s, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) )
212 ::WaitForInputIdle(pi.hProcess, INFINITE);
213 m_hhProcess = pi.hProcess;
214 m_hhThread = pi.hThread;
215 m_processId = pi.dwProcessId;
218 _GetLastError(
"CreateProcess");
230 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand,
bool boIsShowWindow =
true)
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);
248 bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand,
bool boDummy =
true)
252 PROCESS_INFORMATION pi;
253 if ( ::CreateProcess(lpszExecName, lpszCommand, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi) )
255 m_hhProcess = pi.hProcess;
256 m_hhThread = pi.hThread;
257 m_processId = pi.dwProcessId;
264 #ifdef _TnbPROCESS_PipeInOut_ENABLE
284 CPipe* pErrorPipe = NULL,
bool boIsShowWindow =
true)
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; }
294 memset(&si, 0,
sizeof(STARTUPINFO));
295 si.cb =
sizeof(STARTUPINFO);
296 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
299 si.hStdError = pErrorPipe->GetWriteHandle();
300 si.wShowWindow =
static_cast<WORD
>(boIsShowWindow ? SW_SHOWDEFAULT : SW_HIDE);
302 PROCESS_INFORMATION pi;
304 if ( ::CreateProcess(NULL, s, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) )
306 ::WaitForInputIdle(pi.hProcess, INFINITE);
307 m_hhProcess = pi.hProcess;
308 m_hhThread = pi.hThread;
309 m_processId = pi.dwProcessId;
312 _GetLastError(
"CreateProcess");
327 CHandleHandle hhProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, processId);
328 if ( hhProcess != INVALID_HANDLE_VALUE )
330 return !! ::TerminateProcess(hhProcess, uExitCode);
335 #ifdef _TnbPROCESS_EnumProcesses_ENABLE
347 CHandleHandle hhProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
348 if ( hhProcess != INVALID_HANDLE_VALUE )
352 if ( ::EnumProcessModules(hhProcess, &hMod,
sizeof(hMod), &cbNeeded) )
355 if ( ::GetModuleBaseName(hhProcess, hMod, s, MAX_PATH) )
358 if ( pCreateTime != NULL )
361 ::GetProcessTimes(hhProcess, pCreateTime, &ft, &ft, &ft);
379 const int MAX_CNT = 1024;
386 int len = cbNeeded /
sizeof(DWORD);
402 const int MAX_CNT = 1024;
417 w1[cnt].processId = w2[i];
418 w1[cnt].createTime = ft;
420 if ( cnt >= MAX_CNT )
427 ::qsort(w1, cnt,
sizeof(TParam), ms_CompareParam);
431 TRACE2(
"found %d, %s\n", w1[i].processId,
CSystemTime(w1[i].createTime).Format());
432 _ids[i] = w1[i].processId;
447 HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
448 PROCESSENTRY32 entry;
449 entry.dwSize =
sizeof(PROCESSENTRY32);
450 if ( ::Process32First(hSnapshot, &entry) )
454 }
while( ::Process32Next(hSnapshot, &entry) );
456 ::CloseHandle(hSnapshot);
468 static int ms_CompareParam(
const void* P1,
const void* P2)
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);
#define loop(VAR, CNT)
loop構文.
HANDLE GetReadHandle(void) const
[取得] 読み込みハンドル取得
HANDLE GetWriteHandle(void) const
[取得] 書き込みハンドル取得
virtual bool IsOpened(void) const
[確認] オープン確認.
bool IsNull(void) const
[確認] NULLチェック
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)
[取得] プロセス名取得
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
size_t GetSize(void) const
[取得] サイズ取得
void Resize(size_t l)
[設定] サイズ再設定
void Reset(size_t l, const TYP *P)
[設定] 再設定
const TYP * Ref(void) const
[取得] ポインタ取得
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)