[轉(zhuǎn)]檢測USB設(shè)備的插入和拔出

1.需求
需要一個DLL,它可以自動檢測USB設(shè)備插入和拔出,并以回調(diào)函數(shù)的方法告訴調(diào)用DLL的應(yīng)用程序。

2.預(yù)研
2.1參考文檔
(1)關(guān)于“Registering for DeviceNotification”(官方文檔)
地址:https://msdn.microsoft.com/en-us/library/aa363432(VS.85).aspx
(2)關(guān)于“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中的一個實(shí)例“Detecting whendrives are added or removed”
(包含代碼和demo,參考價值很高?。?br> 地址:http://www.codeproject.com/Articles/19264/Detecting-when-drives-are-added-or-removed

3.實(shí)現(xiàn)下面是一個DEMO的代碼,但它不是DLL(DLL的DEMO,以后再寫)。這個實(shí)例的主要代碼來自csdn論壇的一個問答(地址:http://bbs.csdn.net/topics/390112567),非常感謝版主同學(xué)“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;
}

還沒有驗(yàn)證過,先轉(zhuǎn)載一下

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容