Hook技術2:全域鉤子

來源:互聯網
上載者:User

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;
}

我其實想實現的就是捕捉到系統的時間改變事件,

因為多個裝置同步,時間是關鍵。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.