93 static PFNC
Replace(LPCSTR lpszDllName, LPCSTR lpszFuncName, PFNC pFnc, HMODULE hModule = NULL)
96 bool r = a.
Hook(lpszDllName, lpszFuncName, pFnc, hModule);
97 return r ? a() : NULL;
120 bool Hook(LPCSTR lpszDllName, LPCSTR lpszFuncName, PFNC pFnc, HMODULE hModule = NULL)
126 if ( hModule == NULL )
131 m_pOrignalFnc = m_Replace(hModule, lpszDllName, lpszFuncName, pFnc);
132 if ( m_pOrignalFnc != NULL )
135 m_strDllName = lpszDllName;
136 m_strFuncName = lpszFuncName;
151 if ( m_pOrignalFnc != NULL )
153 LPCSTR lpszFuncName = m_strFuncName;
155 if ( m_Replace(m_hModule, m_strDllName, lpszFuncName, m_pOrignalFnc) == NULL )
159 m_pOrignalFnc = NULL;
160 m_strDllName.
Empty();
161 m_strFuncName.
Empty();
174 return m_pOrignalFnc;
185 LPVOID m_DosHead(IMAGE_DOS_HEADER* pDos, DWORD offset)
187 return reinterpret_cast<LPVOID
>(
reinterpret_cast<DWORD
>(pDos) + offset);
191 PFNC m_Replace(HMODULE hModule, LPCSTR lpszDllName, LPCSTR lpszFuncName, PFNC pFnc)
193 IMAGE_DOS_HEADER* pDOS =
reinterpret_cast<IMAGE_DOS_HEADER*
>(hModule);
194 ASSERT( ! ::IsBadReadPtr(pDOS,
sizeof(IMAGE_DOS_HEADER)) );
195 if ( pDOS->e_magic != IMAGE_DOS_SIGNATURE )
199 IMAGE_NT_HEADERS* pPE =
static_cast<IMAGE_NT_HEADERS*
>(m_DosHead(pDOS, pDOS->e_lfanew));
200 if ( pPE->Signature != IMAGE_NT_SIGNATURE )
204 IMAGE_DATA_DIRECTORY* pDataDir
205 =
reinterpret_cast<IMAGE_DATA_DIRECTORY*
>(pPE->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT);
206 if ( pDataDir->VirtualAddress == 0 || pDataDir->Size == 0 )
210 m_modules.
Add(hModule);
211 PFNC pResultFnc = NULL;
214 IMAGE_IMPORT_DESCRIPTOR* pImpDsc =
static_cast<IMAGE_IMPORT_DESCRIPTOR*
>(m_DosHead(pDOS, pDataDir->VirtualAddress));
215 for ( ; pImpDsc->FirstThunk != NULL; pImpDsc++ )
217 LPCSTR lpszName =
static_cast<LPCSTR
>(m_DosHead(pDOS, pImpDsc->Name));
218 TTRACE1A(
"Found DLL = [%s]\n", lpszName);
219 HMODULE h = ::GetModuleHandleA(lpszName);
222 bool isFound =
false;
225 if ( m_modules[i] == h )
233 PFNC PF = m_Replace(h, lpszDllName, lpszFuncName, pFnc);
236 if ( pResultFnc != NULL && pResultFnc != PF )
246 pImpDsc =
static_cast<IMAGE_IMPORT_DESCRIPTOR*
>(m_DosHead(pDOS, pDataDir->VirtualAddress));
247 for ( ; pImpDsc->FirstThunk != NULL; pImpDsc++ )
249 LPCSTR lpszName =
static_cast<LPCSTR
>(m_DosHead(pDOS, pImpDsc->Name));
250 TTRACE1A(
"Found DLL = [%s]\n", lpszName);
251 if ( lpszDllName != NULL && stricmp(lpszName, lpszDllName) != 0 )
257 IMAGE_THUNK_DATA* pIAT =
static_cast<IMAGE_THUNK_DATA*
>(m_DosHead(pDOS, pImpDsc->FirstThunk));
259 IMAGE_THUNK_DATA* pINT =
static_cast<IMAGE_THUNK_DATA*
>(m_DosHead(pDOS, pImpDsc->OriginalFirstThunk));
260 for ( ; pIAT->u1.Function != NULL; pIAT++, pINT++ )
262 if ( IMAGE_SNAP_BY_ORDINAL(pINT->u1.Ordinal) )
266 IMAGE_IMPORT_BY_NAME* pImpName =
static_cast<IMAGE_IMPORT_BY_NAME*
>(m_DosHead(pDOS, pINT->u1.AddressOfData));
267 LPCSTR lpszName =
reinterpret_cast<LPCSTR
>(pImpName->Name);
268 TTRACE1A(
" - Found Func = [%s]\n", lpszName);
269 if( strcmp(lpszName, lpszFuncName) == 0 )
273 ::VirtualProtect(&pIAT->u1.Function,
sizeof(DWORD), PAGE_READWRITE, &oldProt);
274 PFNC PF =
reinterpret_cast<PFNC
>(pIAT->u1.Function);
275 ASSERTLIB( PF != NULL );
276 pIAT->u1.Function =
reinterpret_cast<DWORD
>(pFnc);
278 ::VirtualProtect(&pIAT->u1.Function,
sizeof(DWORD), oldProt, &dummy);
279 if ( pResultFnc != NULL && pResultFnc != PF )
287 if ( lpszDllName != NULL )
#define loop(VAR, CNT)
loop構文.
bool Hook(LPCSTR lpszDllName, LPCSTR lpszFuncName, PFNC pFnc, HMODULE hModule=NULL)
[実行] 処理フック
bool Unhook(void)
[実行] 処理アンフック
static PFNC Replace(LPCSTR lpszDllName, LPCSTR lpszFuncName, PFNC pFnc, HMODULE hModule=NULL)
[実行] 処理変更
PFNC operator()(void)
[取得] フック前の処理アドレス
void RemoveAll(void)
[削除] 空化
size_t GetSize(void) const
[取得] サイズ取得
INDEX Add(const TYP &t)
[追加] 要素一つ追加.
bool IsWow64(void) const
[確認] 64bitチェック
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.