发布时间 : 星期六 文章第4章钩子函数和窗口子类化更新完毕开始阅读a478eed3240c844769eaee1b
{
caseWM_DESTROY: {
PostQuitMessage(0); break; }
caseWM_APP+1024: {
if(w_param==125) {
SendKeys(\SendReturnKey(2); SendKeys(\SendReturnKey(3); SendKeys(\SendReturnKey(2); SendKeys(\SendReturnKey(10); SendKeys(\SendReturnKey(2); SendKeys(\}
if(w_param==126) {
SendKeys(\}
break; }
caseWM_COMMAND: {
if(w_param==BN_CLICKED) {
if(l_param==(long)h_wnd5) {
WinExec(\
hHookedWindow=FindWindow(\未定标题-记事本\if(hHookedWindow==NULL) {
MessageBox(0,\
Notepad.\\r\\nPleaseStartNotepadandtryagain\break; }
HMENUhAppMenu;
hAppMenu=GetMenu(hHookedWindow);
HMENUhAppendMenu;
hAppendMenu=CreateMenu();
AppendMenu(hAppMenu,MF_STRING+MF_POPUP,(unsigned int)hAppendMenu,\
AppendMenu(hAppendMenu,MF_STRING,125,\AppendMenu(hAppendMenu,MF_STRING,126,\HWNDhMenuWnd;
hLib=LoadLibrary(\
hMenuWnd=GetWindow(hHookedWindow,5); DWORDhThread;
hThread=GetWindowThreadProcessId(hHookedWindow,NULL); SetHandle=(sthndl)GetProcAddress(hLib,\UnSubClass=(unsub)GetProcAddress(hLib,\SetHandle(hHookedWindow,hwnd);
FillHandleArray=(filhndl)GetProcAddress(hLib, \
FillHandleArray(hHookedWindow,1); FillHandleArray(hMenuWnd,1);
ShowWindow(hHookedWindow,SW_MINIMIZE); }
if(l_param==(long)h_wnd6) {
UnSubClass(); FreeLibrary(hLib); } }
break; } }
returnDefWindowProc(hwnd,msg2,w_param,l_param); }
钩子实现的代码如下: #include
LRESULTCALLBACKWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAM lParam);//窗口回调过程 #pragmadata_seg(\HWNDhTarget=NULL; HWNDhApp=NULL; intnum=0;
booldone=FALSE;
HINSTANCEhInstance=NULL; #pragmadata_seg()
#pragmabss_seg(\HWNDhndll[100]; intform[100];
longOldWndHndl[100]; BOOLblnsubclassed[100]; HHOOKhWinHook; #pragmabss_seg()
BOOLWINAPIDllMain(HANDLEhinstDLL,DWORDfdwReason,LPVOID lpvReserved) {
switch(fdwReason) {
caseDLL_PROCESS_ATTACH:
hInstance=(HINSTANCE)hinstDLL; break;
caseDLL_PROCESS_DETACH: break; }
returnTRUE; }
intWINAPIFillHandleArray(HWNDhwndSubclass,intintFrmNUm) {
hndll[num]=hwndSubclass;//填充数组 form[num]=intFrmNUm;
blnsubclassed[num]=FALSE;//设置子类化状态 num=num+1; return1; }
//取消子类化过程 intWINAPIUnSubclass() {
intcount;
for(count=0;count if((int)hndll[count]>1) { SetWindowLong(hndll[count],GWL_WNDPROC,OldWndHndl[count]);} } return1; } LRESULTCALLBACKCBTProc(intnCode,WPARAMwParam,LPARAMlParam) { if(nCode==HCBT_ACTIVATE) { if((HWND)(wParam)==hTarget) { intcount; for(count=0;count if(blnsubclassed[count]==FALSE) { if(((int)hndll[count])>1) { OldWndHndl[count]=SetWindowLong(hndll[count],GWL_WNDPROC, (long)WindowProc); } blnsubclassed[count]=TRUE; } } } } if(nCode==HCBT_DESTROYWND) { if((HWND)wParam==hTarget) SendNotifyMessage(hApp,WM_APP+1024,(WPARAM)wParam,(LPARAM)lParam); } returnCallNextHookEx(NULL,nCode,wParam,lParam); } intWINAPISetHandle(HWNDHandleofTarget,HWNDHandleofApp) { hTarget=HandleofTarget; hApp=HandleofApp; hWinHook=SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTProc,hInstance, GetWindowThreadProcessId(hTarget,NULL)); return1; } LRESULTCALLBACKWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam, LPARAMlParam) { longval; intcount; for(count=0;count if(HIWORD(wParam)==0) result=SendNotifyMessage(hApp,WM_APP +1024,(WPARAM)(LOWORD(wParam)),(LPARAM)uMsg); return