Hook.cpp
// HOOK.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "hook.h"
#include <stdio.h>
HINSTANCE g_hInst;
const int KeyPressMask=0x80000000; //键盘掩码常量
char g_PrvChar;
#pragma data_seg ("shared")
static HHOOK g_hHook=NULL;
#pragma data_seg ()
LRESULT CALLBACK TimeChangeProc(int iCode,WPARAM wParam,LPARAM lParam);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_hInst = HINSTANCE(hModule);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}
DLLEXPORT int CALLBACK InstallHOOK()
{
g_hHook=SetWindowsHookEx(WH_SYSMSGFILTER,TimeChangeProc,g_hInst,0);
if (g_hHook)
return TRUE;
else
return FALSE;
}
DLLEXPORT int CALLBACK UninstallHOOK()
{
if (UnhookWindowsHookEx(g_hHook)==0)
return FALSE;
else
return TRUE;
}
LRESULT CALLBACK TimeChangeProc(int iCode,WPARAM wParam,LPARAM lParam)
{
FILE * fp;
fp=fopen("c://logfile.txt","a+");
if(wParam == WM_TIMECHANGE)
printf("fuck you! don't change the time!/n");
fprintf(fp,"fuck you! don't change the time!/n");
fclose(fp);
//return CallNextHookEx(g_hHook,iCode,wParam,lParam);
return 1;
}
Hook.def:
LIBRARY "hook"
EXPORTS
InstallHOOK
UninstallHOOK
Hook.h:
#ifdef __cplusplus
#define DLLEXPORT extern "C" __declspec (dllexport)
#else
#define DLLEXPORT __declspec (dllexport)
#endif
DLLEXPORT int CALLBACK InstallHOOK();
DLLEXPORT int CALLBACK UninstallHOOK();
以上为DLL准备
2 主函数
#include "stdafx.h"
//HWND g_hWnd;
#include <stdio.h>
#include <windows.h>
HINSTANCE g_hInst;
LRESULT CALLBACK ProcWnd(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
typedef int (CALLBACK *InstHOOK)();
typedef int (CALLBACK *UninstHOOK)();
void LogSystem(char *log,...) //输出消息
{
char szBuffer[1024];
va_list va;
va_start(va,log);
_vsnprintf(szBuffer,sizeof(szBuffer)/sizeof(char),log,va);
va_end(va);
printf(szBuffer);
//SetDlgItemText(g_hWnd,IDC_MSG,szBuffer);
}
int main(int argc, char* argv[])
{
static HMODULE h=NULL;
static InstHOOK insthook;
static UninstHOOK uninsthook;
h=LoadLibrary("HOOK.DLL");
if (h==NULL)
{
LogSystem("No Find HOOK.DLL,Please Check!");
return 0;
}
insthook=(InstHOOK)GetProcAddress(h,"InstallHOOK");
insthook();
LogSystem("HOOK已经安装!");
Sleep(200000);
if (h!=NULL)
{
uninsthook=(InstHOOK)GetProcAddress(h,"UninstallHOOK");
uninsthook();
}
LogSystem("HOOK已经卸载!");
FreeLibrary(h);
printf("Hello World!/n");
getchar();
return 0;
}
我其实想实现的就是捕捉到系统的时间改变事件,
因为多个设备同步,时间是关键。