我想通过在后台运行并没有GUI的C ++应用程序检测特定(自定义)USB设备的插入/删除。
我见过很多的问题,他们的解决schemeRegisterDeviceNotification
也是MSDN上的示例代码
但是这些应用程序都有一些窗口/窗体/ GUI。 我的应用程序没有任何。 我如何在我的应用程序中使用它?
我的最后一个select是创build一个无形的窗口…但是有没有其他出路?
创建一个消息窗口 。 尽管这个名字,它实际上只是一个消息队列。
#define ANSI #define WIN32_LEAN_AND_MEAN #define _WIN32_WINNT 0x0501 #include <windows.h> #include <winuser.h> #include <Dbt.h> #include <string> #include <iostream> #include <stdexcept> #define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}} #define CLS_NAME "DUMMY_CLASS" #define HWND_MESSAGE ((HWND)-3) LRESULT message_handler(HWND__* hwnd, UINT uint, WPARAM wparam, LPARAM lparam) { switch (uint) { case WM_NCCREATE: // before window creation return true; break; case WM_CREATE: // the actual creation of the window { // you can get your creation params here..like GUID.. LPCREATESTRUCT params = (LPCREATESTRUCT) lparam; GUID InterfaceClassGuid = *((GUID*)params->lpCreateParams); DEV_BROADCAST_DEVICEINTERFACE NotificationFilter; ZeroMemory(&NotificationFilter, sizeof(NotificationFilter)); NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; NotificationFilter.dbcc_classguid = InterfaceClassGuid; HDEVNOTIFY dev_notify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); if(dev_notify == NULL) { throw std::runtime_error("Could not register for devicenotifications!"); } break; } case WM_DEVICECHANGE: { PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lparam; PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE) lpdb; std::string path; if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { path = std::string(lpdbv->dbcc_name); switch (wparam) { case DBT_DEVICEARRIVAL: std::cout << "new device connected: " << path << "\n"; break; case DBT_DEVICEREMOVECOMPLETE: std::cout << "device disconnected: " << path << "\n"; break; } } break; } } return 0L; } int main(int argc, char* argv[]) { HWND hWnd = NULL; WNDCLASSEX wx; ZeroMemory(&wx, sizeof(wx)); wx.cbSize = sizeof(WNDCLASSEX); wx.lpfnWndProc = reinterpret_cast<WNDPROC>(message_handler); wx.hInstance = reinterpret_cast<HINSTANCE>(GetmoduleeHandle(0)); wx.style = CS_HREDRAW | CS_VREDRAW; wx.hInstance = GetmoduleeHandle(0); wx.hbrBackground = (HBRUSH)(COLOR_WINDOW); wx.lpszClassName = CLS_NAME; GUID guid = HID_CLASSGUID; if (RegisterClassEx(&wx)) { hWnd = CreateWindow(CLS_NAME, "DevNotifWnd", WS_ICONIC, 0, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, NULL, GetmoduleeHandle(0), (void*)&guid); } if(hWnd == NULL) { throw std::runtime_error("Could not create message window!"); } std::cout << "waiting for new devices..\n"; MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
这个简单的程序创建一个不可见的窗口,它的消息处理程序也接收“RegisterDeviceNotification”的通知。
首先创建一个服务,并在RegisterDeviceNotification
,给出该服务的句柄,而不是一个窗口句柄。
也相应地调整RegisterDeviceNotification
的第三个参数。
此程序(C ++)检测笔式驱动器 , 存储卡和外部硬盘驱动器 (每当插入新的USB存储设备时 ) –
#include <stdio.h> #include <time.h> #include <windows.h> #include <string> using namespace std; string allDrives; char getRemovableDisk(); int main(void){ char driveLetter = getRemovableDisk(); while(1){ driveLetter = getRemovableDisk(); if(driveLetter!='0'){ printf("%c \n", driveLetter); } Sleep(1000); } return 0; } char getRemovableDisk(){ char drive='0'; char szLogicalDrives[MAX_PATH]; DWORD dwResult = GetLogicalDriveStrings(MAX_PATH, szLogicalDrives); string currentDrives=""; //cout << dwResult << endl; for(int i=0; i<dwResult; i++) { if(szLogicalDrives[i]>64 && szLogicalDrives[i]< 90) { currentDrives.append(1, szLogicalDrives[i]); if(allDrives.find(szLogicalDrives[i]) > 100) { drive = szLogicalDrives[i]; } } } allDrives = currentDrives; return drive; }
备注 :此代码段只能检测到一个新设备 (即使同时插入多个设备 )。 但是,当然,您也可以实现多重检测,只需稍作更改即可 。 🙂