检测USB设备的插入和拔出
来源:互联网 发布:java url转base64编码 编辑:程序博客网 时间:2024/04/29 04:19
1.需求
需要一个DLL,它可以自动检测USB设备插入和拔出,并以回调函数的方法告诉调用DLL的应用程序。
2.预研
2.1参考文档
(1)关于“Registering for DeviceNotification”(官方文档)
地址:https://msdn.microsoft.com/en-us/library/aa363432(VS.85).aspx
(2)关于“Detecting Media Insertionor Removal”(官方文档)
地址:https://msdn.microsoft.com/en-us/library/aa363215(v=vs.85).aspx
(3)stackoverflow中的一个问答“Detecting USBInsertion / Removal Events in Windows using C++”
地址:http://stackoverflow.com/questions/4078909/detecting-usb-insertion-removal-events-in-windows-using-c
(4)codeproject中的一个实例“Detecting whendrives are added or removed”
(包含代码和demo,参考价值很高!)
地址:http://www.codeproject.com/Articles/19264/Detecting-when-drives-are-added-or-removed
3.实现
需要一个DLL,它可以自动检测USB设备插入和拔出,并以回调函数的方法告诉调用DLL的应用程序。
2.预研
2.1参考文档
(1)关于“Registering for DeviceNotification”(官方文档)
地址:https://msdn.microsoft.com/en-us/library/aa363432(VS.85).aspx
(2)关于“Detecting Media Insertionor Removal”(官方文档)
地址:https://msdn.microsoft.com/en-us/library/aa363215(v=vs.85).aspx
(3)stackoverflow中的一个问答“Detecting USBInsertion / Removal Events in Windows using C++”
地址:http://stackoverflow.com/questions/4078909/detecting-usb-insertion-removal-events-in-windows-using-c
(4)codeproject中的一个实例“Detecting whendrives are added or removed”
(包含代码和demo,参考价值很高!)
地址:http://www.codeproject.com/Articles/19264/Detecting-when-drives-are-added-or-removed
3.实现
下面是一个DEMO的代码,但它不是DLL(DLL的DEMO,以后再写)。这个实例的主要代码来自csdn论坛的一个问答(地址:http://bbs.csdn.net/topics/390112567),非常感谢版主同学“VisualEleven”!
#include <tchar.h>#include <string>#include <iostream>#include <Windows.h>#include <dbt.h>/*------------------------------------------------------------------FirstDriveFromMask( unitmask )DescriptionFinds the first valid drive letter from a mask of drive letters.The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C, and so on. A valid drive letter is defined when the corresponding bit is set to 1.Returns the first drive letter that was found.--------------------------------------------------------------------*/char FirstDriveFromMask(ULONG unitmask){char i;for (i = 0; i < 26; ++i){if (unitmask & 0x1)break;unitmask = unitmask >> 1;}return(i + 'A');}LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;TCHAR szMsg[80];char driveName; switch(uMsg) { case WM_DEVICECHANGE:switch(wParam ){case DBT_DEVICEARRIVAL:driveName = FirstDriveFromMask(lpdbv->dbcv_unitmask);sprintf(szMsg, "USB Drive %c: has inserted.\n", driveName);printf("%s\r\n", szMsg);MessageBox(hWnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK);break;case DBT_DEVICEREMOVECOMPLETE:driveName = FirstDriveFromMask(lpdbv->dbcv_unitmask);sprintf(szMsg, "USB Drive %c: has removed.\n", driveName);printf("%s\r\n", szMsg);MessageBox(hWnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK );break;default:;} break;default:; } return DefWindowProc(hWnd, uMsg, wParam, lParam);}int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ TCHAR szClassName[] = _T("MyApp"); WNDCLASS wndcls = {0}; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION); wndcls.lpfnWndProc = WndProc; wndcls.lpszClassName = szClassName; if(!RegisterClass(&wndcls)) { printf("RegisterClass Failed!\r\n"); return 0; } HWND hWnd = CreateWindow(szClassName, szClassName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if(NULL == hWnd) { printf("CreateWindow Failed!\r\n"); return 0; } ShowWindow(hWnd, SW_HIDE); UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0;}
1 0
- 检测USB设备的插入和拔出
- 检测USB设备的插入和拔出
- 检测USB设备插入和拔出
- USB设备插入和拔出的波形
- USB设备插入与拔出的响应
- USB设备插入与拔出的响应
- USB驱动程序代码分析——检测设备插入拔出
- MFC 如何检测到USB设备插入拔出
- 一个很有用的检测USB设备插入拔出的对象
- QT编程--监听USB设备拔出和插入。
- Windows中USB设备插入的拔出和所产生的消息值
- Windows中USB设备插入的拔出和所产生的消息值
- 检测USB或SD卡的插入与拔出
- USB设备插入/拔出时重要的windows消息值
- USB设备的插入检测
- USB设备的插入检测
- QT 监听 USB 设备 插入、拔出动作
- android-检测耳机的插入和拔出动作
- cmd下运行php脚本 不支持curl库的问题
- win2008R2 不能访问局域网共享\局域网共享中无本机,解决办法
- leetcode-226-Invert Binary Tree
- STL中的常用算法
- 标准字符串和宽字符串处理函数
- 检测USB设备的插入和拔出
- poj 1060 Modular multiplication of polynomials 除数是大数的高精度除法
- 第四章第二十一题
- Linux安装
- 2015062107 - 快速学习的价值
- 重装电脑系统Win7并激活、删除其中一个系统、修复电脑 发出尖锐的 嘀嘀嘀嘀嘀 警报生声
- poj 1018 Communication System
- HDU Piggy-Bank (完全背包)
- 暗时间里的明道理