Windows键盘钩子
Runtime:VS2013
#include "stdafx.h"
#include <windows.h>
#include <conio.h> DWORD g_main_tid = ;
HHOOK g_kb_hook = ; bool CALLBACK con_handler(DWORD CtrlType)
{
PostThreadMessage(g_main_tid, WM_QUIT, , );
return TRUE;
}; LRESULT CALLBACK kb_proc(int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
const char *info = NULL;
if (w == WM_KEYDOWN)
info = "key dn";
else if (w == WM_KEYUP)
info = "key up";
else if (w == WM_SYSKEYDOWN)
info = "sys key dn";
else if (w == WM_SYSKEYUP)
info = "sys key up";
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 KbHookThread(LPVOID lpParam)
{
g_main_tid = GetCurrentThreadId(); //register Ctrl+C callback
SetConsoleCtrlHandler((PHANDLER_ROUTINE)&con_handler, TRUE);
g_kb_hook = SetWindowsHookEx(WH_KEYBOARD_LL, //Installs a hook procedure that monitors low-level keyboard input events
(HOOKPROC)&kb_proc, //A hook procedure that monitors low-level keyboard input events
NULL,//A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.(GetModuleHandle(NULL))
);//The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.
if (g_kb_hook == NULL) {
printf("SetWindowsHookEx failed, %ld\n", GetLastError());
return ;
} // Message loop
MSG msg; //If GetMessage retrieves the WM_QUIT message, the return value is zero.
while (GetMessage(&msg, NULL, , )) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} //exit the thread
UnhookWindowsHookEx(g_kb_hook);
return ;
}; int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h = CreateThread(NULL, , KbHookThread, NULL, , NULL);
if (NULL == h) {
printf("CreateThread failed, %ld\n", GetLastError());
return -;
} //wait thread to exit
WaitForMultipleObjects(, &h, TRUE, INFINITE); return ;
}
Windows键盘钩子的更多相关文章
- 安全之路 —— 使用Windows全局钩子打造键盘记录器
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
- windows 键盘全局钩子
// HookapiTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #inc ...
- 使用windows函数SetWindowsHookEx实现键盘钩子
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 使用Windows全局钩子打造键盘记录器
简介 键盘记录功能一直是木马等恶意软件窥探用户隐私的标配,那么这个功能是怎么实现的呢?在Ring3级下,微软就为我们内置了一个Hook窗口消息的API,也就是SetWindowsHookEx函数,这个 ...
- windows消息钩子注册底层机制浅析
标 题: [原创]消息钩子注册浅析 作 者: RootSuLe 时 间: 2011-06-18,23:10:34 链 接: http://bbs.pediy.com/showthread.php?t= ...
- 基于C#实现的HOOK键盘钩子实例代码
本文所述为基于C#实现的HOOK实例,该实例可用来屏蔽系统热键.程序主要实现了安装钩子.传递钩子.卸载钩子等功能.在传递钩子中:<param name="pHookHandle&quo ...
- C#鼠标键盘钩子
using System;using System.Collections.Generic; using System.Reflection; using System.Runtime.Interop ...
- C# 键盘钩子
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例
键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...
随机推荐
- 洛谷P3480 KAM-Pebbles
题目大意: 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件.谁没有石子可移时输掉游戏.问先手是 ...
- application对象的应用案例
application对象由多个客户端用户共享,它的应用范围是所有的客户,服务器启动后,新建一个application对象,该对象一旦建立,就一直保持到服务器关闭.当有客户访问服务器上的一个JSP页面 ...
- java-关于java_home配置,classpath配置和javac,java命令,javac编译器,和java虚拟机之间的关系
在每个人学习java的第一步,都是安装jdk ,jre,配置java_home,classpath,path. 为什么要做这些?在阅读java-core的时候,看到了原理,p141. 一 关于类的共享 ...
- 2018-2019-2 20165232《网络对抗技术》Exp1 缓冲区溢出实验
2018-2019-2 20165232<网络对抗技术>Exp1 缓冲区溢出实验 实验点1:逆向及Bof基础实践 实践任务 用一个pwn1文件. 该程序正常执行流程是:main调用foo函 ...
- 为Nexus配置阿里云代理仓库【转】
Nexus默认远程仓库为https://repo1.maven.org/maven2/ 慢死,还常连不上. 可以添加阿里云代理仓库 URL:http://maven.aliyun.com/nexus/ ...
- Angular记录(11)
开始使用Angular写页面 使用WebStorm:版本2018.3.5 官网资料 资料大部分有中文翻译,很不错 速查表:https://www.angular.cn/guide/cheatsheet ...
- JavaScript 归纳
MDN 本文以 NodeJS 为交互解释器实验 尽量遵循 ES6 标准 javascript 重点 1.javascript 是单线程,通过 EventLoop 实现模拟异步,其中包括宏任务,微任务 ...
- 《JAVA并发编程实战》示例程序 第三章
3.1 可见性 程序清单3-1 在没有同步的情况下共享变量(不要这么做) /** * 主线程和读线程都将访问共享变量:ready 和 number * 结果可能 * 1. 主线程先运行完,读线程后运行 ...
- nodejs学习以及SSJS漏洞
0x01 简介 什么是nodejs,it's javascript webserver! JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对 ...
- 毕业设计——Django邮件发送功能实现及问题记录
django发送邮件:send_mail()&send_mass_mail() 自强学堂 刘江的博客 HTTPS,TLS和SSL django发送邮件及其相关问题 步骤 : 0. 登录作为发送 ...