检测U盘插入、拨出状态

时间:2021-05-06 16:06:24

头文件

#include <Dbt.h>

 

关键代码:

 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2
3
4    switch (message)
5 {
6 case WM_DEVICECHANGE:
7 if (DBT_DEVICEARRIVAL==wParam )
8 MessageBox(NULL,TEXT("U盘插入"),TEXT(""),MB_ICONWARNING);
9 else if (DBT_DEVICEREMOVEPENDING==wParam || DBT_DEVICEREMOVECOMPLETE ==wParam)
10 MessageBox(NULL,TEXT("U盘拔出"),TEXT(""),MB_ICONWARNING);
11 break;
12 case WM_DESTROY:
13 PostQuitMessage(0);
14 break;
15 default:return DefWindowProc(hWnd, message, wParam, lParam);
16
17 }

在窗口消息处理函数中,响应WM_DEVICECHANGE消息。