怎么linux区分硬盘和u盘u盘插入和拔出时触发的dbt

2033人阅读
原理说明:创建一个win32窗体程序。使用全局变量hWnd获取创建窗体的句柄。在winmain函数中创建窗体,同时将盘符改变的消息注册给窗体。在窗体回调函数中添加对盘符改变消息的响应处理即可。
// TestWin32.cpp : Defines the entry point for the application.//
#include "stdafx.h"#include "TestWin32.h"#include &dbt.h&#include &strsafe.h&
#define MAX_LOADSTRING 100
// Global Variables:HINSTANCE hI&&&&&&&&// current instanceTCHAR szTitle[MAX_LOADSTRING];&&&&&// The title bar textTCHAR szWindowClass[MAX_LOADSTRING];&&&// the main window class nameHWND hW&&& //自定义全局变量,窗体句柄
// Forward declarations of functions included in this code module:ATOM&&&&MyRegisterClass(HINSTANCE hInstance);BOOL&&&&InitInstance(HINSTANCE, int);LRESULT CALLBACK&WndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACK&About(HWND, UINT, WPARAM, LPARAM);
//获取新增的盘符,即usb接口插入设备的盘符& 光如此其实还是不够的,最好的就是使用GetDeviceType()函数对盘符加以判断char FirstDriveFromMask (ULONG unitmask){&&
&& for (i = 0; i & 26; ++i)&& {&&&&& if (unitmask & 0x1)&&&&&&&&&&&&& unitmask = unitmask && 1;&& }
&& return (i + 'A');}
///向窗体注册盘符改变 的消息BOOL DoRegisterDeviceInterface( &&&&&&&&& GUID InterfaceClassGuid, &&&&&&&&& HDEVNOTIFY *hDevNotify &&&&&&&&& )&&&&&&&&& /*&&&&&&&&& Routine Description:&&&&&&&&& Registers for notification of changes in the device interfaces for&&&&&&&&& the specified interface class GUID.
&&&&&&&&& Parameters:&&&&&&&&& InterfaceClassGuid - The interface class GUID for the device &&&&&&&&& interfaces.
&&&&&&&&& hDevNotify - Receives the device notification handle. On failure, &&&&&&&&& this value is NULL.
&&&&&&&&& Return Value:&&&&&&&&& If the function succeeds, the return value is TRUE.&&&&&&&&& If the function fails, the return value is FALSE.&&&&&&&&& */
{&DEV_BROADCAST_DEVICEINTERFACE NotificationF&char szMsg[80];
&ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );&NotificationFilter.dbcc_size = &&sizeof(DEV_BROADCAST_DEVICEINTERFACE);&NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;&NotificationFilter.dbcc_classguid = InterfaceClassG
&*hDevNotify = RegisterDeviceNotification( hWnd, &&&NotificationFilter,&&DEVICE_NOTIFY_WINDOW_HANDLE&&);
&if(!*hDevNotify) &{&&sprintf(szMsg, "RegisterDeviceNotification failed: %d/n", &&&GetLastError());&&MessageBox(hWnd, szMsg, "Registration", MB_OK);&&&&&&& &&return FALSE;&}
&return TRUE;}
int APIENTRY _tWinMain(HINSTANCE hInstance,&&&&&&&&&&&&&&&&&&&& HINSTANCE hPrevInstance,&&&&&&&&&&&&&&&&&&&& LPTSTR&&& lpCmdLine,&&&&&&&&&&&&&&&&&&&& int&&&&&& nCmdShow){&UNREFERENCED_PARAMETER(hPrevInstance);&UNREFERENCED_PARAMETER(lpCmdLine);
&&// TODO: Place code here.&MSG&HACCEL hAccelT
&// Initialize global strings&LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);&LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);&MyRegisterClass(hInstance);
&// Perform application initialization:&if (!InitInstance (hInstance, nCmdShow))&{&&return FALSE;&}
//定义盘符改变消息的guid,并通过函数向窗体注册
&_GUID&&&&&&&&&&&& GUID_DEVINTERFACE_USB_DEVICE;&GUID_DEVINTERFACE_USB_DEVICE.Data1 = 0xA5DCBF10L;&GUID_DEVINTERFACE_USB_DEVICE.Data2 = 0x6530;&GUID_DEVINTERFACE_USB_DEVICE.Data3 = 0x11D2;&GUID_DEVINTERFACE_USB_DEVICE.Data4[0] = 0x90;&GUID_DEVINTERFACE_USB_DEVICE.Data4[1] = 0x1F;&GUID_DEVINTERFACE_USB_DEVICE.Data4[2] = 0x00;&GUID_DEVINTERFACE_USB_DEVICE.Data4[3] = 0xC0;&GUID_DEVINTERFACE_USB_DEVICE.Data4[4] = 0x4F;&GUID_DEVINTERFACE_USB_DEVICE.Data4[5] = 0xB9;&GUID_DEVINTERFACE_USB_DEVICE.Data4[6] = 0x51;&GUID_DEVINTERFACE_USB_DEVICE.Data4[7] = 0xED;
&HDEVNOTIFY hDevN&DoRegisterDeviceInterface( GUID_DEVINTERFACE_USB_DEVICE, &hDevNotify);&//WaitForSingleObject(hDevNotify, INFINITE);
&hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN32));
&// Main message loop:&while (GetMessage(&msg, NULL, 0, 0))&{&&if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))&&{&&&TranslateMessage(&msg);&&&DispatchMessage(&msg);&&}&}
&return (int) msg.wP}
////& FUNCTION: MyRegisterClass()////& PURPOSE: Registers the window class.////& COMMENTS:////&&& This function and its usage are only necessary if you want this code//&&& to be compatible with Win32 systems prior to the 'RegisterClassEx'//&&& function that was added to Windows 95. It is important to call this function//&&& so that the application will get 'well formed' small icons associated//&&& with it.//ATOM MyRegisterClass(HINSTANCE hInstance){&WNDCLASSEX
&wcex.cbSize = sizeof(WNDCLASSEX);
&wcex.style&&&= CS_HREDRAW | CS_VREDRAW;&wcex.lpfnWndProc&= WndP&wcex.cbClsExtra&&= 0;&wcex.cbWndExtra&&= 0;&wcex.hInstance&&= hI&wcex.hIcon&&&= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN32));&wcex.hCursor&&= LoadCursor(NULL, IDC_ARROW);&wcex.hbrBackground&= (HBRUSH)(COLOR_WINDOW+1);&wcex.lpszMenuName&= MAKEINTRESOURCE(IDC_TESTWIN32);&wcex.lpszClassName&= szWindowC&wcex.hIconSm&&= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
&return RegisterClassEx(&wcex);}
////&& FUNCTION: InitInstance(HINSTANCE, int)////&& PURPOSE: Saves instance handle and creates main window////&& COMMENTS:////&&&&&&& In this function, we save the instance handle in a global variable and//&&&&&&& create and display the main program window.//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){&& hInst = hI // Store instance handle in our global variable
&& hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,&&&&& CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
&& if (!hWnd)&& {&&&&& return FALSE;&& }
///显示窗体并刷新
&& ShowWindow(hWnd, nCmdShow);&& UpdateWindow(hWnd);
&& return TRUE;}
////& FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)////& PURPOSE:& Processes messages for the main window.////& WM_COMMAND&- process the application menu//& WM_PAINT&- Paint the main window//& WM_DESTROY&- post a quit message and return////LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){&int wmId, wmE&PAINTSTRUCT&HDC
&switch (message)&{&case WM_COMMAND:&&wmId&&& = LOWORD(wParam);&&wmEvent = HIWORD(wParam);&&// Parse the menu selections:&&switch (wmId)&&{&&case IDM_ABOUT:&&&DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);&&&&&case IDM_EXIT:&&&DestroyWindow(hWnd);&&&&&default:&&&return DefWindowProc(hWnd, message, wParam, lParam);&&}&&&case WM_PAINT:&&hdc = BeginPaint(hWnd, &ps);&&// TODO: Add any drawing code here...&&EndPaint(hWnd, &ps);&&&case WM_DESTROY:&&PostQuitMessage(0);&&&///////盘符改变的消息响应处理&case WM_DEVICECHANGE:&&&&&&&& &&&&&&&& if(wParam == DBT_DEVICEARRIVAL) //设备激活&&&&&&&& {&&&&&&&&&&&& PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lP&&&&&&&&&&&& &&&&&&&&&&&& PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)&&&&&&&&&&&& &&&&&&&&&&&& char szMsg[80];&&&&&&&&&&&& sprintf (szMsg, "Drive %c: 被装载/n",&&&&&&&&&&&&&&&& FirstDriveFromMask(lpdbv -&dbcv_unitmask));&&&&&&&&&&&& &&&&&&&&&&&& MessageBox(hWnd, szMsg, "WM_DEVICECHANGE", MB_OK);&&&&&&&&&&&& &&&&&&&& }&& else if(wParam == DBT_DEVICEREMOVECOMPLETE)&& {&&& PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lP&&&&&&&&&&&& &&&&&&&&&&&& PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)&&&&&&&&&&&& &&&&&&&&&&&& char szMsg[80];&&&&&&&&&&&& sprintf (szMsg, "Drive %c: 被卸载/n",&&&&&&&&&&&&&&&& FirstDriveFromMask(lpdbv -&dbcv_unitmask));&&&&&&&&&&&& &&&&&&&&&&&& MessageBox(hWnd, szMsg, "WM_DEVICECHANGE", MB_OK);&& }&&&&&&&& &&&&&&&&&default:&&return DefWindowProc(hWnd, message, wParam, lParam);&}&return 0;}
// Message handler for about box.INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){&UNREFERENCED_PARAMETER(lParam);&switch (message)&{&case WM_INITDIALOG:&&return (INT_PTR)TRUE;
&case WM_COMMAND:&&if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)&&{&&&EndDialog(hDlg, LOWORD(wParam));&&&return (INT_PTR)TRUE;&&}&&&}&return (INT_PTR)FALSE;}
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:31034次
排名:千里之外
转载:11篇
(4)(5)(1)(3)(7)

我要回帖

更多关于 u盘好坏怎么区分 的文章

 

随机推荐