12#ifdef __TNBPROCESSHANDLE_H__
13 #ifndef _TnbPROCESS_PipeInOut_ENABLE
14 #error Please define _TnbPROCESS_PipeInOut_ENABLE first rather than you include TnbProcessHandle.h.
16 #ifndef _TnbPROCESS_EnumProcesses_ENABLE
17 #error Please define _TnbPROCESS_EnumProcesses_ENABLE first rather than you include TnbProcessHandle.h.
22 #define _TnbPROCESS_PipeInOut_ENABLE
23 #define _TnbPROCESS_EnumProcesses_ENABLE
52 CConsoleHooker(
void) : m_processId(0), m_isHooked(false), m_hStdOutput(NULL), m_hStdInput(NULL)
54 m_rep.
SetKind(_T(
"ConsoleHooker"));
70 return m_isHooked || m_proc.
IsAlive();
81 return (m_processId != 0 && ::GetProcessVersion(m_processId) != 0);
102 m_processName.
Format(_T(
"%d"), processId);
103 return m_Hook(processId);
113 bool Hook(LPCTSTR lpszProcessName)
115 m_processName = lpszProcessName;
142 bool Execute(LPCTSTR lpszProcessName,
bool isShow =
false)
147 if ( m_proc.
Create(lpszProcessName, &m_pipeIn, &m_pipeOut, NULL, isShow) )
150 CStr s = lpszProcessName;
151 s = m_processName.
FindCut(
' ');
157 m_processName = (r < 0) ? s : s.
Mid(r + 1);
202 if ( ::GetProcessVersion(m_processId) == 0 )
227 if ( ! m_SendChar(s[i]) )
238 return (m_pipeIn.
Send(l, P) == l);
251 CONSOLE_SCREEN_BUFFER_INFO sb;
252 if ( ::GetConsoleScreenBufferInfo(m_hStdOutput, &sb) )
254 size_t len = sb.dwCursorPosition.Y * sb.dwSize.X + sb.dwCursorPosition.X;
256 COORD coord = { 0, 0 };
263 size_t z = min(sb.dwSize.X,
ToInt(sz));
264 ::ReadConsoleOutputCharacter(m_hStdOutput, buf,
ToDword(z), coord, &dw);
270 TRACE2(
"%s [%s]\n", m_processName, s);
274 ::FillConsoleOutputCharacter(m_hStdOutput,
' ',
ToDword(len), coord, &dw);
275 ::SetConsoleCursorPosition(m_hStdOutput, coord);
281 if ( l > 0 && l != INVALID_SIZE )
290 TRACE2(
"%s [%s]\n", m_processName,
CStr(va[i]));
313 if ( ::GetProcessVersion(m_processId) == 0 )
321 if ( vs[i].Find(a) != INVALID_INDEX )
347 if ( ::GetProcessVersion(m_processId) == 0 )
357 if ( vs[i].Find(words[j]) != INVALID_INDEX )
395 return m_lastStrings;
410 bool m_Hook(DWORD processId)
413 if ( ::AttachConsole(processId) )
415 m_hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
416 m_hStdOutput =
reinterpret_cast<HANDLE
>(7);
417 m_hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);
418 if ( m_hStdOutput != NULL && m_hStdInput != NULL )
420 m_processId = processId;
425 _GetLastError(
"AttachConsole");
429 bool m_WaitIdle(DWORD to = 500)
431 ASSERT( m_isHooked );
432 CONSOLE_SCREEN_BUFFER_INFO sb1;
433 CONSOLE_SCREEN_BUFFER_INFO sb;
434 if ( ! ::GetConsoleScreenBufferInfo(m_hStdOutput, &sb1) )
436 _GetLastError(
"GetConsoleScreenBufferInfo");
446 if ( ! ::GetConsoleScreenBufferInfo(m_hStdOutput, &sb) )
450 if ( sb1.dwCursorPosition.X != sb.dwCursorPosition.X || sb1.dwCursorPosition.Y != sb.dwCursorPosition.Y )
466 bool m_SendChar(TCHAR c)
468 ASSERT( m_isHooked );
469 INPUT_RECORD ir[2] = { 0 };
470 ir[0].EventType = KEY_EVENT;
472 ir[0].Event.KeyEvent.uChar.AsciiChar = c;
474 ir[0].Event.KeyEvent.uChar.UnicodeChar = c;
477 ir[0].Event.KeyEvent.bKeyDown =
true;
479 if ( ::WriteConsoleInput(m_hStdInput, ir, 2, &dw) )
#define loop(VAR, CNT)
loop構文.
void Unhook(void)
[設定] アンフック.
bool SendString(LPCTSTR lpsz)
[送信] 文字列送信.
void Terminate(void)
[設定] ターミネート.
bool Hook(LPCTSTR lpszProcessName)
[設定] フック.
bool Hook(DWORD processId)
[設定] フック.
bool Execute(LPCTSTR lpszProcessName, bool isShow=false)
[設定] 実行.
DWORD GetProcessId(void) const
[取得] プロセスID取得.
const CStrVector & GetLastString(void) const
[取得] 取得文字列取得.
CConsoleHooker(void)
コンストラクタ
bool WaitForExit(DWORD to)
[処理] 終了待ち.
bool IsHooked(void) const
[確認] フック済み?
~CConsoleHooker(void)
デストラクタ
CStrVector GetString(void)
[取得] コンソール文字列取得.
int WaitStringEx(LPCTSTR lpszWord1, LPCTSTR lpszWord2, DWORD to)
[取得] 指定文字列待ち.
void SetLastString(const CStrVector &vs)
[設定] 取得文字列設定.
bool IsAlived(void) const
[確認] プロセス生存確認.
bool WaitString(LPCTSTR lpsz, DWORD to)
[取得] 指定文字列待ち.
int WaitStringEx(const CStrVector &words, DWORD to)
[取得] 指定文字列待ち.
virtual bool Open(void)
[処理] オープン.
virtual void Close(void)
[処理] クローズ.
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
bool Terminate(UINT uExitCode=0)
[通知] 強制終了.
static bool EnumProcesses(CWorkMemT< DWORD > &_ids)
[取得] プロセスID一覧取得
DWORD GetProcessId(void) const
[取得] プロセスID取得
bool Create(LPCTSTR lpszExecName, LPCTSTR lpszCommand, STARTUPINFO &si)
[実行] プロセス実行
bool IsAlive(void) const
[確認] 起動確認.
void SetKind(DWORD kind)
[設定] 種類設定.
void ToViewer(size_t sizeData, LPCVOID lpcvData, DWORD dire=0)
[出力] ダンプ出力.
static CVectorT< CStrT< TYP > > SeparateLine(const TYP *lpsz)
[取得] 改行毎分離
INT_PTR ReverseFindOneOf(const TYP *lpsz) const
[確認] 検索(後ろから)
bool IsEmpty(void) const
[確認] 空チェック
size_t GetLength(void) const
[取得] 文字列長
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
CStrT & TrimRight(TYP t=' ')
[処理] 末尾から文字をトリム.
CStrT FindCut(TYP c, CStrT *_pstrRest=NULL) const
[作成] 切り分け
int Replace(TYP tOld, TYP tNew)
[処理] 文字置換.
void Format(const TYP *lpszFormat,...)
[代入] 書式付き文字列代入.
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
bool IsPassed(DWORD dwTime) const
[確認] 経過確認.
virtual bool RemoveAll(void)
[削除] 空化
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
virtual size_t Append(const IConstCollectionT< TYP > &c)
[追加] 追加.
size_t GetSize(void) const
[取得] サイズ取得
void Fill(const TYP &other)
[設定] 全情報代入.
const TYP * Ref(void) const
[取得] ポインタ取得
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
TNB::CStrT< TCHAR > CStr
文字列クラス
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).