這是我在網上的朋友那裡並經過修改得到的最簡單的HOOK的例子
首先看DLL檔案:
#include <windows.h>
_declspec(dllexport) void sethook();
HHOOK g_hMouse=NULL;
HHOOK g_hKeyboard = NULL;
LRESULT CALLBACK MouseProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
if ( HC_ACTION == code)
{
switch(wParam)
{
case WM_LBUTTONUP:
::MessageBox(NULL, L"fadsf", L"fasf", MB_OK);
break;
}
}
return CallNextHookEx(g_hMouse,code,wParam,lParam);
}
LRESULT CALLBACK KeyboardProc(
int code,
WPARAM wParam,
LPARAM lParam
)
{
if (HC_ACTION == code)
{
switch(wParam)
{
case VK_UP :
::MessageBox(NULL, L"gdsg", L"hfghd", MB_OK);
}
}
return CallNextHookEx(g_hMouse,code,wParam,lParam);
}
_declspec(dllexport) void sethook(DWORD dwThreadId)
{
//g_hMouse = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandle(L"SimpleDll"), dwThreadId);
g_hKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, GetModuleHandle(L"SimpleDll"), 0);
}
下面是實現檔案:
#include <windows.h>
#pragma comment(lib,"..//Debug//SimpleDll.lib")
//視窗物件的過程處理函式
_declspec(dllimport) void sethook(DWORD dwThreadId);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Windows應用程式的主函式
#define hmenu2 100
HWND Hbutton;
HINSTANCE hin;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR lpszCmdLine, int iCmdShow)
{
hin=hInstance;
//定義視窗類
TCHAR tcClassName[] = TEXT("My Window"); //視窗類名字串
WNDCLASS wc; //視窗類屬性描述結構
wc.lpszClassName = tcClassName; //視窗類名
wc.lpszMenuName = NULL; //視窗類選單資源名
wc.lpfnWndProc = WndProc; //視窗物件的過程處理函式
wc.hInstance = hInstance; //當前程序物件控制代碼
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); //視窗背景刷子物件
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //圖示物件
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //游標物件
wc.cbClsExtra = 0; //同類視窗物件公共資料區大小
wc.cbWndExtra = 0; //當前視窗物件私有資料區大小
wc.style = CS_HREDRAW | CS_VREDRAW; //視窗類風格
if (!RegisterClass(&wc)) //註冊視窗類
{
//視窗類註冊不成功的錯誤資訊
MessageBox(NULL, TEXT("RegisterClassError!"), TEXT("Error"), MB_ICONERROR);
return 0;
}
//建立視窗物件
TCHAR tcWindowCaptionName[] = TEXT("Win32 API"); //視窗物件標題名稱
CREATESTRUCT cs; //視窗物件屬性描述結構
cs.lpszClass = tcClassName; //視窗類名
cs.lpszName = tcWindowCaptionName; //視窗物件標題名稱
cs.style = WS_OVERLAPPEDWINDOW; //視窗物件風格
cs.x = 100; //視窗物件在螢幕上的x座標
cs.y = 100; //視窗物件在螢幕上的y座標
cs.cx = 400; //視窗物件的寬度
cs.cy = 300; //視窗物件的高度
cs.hwndParent = NULL; //視窗物件的父視窗控制代碼
cs.hMenu = NULL; //視窗物件的選單控制代碼或子視窗編號
cs.hInstance = hInstance;//當前程序的例項控制代碼
cs.lpCreateParams = NULL;
//建立視窗物件
HWND hWnd = CreateWindow( cs.lpszClass,
cs.lpszName,
cs.style,
cs.x,
cs.y,
cs.cx,
cs.cy,
cs.hwndParent,
cs.hMenu,
cs.hInstance,
cs.lpCreateParams);
sethook(GetCurrentThreadId()); //把當前程序ID傳給鉤子程式
Hbutton=CreateWindow(L"BUTTON",L"按鈕",WS_CHILD|WS_VISIBLE,0,0,40,20,hWnd,HMENU(hmenu2),hin,0);
if (hWnd == NULL)
{
//視窗物件建立不成功的錯誤提示
MessageBox(NULL, TEXT("CreateWindowError!"), TEXT("Error!"),MB_ICONERROR);
return 0;
}
//顯示視窗物件
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);//立即重新整理視窗物件
//訊息檢索
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
//檢索訊息;當檢索到WM_QUIT訊息時,從訊息迴圈中退出
TranslateMessage(&msg);//進行虛擬鍵盤訊息的轉換
DispatchMessage(&msg); //傳送訊息,由此作業系統呼叫相應的視窗過程處理訊息
}
//主視窗返回
return msg.wParam;
}
//視窗物件的過程處理函式
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
//訊息分類處理
HWND hw,he;
switch (iMsg)
{
//客戶區的繪製訊息
case WM_CREATE:
break;
case WM_PAINT:
{
PAINTSTRUCT ps; //繪製屬性結構描述
HDC hDC = BeginPaint(hWnd, &ps); //獲取顯示裝置物件及繪製描述屬性
RECT rect;
GetClientRect(hWnd, &rect);//獲取當前視窗物件客戶區矩形
SetBkMode(hDC,TRANSPARENT);//設定背景方式
SetTextColor(hDC, RGB(255, 0, 0));//設定文字顏色
//繪製文字
DrawText(hDC, TEXT("Hello, Win32!"), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps); //客戶區繪製結束,歸還顯示裝置物件
return 0;
}
case WM_COMMAND:
if(wParam==hmenu2)
{
hw=FindWindow(L"Shell_Traywnd",NULL);
if (hw!=0)
{ ShowWindow(hw,SW_HIDE);
//he=GetWindow(hw,GW_HWNDNEXT);//獲取子視窗控制代碼
ShowWindow(hw,SW_MAXIMIZE);
SendMessage(hw,WM_CLOSE,0,0);//傳送退出訊息
EnableWindow(hw,TRUE); //禁止
}
else
{
MessageBox(NULL,L"未找到",L"",0);
}
}
break;
case WM_DESTROY:
{
//傳送WM_QUIT訊息,通知執行緒訊息檢索迴圈,主程式可以退出
PostQuitMessage(0);
return 0;
}
}
//程式末處理的訊息交給window系統的預設視窗物件的過程處理函式處理
return DefWindowProc(hWnd, iMsg, wParam,lParam);
}