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)本来就是微 ...
随机推荐
- kubernets event 分析
1. event 是一个很重要的组成部分 event 分析 Kubernetes(K8s)Events介绍(上) Kubernetes Events介绍(中) Kubernetes Events介绍( ...
- 如何改变vim中的光标形状 : 在插入状态下显示为 beam?而在 其他 状态下 为 block?
分成两种情况来说明: 如果是在 shell 即: gnome-termial终端中, 来启动或 使用 vim的话, 你是 无法 实现这种需求的: 改变vim中的光标形状 : 在插入状态下显示为 bea ...
- 2019-4-23 plan
需要制作springcloud es6的技术文档和demo
- js基础语句
// for 循环语句 // if else 条件判断语句 // switch 条件循环语句 // while // do while // 这里的 i 是循环变量 一般初始值为0,因为下标从0开始 ...
- Python玩转Arduino——简单介绍
关于Python语言的介绍安装请参考廖雪峰的Python教程 Python是一门解释型语言,虽然不能够像c语言一样编译上传到Arduino--什么你说MicroPython,我们再说Arduino呢- ...
- 《图解Java多线程设计模式》读书笔记
略读中...后面详读的时候,补充经典图片和文字说明
- Eclipse下运行maven项目失败且Tomcat服务器也启动不了
今天遇到一个神奇的问题,在eclipse中创建一个maven项目后,Run on server 时说服务器启动失败.我以为是Eclipse配置tomcat的问题.找了一大堆没找到想要的答案!!! 我还 ...
- mint修改host
sudo xed /etc/hosts # Pycharm 0.0.0.0 account.jetbrains.com0.0.0.0 www.jetbrains.com #sublime text3 ...
- Go语言学习之12 etcd、contex、kafka消费实例、logagent
本节内容: 1. etcd介绍与使用 2. ElastcSearch介绍与使用 1. etcd介绍与使用 概念:高可用的分布式key-value存储,可以使用配置共享和服务发现 ...
- NetSec2019 20165327 Exp4 恶意代码分析
NetSec2019 20165327 Exp4 恶意代码分析 一.实践目标 1.监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分 ...