Files
Work/proto/hookdll.cpp
2024-08-07 09:16:27 -04:00

76 lines
1.8 KiB
C++

#include <common/string.hpp>
#include <common/stdio.hpp>
#include <common/file.hpp>
#include <common/console.hpp>
#include <thread/mutex.hpp>
#include <sgi_stl/map.h>
extern "C"
{
__declspec(dllexport) bool FAR PASCAL unHook(void);
__declspec(dllexport) bool FAR PASCAL setHook(void);
}
LRESULT CALLBACK callWndHookProc(int nCode,WPARAM wParam,LPARAM lParam);
void handleMessage(UINT message,HWND hwnd,WPARAM wParam,LPARAM lParam);
HHOOK smhHook=(HHOOK)0;
File outFile("d:\log.txt","wb");
HINSTANCE hModuleInst;
BOOL _stdcall DllMain(HINSTANCE hDLLInst,DWORD dwReason,LPVOID lpReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH :
hModuleInst=hDLLInst;
::printf("Process attach: 0x%08lx\n",hModuleInst);
break;
case DLL_PROCESS_DETACH :
::printf("Process detach\n");
break;
case DLL_THREAD_ATTACH :
::printf("Thread attach\n");
break;
case DLL_THREAD_DETACH :
::printf("Thread detach\n");
break;
default :
break;
}
return TRUE;
}
bool FAR PASCAL setHook(void)
{
smhHook=::SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)&callWndHookProc,hModuleInst,0);
return smhHook?true:false;
}
bool FAR PASCAL unHook()
{
if(!smhHook)return false;
::UnhookWindowsHookEx(smhHook);
smhHook=0;
return true;
}
LRESULT CALLBACK callWndHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
LRESULT result=0;
CWPSTRUCT *pCWPSTRUCT=(CWPSTRUCT*)lParam;
printf("callWndHookProc\n");
handleMessage(pCWPSTRUCT->message,pCWPSTRUCT->hwnd,pCWPSTRUCT->wParam,pCWPSTRUCT->lParam);
if(nCode<0)return result=::CallNextHookEx(smhHook,nCode,wParam,lParam);
return result;
}
void handleMessage(UINT message,HWND hwnd,WPARAM wParam,LPARAM lParam)
{
String outLine;
outLine.reserve(256);
::sprintf(outLine.str(),"MESSAGE:%ld HWND:0x%08lx WPARAM:%d LPARAM:%ld",message,hwnd,wParam,lParam);
outFile.writeLine(outLine);
outFile.flush();
}