windos usb 监听

#include 
#include 
#include 
#include 
#include 
//创建回调函数,与窗口回调相联
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)));
				std::cout<<'A' + l <<" 入系统"<dbcv_devicetype == DBT_DEVTYP_VOLUME) {
				int l = (int)(log(double(p->dbcv_unitmask)) / log(double(2)));
				std::cout<<'A' + l <<" 出系统"< 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

1、windowsU口探测事件

设备事件:
WM_DEVICECHANGE
首先要创建一个窗口,把设备事件和窗口关联

HWND h = CreateWindow(TEXT("USB_EVENT"), TEXT(""), 0, 0, 0, 0, 0,
		0, 0, GetModuleHandle(0), 0);

创建回调函数,与窗口回调相联

LRESULT CALLBACK WndProc(HWND h, UINT msg, WPARAM wp, LPARAM lp)

你可能感兴趣的:(C++,c++,开发语言)