惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
L
LINUX DO - 最新话题
博客园_首页
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
C
Cisco Blogs
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
A
About on SuperTechFans
G
GRAHAM CLULEY
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
D
DataBreaches.Net
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
I
InfoQ
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 叶小钗
Project Zero
Project Zero

博客园 - 幸福的菜菜

windows下使用ACME申请SSL证书的办法 ElementPlus Radio 实现双击取消选择的效果 Windows10 LTSC版本 无法访问网络中部分的共享文件夹 SqlSugar : date绑定到XX失败,可以试着换一个类型,或者使用ORM自定义类型实现 VisualStudio Debug模式突然变慢 Visual Stadio 编译提示 The BaseOutputPath/OutputPath property is not set for project ... winform绘图与前端canvas绘图效率对比 node-sass编译不通过, 提示 “checking for Python executable "python2" in the PATH” c# async await的使用方式及为啥要用它 Laravel The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths Wampserver 配置端口可访问服务 git credential for windows 总是弹出的问题 如何用B表的数据,更新A表的值 WampServer部署https 服务的过程 PHP 命名空间冲突解决方式 Windows下 Docker 简单部署 Django应用 C#实现后台格式化U盘的功能 Winform 实现断点续传的思路及代码 WAMPServer ServerName has syntax error 的问题(阿里云服务器上)
winform 实现对usb热拔插的监听
幸福的菜菜 · 2022-01-21 · via 博客园 - 幸福的菜菜

本文参考链接

https://www.cnblogs.com/gsk99/p/4983043.html
https://blog.csdn.net/z0582/article/details/7328290
https://docs.microsoft.com/en-us/windows/win32/devio/registering-for-device-notification

最近因为一个项目需要跟硬件打交道,所以需要监听存储设备的事件。看了很多资料,最有用的还是上面的三个篇,故此分享出来

Windows消息有两种监听方式:

  1.通过服务的方式监听(未涉及)

  2.通过窗体的方式监听(本例)

在demo中,可能很多人会疑惑

            public const int WM_DEVICECHANGE = 537;
            /// BROADCAST_QUERY_DENY -> 0x424D5144
            //public const int BROADCAST_QUERY_DENY = 1112363332;
            //public const int DBT_DEVTYP_DEVICEINTERFACE = 5;
            //public const int DBT_DEVTYP_HANDLE = 6;
            public const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
                                                         //public const int DBT_DEVICEQUERYREMOVE = 0x8001;   // Preparing to remove (any program can disable the removal)
            public const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // removed 
            public const int DBT_DEVTYP_VOLUME = 0x00000002; // drive type is logical volume

  这里的值,比如 “0x8000” 的值是怎么来的?

  就需要查看微软的官方手册,比如,我们是需要实现监听usb的功能,就看官方的代码

  https://docs.microsoft.com/en-us/windows/win32/devio/registering-for-device-notification

……
// Output some messages to the window. switch (wParam) { case DBT_DEVICEARRIVAL: msgCount++; StringCchPrintf( strBuff, 256, TEXT("Message %d: DBT_DEVICEARRIVAL\n"), (int)msgCount); break; case DBT_DEVICEREMOVECOMPLETE: msgCount++; StringCchPrintf( strBuff, 256, TEXT("Message %d: DBT_DEVICEREMOVECOMPLETE\n"), (int)msgCount); break; case DBT_DEVNODES_CHANGED: msgCount++; StringCchPrintf( strBuff, 256, TEXT("Message %d: DBT_DEVNODES_CHANGED\n"), (int)msgCount); break; default: msgCount++; StringCchPrintf( strBuff, 256, TEXT("Message %d: WM_DEVICECHANGE message received, value %d unhandled.\n"), (int)msgCount, wParam); break; }
……

  我们要查看设备刚刚连接时的值,其实就是  DBT_DEVICEARRIVAL 的枚举。查看定义的时候,能看到具体定义的值

……
#define
DBT_DEVICEARRIVAL 0x8000 // system detected a new device #define DBT_DEVICEQUERYREMOVE 0x8001 // wants to remove, may fail #define DBT_DEVICEQUERYREMOVEFAILED 0x8002 // removal aborted #define DBT_DEVICEREMOVEPENDING 0x8003 // about to remove, still avail. #define DBT_DEVICEREMOVECOMPLETE 0x8004 // device is gone #define DBT_DEVICETYPESPECIFIC 0x8005 // type specific event
……

  如果还想实现对windows其它事件的监听处理,只需要查到对应编码,然后处理相关事件即可。

项目的demo地址: