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的更多相关文章

  1. Detours简介 (拦截x86机器上的任意的win32 API函数)

    Detours 当然是用detours,微软明显高腾讯一筹,同上,至今没失败过.写这种HOOK一定要再写个测试程序,不要直接HOOK你的目的程序,例如QQ,因为这样不方面更灵活的测试.说明一下:Det ...

  2. 初次认识 C# win32 api

    第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...

  3. Serial Port Programming using Win32 API(转载)

    In this tutorial we will learn How to communicate with an external device like a microcontroller boa ...

  4. 从.NET平台调用Win32 API

    MSDN文章<Microsoft Win32 to Microsoft .NET Framework API Map> 介绍了.net 类库对win32的封装 从.NET平台调用Win32 ...

  5. 【温故Delphi】GAEA用到Win32 API目录

    Delphi是Windows平台下著名的快速应用程序开发工具,它在VCL中封装并使用了大量的Win32 API. GAEA基于VCL开发的工具类产品,在程序中使用了大量的Win32 API,将经常用到 ...

  6. 【C#】分享基于Win32 API的服务操作类(解决ManagedInstallerClass.InstallHelper不能带参数安装的问题)

    注:这里的服务是指Windows 服务. ------------------201508250915更新------------------ 刚刚得知TransactedInstaller类是支持带 ...

  7. C#中导入Win32 API函数

    C#中导入Win32 API的方法: 1.引用命名空间 using System.Net.Security; using System.Runtime.InteropServices; 2. [Dll ...

  8. MSIL 教程(二):数组、分支、循环、使用不安全代码和如何调用Win32 API(转)

    转自:http://www.cnblogs.com/Yahong111/archive/2007/08/16/857574.html 续上文[翻译]MSIL 教程(一) ,本文继续讲解数组.分支.循环 ...

  9. C#调用Win32 api学习总结

    从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...

随机推荐

  1. freeswitch 事件命令

    1.uuid_bridge 桥接两条呼叫的腿. Usage: uuid_bridge <uuid> <other_uuid> uuid_bridge至少需要有一条腿是被呼通的. ...

  2. 王之泰201771010131《面向对象程序设计(java)》第十二周学习总结

    第一部分:理论知识学习部分 第10章 图形程序设计 10.1 AWT与Swing简介 1.用户界面(User Interface) 的概念:用户与计算机系统(各种程序)交互的接口2.图形用户界面(Gr ...

  3. Office 2016 自定义安装

    Office2016已经不提供自定义安装功能,而采用C2R安装方式.使用镜像安装时,默认全部安装.想要自定义安装就需要用到微软提供的Office2016部署工具. 步骤 下载并运行微软提供的Offic ...

  4. 比原链(Bytom)先知节点 Ubuntu接入文档

    系统要求 我们建议选择知名的VPS服务商,运行比原链节点对算力没有要求,但是请配置尽可能大的磁盘空间. 节点服务器最小配置: 操作系统: Windows/Linux/Docker CPU: 2核 内存 ...

  5. idea自个常用工具的总结

    1.直接打开某类:ctrl+shift+t2.注释某类:ctrl+?3. implementation :Ctrl+T4.rename:Alt +Shirft +R5.Show Intention A ...

  6. Java的异常机制

    Java的异常机制 (一)异常的概念 异常是指程序在编译或运行时出现的导致程序不能继续编译或运行的状况.. (二)Throwable类 Throwable类继承自Object类,是Java中所有错误或 ...

  7. Java 的 clone 方法 && 浅复制和深复制

    1 Java中对象的创建过程 java创建对象的方式有以下两种: (1)使用new操作符创建一个对象 (2)使用clone的方法复制一个对象,(在Java中,clone是Object类的protect ...

  8. JQuery Checkbox 获取多选值 Checkbox选中个数

    1.获取checkbox选中个数 $("input[name='ckb-jobid']:checked").length $("input[type='checkbox' ...

  9. C# 封装SDK 获取摄像头的水平角度和垂直角度

    最近需要做一个C#版本的控制终端,控制摄像头,获取摄像头的水平角度和垂直角度 获取当前摄像头的角度,需要调用一个名为NET_DVR_GetDVRConfig的bool类型的函数 在C++中,函数定义: ...

  10. DAY18 常用模块(二)

    一.随机数:RANDOM 1.(0,1)小数:random.random() 2.[1,10]整数:random.randint(1,10) 3.[1,10)整数:random.randrang(1, ...