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)本来就是微 ...
随机推荐
- spring-data-jpa中save不触发数据库insert语句的问题
最近学习spring mvc,用到jpa简化DAO层代码,发现save死活不触发SQL语句,找了好久才解决这个问题,实在是坑.. <!-- 关键是这个bean,一定要设置正确才行 --> ...
- ECS之Git服务器搭建
最简教程 ### . 安装Git 安装Git服务,命令如下: ```Shell $ yum install curl-devel expat-devel gettext-devel openssl-d ...
- 2018年—2019年第二学期第四周C#学习个人总结
第五章面向对象高级包括:5.1类的继承中有5.1.1继承的概念:一是在现实生活中,继承一般指的是子女继承父辈的财产;在C#,类的继承是指在一个现有类的基础上去构建一个新的类,构建出来的新类被称作子类, ...
- HTML与CSS的一些知识(二)
续: 5.表单标签<form></form> 用于收集用户信息,统一提交到服务器 一般用input标签收集,再用提交按钮提交:input标签根据type属性值不同有不同的类型: ...
- ejs常用功能函数
利用<%- include filename %>加载其他页面模版: 1.缓存功能,能够缓存已经解析好的html模版: 2.<% code %>用于执行其中javascript ...
- [十二省联考2019]异或粽子 01trie
[十二省联考2019]异或粽子 01trie 链接 luogu 思路 首先求前k大的(xo[i]^xo[j])(i<j). 考场上只想到01trie,不怎么会写可持久,就写了n个01trie,和 ...
- gcc 无法编译c17程序解决办法
1.保证将gcc程序升级到7.1以上. 2.如果用命令行手工编译,就多加个参数-std=c++17,例如命令应该是: "g++" -std=c++17 "text.cp ...
- C#socket编程之实现一个简单的TCP通信
TCP(TransmissionControl Protocol)传输控制协议. 是一种可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TC ...
- virtual box centos7 common operation
======= network part =======1.设置桥接模式 2.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3BOOTPROTO=stati ...
- springboot与Mybatis结合
一:使用generator,关联上数据库生成相关文件, 如 mapping/UserMapper.xml,mapper/UserMapper.java,model/User.java; generat ...