C# [WIN32] [API] Global Hook
NativeMethods.cs:
using System;
using System.Runtime.InteropServices; namespace GlobalHook
{
internal static class NativeMethods
{
#region window message
internal const int WM_MOUSEMOVE = 0x200; internal const int WM_LBUTTONDOWN = 0x201; internal const int WM_RBUTTONDOWN = 0x204; internal const int WM_MBUTTONDOWN = 0x207; internal const int WM_LBUTTONUP = 0x202; internal const int WM_RBUTTONUP = 0x205; internal const int WM_MBUTTONUP = 0x208; internal const int WM_LBUTTONDBLCLK = 0x203; internal const int WM_RBUTTONDBLCLK = 0x206; internal const int WM_MBUTTONDBLCLK = 0x209; internal const int WM_MOUSEWHEEL = 0x020A;
#endregion #region param idHook
internal const int WH_CALLWNDPROC = ; internal const int WH_CALLWNDPROCRET = ; internal const int WH_CBT = ; internal const int WH_DEBUG = ; internal const int WH_FOREGROUNDIDLE = ; internal const int WH_GETMESSAGE = ; internal const int WH_JOURNALPLAYBACK = ; internal const int WH_JOURNALRECORD = ; internal const int WH_KEYBOARD = ; internal const int WH_KEYBOARD_LL = ; internal const int WH_MOUSE = ; internal const int WH_MOUSE_LL = ; internal const int WH_MSGFILTER = -; internal const int WH_SHELL = ; internal const int WH_SYSMSGFILTER = ;
#endregion [StructLayout(LayoutKind.Sequential)]
internal class POINT
{
internal int x;
internal int y; internal POINT(int x, int y)
{
this.x = x;
this.y = y;
}
} [StructLayout(LayoutKind.Sequential)]
internal class MOUSEHOOKSTRUCT
{
internal POINT pt;
internal IntPtr hWnd;
internal int wHitTestCode;
internal int dwExtraInfo;
} [StructLayout(LayoutKind.Sequential)]
internal class MOUSEHOOKSTRUCTEX
{
internal MOUSEHOOKSTRUCT MOUSEHOOKSTRUCT;
internal uint mouseData;
} internal delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
}
}
UnsafeNativeMethods.cs:
using System;
using System.Runtime.InteropServices;
using static GlobalHook.NativeMethods; namespace GlobalHook
{
internal static class UnsafeNativeMethods
{
/// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx /// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn"></param>
/// <param name="hMod"></param>
/// <param name="dwThreadId"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, HandleRef hMod, int dwThreadId); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644993(VS.85).aspx /// </summary>
/// <param name="hhk"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern bool UnhookWindowsHookEx(HandleRef hhk); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms644974(VS.85).aspx /// </summary>
/// <param name="hhk"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr CallNextHookEx(HandleRef hhk, int nCode, IntPtr wParam, IntPtr lParam); /// <summary>
/// https://msdn.microsoft.com/en-us/library/ms683199(VS.85).aspx /// </summary>
/// <param name="lpModuleName"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetModuleHandle(string lpModuleName);
}
}
From1.cs:
using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using static GlobalHook.NativeMethods;
using static GlobalHook.SafeNativeMethods;
using static GlobalHook.UnsafeNativeMethods; namespace GlobalHook
{
public partial class Form1 : Form
{
private IntPtr hook; private HookProc mouseHookProcedure; public Form1()
{
InitializeComponent();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnhookWindowsHookEx(new HandleRef(null, hook));
} private void btnHook_Click(object sender, EventArgs e)
{
if (btnHook.Text == "hook")
{
mouseHookProcedure = LowLevelMouseProc;
hook = SetWindowsHookEx(WH_MOUSE_LL, mouseHookProcedure, new HandleRef(null, GetModuleHandle(null)), );
if (hook != IntPtr.Zero)
{
btnHook.Text = "unhook";
}
}
else
{
if (UnhookWindowsHookEx(new HandleRef(null, hook)))
{
btnHook.Text = "hook";
}
}
} private IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
{
MOUSEHOOKSTRUCT mouseHookStruct = Marshal.PtrToStructure<MOUSEHOOKSTRUCT>(lParam);
switch ((int)wParam)
{
case WM_MOUSEMOVE:
break;
case WM_LBUTTONDOWN:
break;
case WM_RBUTTONDOWN:
break;
case WM_MBUTTONDOWN:
break;
case WM_LBUTTONUP:
break;
case WM_RBUTTONUP:
break;
case WM_MBUTTONUP:
break;
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONDBLCLK:
break;
case WM_MBUTTONDBLCLK:
break;
case WM_MOUSEWHEEL:
break;
default:
break;
}
return CallNextHookEx(new HandleRef(null, hook), nCode, wParam, lParam);
}
}
}
C# [WIN32] [API] Global Hook的更多相关文章
- Detours简介 (拦截x86机器上的任意的win32 API函数)
Detours 当然是用detours,微软明显高腾讯一筹,同上,至今没失败过.写这种HOOK一定要再写个测试程序,不要直接HOOK你的目的程序,例如QQ,因为这样不方面更灵活的测试.说明一下:Det ...
- 初次认识 C# win32 api
第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...
- Serial Port Programming using Win32 API(转载)
In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...
- 从.NET平台调用Win32 API
MSDN文章<Microsoft Win32 to Microsoft .NET Framework API Map> 介绍了.net 类库对win32的封装 从.NET平台调用Win32 ...
- 【温故Delphi】GAEA用到Win32 API目录
Delphi是Windows平台下著名的快速应用程序开发工具,它在VCL中封装并使用了大量的Win32 API. GAEA基于VCL开发的工具类产品,在程序中使用了大量的Win32 API,将经常用到 ...
- 【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)
注:这里的服务是指Windows 服务. ------------------201508250915更新------------------ 刚刚得知TransactedInstaller类是支持带 ...
- C#中导入Win32 API函数
C#中导入Win32 API的方法: 1.引用命名空间 using System.Net.Security; using System.Runtime.InteropServices; 2. [Dll ...
- MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)
转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...
- C#调用Win32 api学习总结
从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...
随机推荐
- MongoDB --- 02. 基本操作,增删改查,数据类型,比较符,高级用法,pymongo
一.基本操作 . mongod 启动服务端 2. mongo 启动客户端 3. show databses 查看本地磁盘的数据库 4. use 库名 切换到要使用的数据库 5. db 查看当前使用的数 ...
- android开发_Eclipse新建项目+新建模拟器
一.新建项目 1 Eclipse->右键->new->Android Application Project,得到下图: 2 输入创建项目信息 3 点击next 4 点击 next ...
- 容器中的诊断与分析4——live diagnosis——LTTng
官网地址 LTTng 简介&使用实战 使用LTTng链接内核和用户空间应用程序追踪 简介: LTTng: (Linux Trace Toolkit Next Generation),它是用于跟 ...
- python on sql
USE [DemoDb]GOexecute sp_execute_external_script @language = N'Python', @script = N'a = 1b = 2c = a/ ...
- IDLE清屏扩展
新建ClearWindows.py,复制以下代码: class ClearWindow: menudefs = [ ('options', [None, ('Clear Shell Window', ...
- Java问题解决:springboot启动出现-Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
参考资料: http://www.mamicode.com/info-detail-2101273.html https://blog.csdn.net/u012834750/article/deta ...
- loadrunner中使用web_custom_request函数调用webservice接口
1.使用的接口地址: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName 以SOAP ...
- python pyqt绘制直方图
# -*- coding: utf-8 -*- """ In this example we draw two different kinds of histogram. ...
- 《R语言入门与实践》第六章:R 的环境系统
前言 这一章在对象的基础之上,讲解了对象所处的环境,进一步讲了环境对对象的作用,以及如何使用环境.结构如下: 环境的定义和操作 环境的规则 制作闭包 环境 R 环境的定义 在 R 中,每一个数据对象都 ...
- python 学习笔记 5 ----> dive into python 3
字符串 文本:屏幕上显示的字符或者其他的记号 计算机认识的东西:位(bit)和字节(byte) 文本的本质:某种字符编码方式保存的内容. 字符编码:一种映射(显示的内容 ----> 内存.磁盘 ...