美文网首页
[转]检测USB设备的插入和拔出

[转]检测USB设备的插入和拔出

作者: 小龙9000 | 来源:发表于2018-09-26 19:19 被阅读36次

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.实现下面是一个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 )
Description
Finds 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;
}

还没有验证过,先转载一下

相关文章

网友评论

      本文标题:[转]检测USB设备的插入和拔出

      本文链接:https://www.haomeiwen.com/subject/sjpjoftx.html