// UsbHook.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "Dbt.h"
void DeviceChangeEventOpt(WPARAM wParam, LPARAM lParam)
{
switch(wParam)
{
case DBT_CONFIGCHANGECANCELED:
MessageBox(NULL,"设备改变DBT_CONFIGCHANGECANCELED","提示",MB_OK);
break;
case DBT_CONFIGCHANGED:
MessageBox(NULL,"设备改变DBT_CONFIGCHANGED","提示",MB_OK);
break;
//case DBT_CUSTOMEVENT:
// MessageBox(NULL,"设备改变DBT_CUSTOMEVENT","提示",MB_OK);
// break;
case DBT_DEVICEARRIVAL: // A device has been inserted and is now available.
MessageBox(NULL,"设备改变DBT_DEVICEARRIVAL","提示",MB_OK);
DEV_BROADCAST_HDR *stHDR;
stHDR = (DEV_BROADCAST_HDR *)lParam;
//判断设备类型
switch(stHDR->dbch_devicetype)
{
case DBT_DEVTYP_DEVNODE:
MessageBox(NULL,"设备类型 DBT_DEVTYP_DEVNODE","提示",MB_OK);
break;
case DBT_DEVTYP_NET:
MessageBox(NULL,"设备类型 DBT_DEVTYP_NET","提示",MB_OK);
break;
case DBT_DEVTYP_OEM:
MessageBox(NULL,"设备类型 DBT_DEVTYP_OEM","提示",MB_OK);
break;
case DBT_DEVTYP_PORT:
MessageBox(NULL,"设备类型 DBT_DEVTYP_PORT","提示",MB_OK);
break;
case DBT_DEVTYP_VOLUME:// Logical volume. This structure is a DEV_BROADCAST_VOLUME structure
MessageBox(NULL,"设备类型 DBT_DEVTYP_VOLUME","提示",MB_OK);
break;
}
//
break;
case DBT_DEVICEQUERYREMOVE:
MessageBox(NULL,"设备改变DBT_DEVICEQUERYREMOVE","提示",MB_OK);
break;
case DBT_DEVICEQUERYREMOVEFAILED:
MessageBox(NULL,"设备改变DBT_DEVICEQUERYREMOVEFAILED","提示",MB_OK);
break;
case DBT_DEVICEREMOVECOMPLETE:// A device has been removed.
MessageBox(NULL,"设备改变DBT_DEVICEREMOVECOMPLETE","提示",MB_OK);
break;
case DBT_DEVICEREMOVEPENDING://
MessageBox(NULL,"设备改变DBT_DEVICEREMOVEPENDING","提示",MB_OK);
break;
case DBT_DEVICETYPESPECIFIC://
MessageBox(NULL,"设备改变DBT_DEVICETYPESPECIFIC","提示",MB_OK);
break;
case DBT_QUERYCHANGECONFIG:
MessageBox(NULL,"设备改变DBT_QUERYCHANGECONFIG","提示",MB_OK);
break;
case DBT_USERDEFINED ://
MessageBox(NULL,"设备改变DBT_USERDEFINED","提示",MB_OK);
break;
}
}
HWND hwndMain;
LRESULT CALLBACK WndProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
break;
case WM_DEVICECHANGE:
DeviceChangeEventOpt(wParam,lParam);
break;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
// TODO: Place code here.
WNDCLASSEX wclass;
MSG msg;
wclass.cbClsExtra = 0;
wclass.cbSize = sizeof(WNDCLASSEX);
wclass.cbWndExtra = 0;
wclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wclass.hCursor = NULL;
wclass.hIcon = NULL;
wclass.hIconSm = NULL;
wclass.hInstance = hInstance;
wclass.lpfnWndProc =(WNDPROC)WndProc;
wclass.lpszClassName = "CheckUSB";
wclass.lpszMenuName = NULL;
wclass.style = CS_DBLCLKS;
if(!RegisterClassEx(&wclass))
{
MessageBox(NULL,"类创建失败","类创建失败",MB_OK);
}
hwndMain = CreateWindow("CheckUSB","检测USB设备",
WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 320, 300,NULL, NULL, hInstance, NULL);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg); //translate the message into its char equivelent
DispatchMessage(&msg);
}
return msg.wParam;
}
原帖来自http://search.csdn.net/Expert/topic/989/989392.xml?temp=.1137812
代码中有部分参数意思不是很明确,在参考了msdn后做了一定的改动。用U盘测试并没有什么问题。反正那几个参数标识的也不是U盘的参数。
用USB鼠标和键盘测试并没有反映,但是可以监视USB存储设备感觉已经达到要求了,有时间再改进一下