15,471
社区成员
发帖
与我相关
我的任务
分享
HHOOK g_ShellHook;
HHOOK g_HTarget;
void SetAPIHook()
{
g_HTarget=SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)CallProc,hInst,0);
}
//在回调函数中判断窗口标题是否是监视的程序,如果是的话下另一个钩子。
LRESULT CALLBACK CallProc(
int nCode, // hook code
WPARAM wParam, // current-process flag
LPARAM lParam // message data
)
{
CWPSTRUCT *pCwp = NULL;
TCHAR szWindowName[MAX_PATH];
pCwp = (CWPSTRUCT *)lParam;
GetWindowText(pCwp->hwnd,szWindowName,MAX_PATH);
//如果窗口标题等于监视的窗口下一个进程内局部钩子
if(lstrcmpi(szWindowName,"计算器")==0)
g_ShellHook=SetWindowsHookEx(WH_SHELL,(HOOKPROC)ShellProc,hInst,GetWindowThreadProcessId(pCwp->hwnd,0));
}
//程序退出的时候调用UnAPIHook
void UnAPIHook()
{
if(::UnhookWindowsHookEx(g_HTarget))
{
g_HTarget=NULL;
}
if(::UnhookWindowsHookEx(g_ShellHook))
{
g_ShellHook=NULL;
}
}
#include <windows.h>
#include "detours.h"
#include "Hook.h"
#pragma data_seg("MySec")
HWND g_hMain=NULL;
HHOOK g_ShellHook=NULL;
HHOOK g_HTarget=NULL;
#pragma data_seg()
#pragma comment(linker,"/section:MySec,RWS")
#pragma comment(lib,"detours.lib")
BOOL isHook=FALSE;
HINSTANCE hInst=NULL;
HWND g_hWnd=NULL;
DETOUR_TRAMPOLINE(BOOL WINAPI Real_ExtTextOut(
HDC hdc, // handle to DC
int X, // x-coordinate of reference point
int Y, // y-coordinate of reference point
UINT fuOptions, // text-output options
CONST RECT* lprc, // optional dimensions
LPCTSTR lpString, // string
UINT cbCount, // number of characters in string
CONST INT* lpDx // array of spacing values
),ExtTextOut);
LRESULT CALLBACK ShellProc(
int nCode, // hook code
WPARAM wParam, // current-process flag
LPARAM lParam // message data
)
{
HWND hWnd=HWND(wParam);
TCHAR szWindowText[MAX_PATH]={0};
GetWindowText(hWnd,szWindowText,MAX_PATH);
//新建一个窗口时
if(nCode==HSHELL_WINDOWCREATED)
{
}
//销毁一个窗口时
if(nCode==HSHELL_WINDOWDESTROYED)
{
}
return CallNextHookEx(g_ShellHook,nCode,wParam,lParam);
}
LRESULT CALLBACK CallProc(
int nCode, // hook code
WPARAM wParam, // current-process flag
LPARAM lParam // message data
)
{
CWPSTRUCT *pCwp = NULL;
TCHAR szClassName[MAX_PATH];
TCHAR szWindowName[MAX_PATH];
pCwp = (CWPSTRUCT *)lParam;
if (WM_SHOWWINDOW == pCwp->message)
{
GetClassName(pCwp->hwnd,szClassName,MAX_PATH);
GetWindowText(pCwp->hwnd,szWindowName,MAX_PATH);
if(lstrcmpi(szWindowName,"计算器")==0) //或者其它程序
{
if(!isHook)
{
g_ShellHook=SetWindowsHookEx(WH_SHELL,(HOOKPROC)ShellProc,hInst,GetWindowThreadProcessId(pCwp->hwnd,0));
isHook=TRUE;
}
}
}
return CallNextHookEx(g_HTarget,nCode,wParam,lParam);
}
BOOL SetAPIHook(HWND hWindow)
{
g_hMain=hWindow;
if(g_HTarget == NULL)
{
g_HTarget=SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)CallProc,hInst,0);
}
if(g_HTarget!=NULL)
{
return true;
}
else
{
return false;
}
}
void UnAPIHook()
{
if(::UnhookWindowsHookEx(g_ShellHook))
{
g_ShellHook=NULL;
}
if(::UnhookWindowsHookEx(g_HTarget))
{
g_HTarget=NULL;
}
}
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to the DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
)
{
hInst=hinstDLL;
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
Intercept();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
UnIntercept();
break;
}
return true;
}
BOOL WINAPI Replace_ExtTextOut(
HDC hdc, // handle to DC
int X, // x-coordinate of reference point
int Y, // y-coordinate of reference point
UINT fuOptions, // text-output options
CONST RECT* lprc, // optional dimensions
LPCTSTR lpString, // string
UINT cbCount, // number of characters in string
CONST INT* lpDx // array of spacing values
)
{
return Real_ExtTextOut(hdc,X,Y,fuOptions,lprc,lpString,cbCount,lpDx);
}
void Intercept()
{
DetourFunctionWithTrampoline((PBYTE)Real_ExtTextOut, (PBYTE)Replace_ExtTextOut);
}
void UnIntercept()
{
DetourRemove( (PBYTE)Real_ExtTextOut,(PBYTE)Replace_ExtTextOut);
}
//开始按钮
void CMainDlg::OnButton1()
{
//InjectDLL();
// TODO: Add your control notification handler code here
m_Library=LoadLibrary("MyDll.dll");
if(m_Library==NULL)
MessageBox("加载失败");
if(m_Library!=NULL)
{
typedef BOOL (*pProc)(HWND);
pProc SetAPIHook=(pProc)GetProcAddress(m_Library,"SetAPIHook");
if(SetAPIHook!=NULL)
{
if(!SetAPIHook(m_hWnd))
{
MessageBox("安装钩子失败");
return;
}
}
else
{
MessageBox("执行SetAPIHook失败");
return;
}
}
}
//停止按钮
void CMainDlg::OnButton2()
{
if(m_Library!=NULL)
{
typedef void (*pProc)();
pProc UnAPIHook=(pProc)GetProcAddress(m_Library,"UnAPIHook");
if(UnAPIHook!=NULL)
{
UnAPIHook();
}
else
{
MessageBox("执行UnAPIHook失败");
return;
}
FreeLibrary(m_Library);
}
}