64,650
社区成员
发帖
与我相关
我的任务
分享
/*
kbhook.cpp
*/
#define _WIN32_WINNT 0400
#define STRICT
#define WIN32_LEAN_AND_MEAN
#define WH_KEYBOARD_LL 13
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//#include <iostream.h>
DWORD g_main_tid = 0;
HHOOK g_kb_hook = 0;
int _sign = 0;
DWORD WINAPI Fun1Proc(LPVOID lpParameter);//thread data
BOOL CALLBACK con_handler (DWORD)
{
PostThreadMessage (g_main_tid, WM_QUIT, 0, 0);
return TRUE;
};
LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
DWORD vkCode = p->vkCode;
//const char *info = NULL;
if (w == WM_KEYDOWN)
{
printf("key down...\n");
}
else if (w == WM_KEYUP)
{
if(vkCode == 221)
{
printf("...key up [%d]\n", vkCode);
if(_sign == 0)
{
_sign = 1;
printf("we need open you.\n");
HANDLE hThread1 = CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
}
else
{
_sign = 0;
printf("we need stop you.\n");
system("taskkill /im wscript.exe /f");
}
}
}
/*
printf ("%s - vkCode [%04x], scanCode [%04x]\n",info, p->vkCode, p->scanCode);
*/
// always call next hook
return CallNextHookEx (g_kb_hook, code, w, l);
};
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
printf("the thread is start.\n");
system("todoscan.vbs");
printf("the thread is end.\n");
};
int main (void)
{
g_main_tid = GetCurrentThreadId ();
SetConsoleCtrlHandler (&con_handler, TRUE);
printf ("1\n");
g_kb_hook = SetWindowsHookEx (WH_KEYBOARD_LL, &kb_proc, GetModuleHandle(NULL), 0);// 不能为NULL,否则失败
printf ("2\n");
if (g_kb_hook == NULL)
{
fprintf (stderr, "SetWindowsHookEx failed with error %d\n", ::GetLastError ());
return 0;
};
printf ("3\n");
// 消息循环是必须的.
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
};
printf ("4\n");
UnhookWindowsHookEx (g_kb_hook);
printf ("5\n");
return 0;
};