#include <dbt.h>
#include <stdio.h>
#include <math.h>
LRESULT CALLBACK WndProc(HWND h, UINT msg, WPARAM wp, LPARAM lp)
{
if (msg == WM_DEVICECHANGE) {
if ((DWORD)wp == DBT_DEVICEARRIVAL) {
DEV_BROADCAST_VOLUME* p = (DEV_BROADCAST_VOLUME*) lp;
if (p->dbcv_devicetype == DBT_DEVTYP_VOLUME) {
int l = (int)(log(double(p->dbcv_unitmask)) / log(double(2)));
printf("啊……%c盘插进来了\n", 'A' + l);
}
} else if ((DWORD)wp == DBT_DEVICEREMOVECOMPLETE) {
DEV_BROADCAST_VOLUME* p = (DEV_BROADCAST_VOLUME*) lp;
if (p->dbcv_devicetype == DBT_DEVTYP_VOLUME) {
int l = (int)(log(double(p->dbcv_unitmask)) / log(double(2)));
printf("啊……%c盘被拔掉了\n", 'A' + l);
}
}
return TRUE;
} else return DefWindowProc(h, msg, wp, lp);
}
int main() {
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.lpszClassName = TEXT("myusbmsg");
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
HWND h = CreateWindow(TEXT("myusbmsg"), TEXT(""), 0, 0, 0, 0, 0,
0, 0, GetModuleHandle(0), 0);
MSG msg;
while( GetMessage(&msg, 0, 0, 0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}