Detect USB device insertion removal using C++ (Cpp)

 These type of programs fall under the category winapi. Detection of usb device whenever they are inserted or removed using C or C++ is not very hard and can be coded in less than 100 lines of code.

Whenever a usb device is inserted into a usb port, the system generates one or more notification message(s) which contains information about the device being inserted or removed. The goal of our C++ program is to intercept this message. To intercept usb insertion/removal message, our program needs to register to receive this message using RegisterDeviceNotification.

The steps involved are as follows:

    1. Identify the GUID of the device or class of devices that you want to get notification.

Click here to view GUID of some of the device classes.

In our program we want to get notifications for usb devices so the GUID we will use is { 25dbce51-6c8f-4a72-8a6d-b54c2b4fc835 }

    2. Specify properties of the window that will receive device change notification. Properties are specified by using WNDCLASSEX

    3. Create a Console window handle HWND to be later used while registering to get usb change notification. HWND handle is generated by creating a window.

    4. Register to receive the hardware change notification using GUID of class of device(s). In our case the GUID used is for usb devices.

So here is the code:

#include<windows.h>
#include<iostream>
#include<Dbt.h>

using namespace std;

GUID WceusbshGUID = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 };

LRESULT message_handler(HWND__* hWnd, UINT message, WPARAM wParam, LPARAM lParam);//forward declaration

void RegisterListener()
{
HWND hWnd = NULL;
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = 0; // Class styles
wcx.lpfnWndProc = (WNDPROC)message_handler; // Pointer to the callback procedure
wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure
wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure
wcx.hInstance = GetModuleHandle(NULL); // Instance of the application
wcx.hIcon = NULL; // Class Icon
wcx.hCursor = NULL; // Class Cursor
wcx.hbrBackground = NULL; // Background brush
wcx.lpszMenuName = NULL; // Menu resource
wcx.lpszClassName = L"USB"; // Name of this class
wcx.hIconSm = NULL; // Small icon for this class
if (!RegisterClassEx(&wcx))
{
return;
}
hWnd = CreateWindowEx(0,// Extended window style
L"USB", // Window class name
L"", // Window title
WS_POPUP, // Window style
0, 0, // (x,y) pos of the window
0, 0, // Width and height of the window
NULL, // HWND of the parent window (can be null also)
NULL, // Handle to menu
GetModuleHandle(NULL), // Handle to application instance
NULL); // Pointer to window creation data
// Check if window creation was successful
if (!hWnd)
return;
// Make the window invisible
ShowWindow(hWnd, SW_HIDE);
std::cout << "waiting for new devices..\n";
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

LRESULT message_handler(HWND__* hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch
(message)
{
case WM_CREATE:
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = WceusbshGUID;
HDEVNOTIFY dev_notify = RegisterDeviceNotification(
hWnd, // events recipient
&NotificationFilter, // type of device
DEVICE_NOTIFY_WINDOW_HANDLE // type of recipient handle
);
if (NULL == dev_notify)
{
std::cout << "RegisterDeviceNotification Failed";
}
}
break;
case
WM_DEVICECHANGE:
{
PDEV_BROADCAST_DEVICEINTERFACE pdbd = (PDEV_BROADCAST_DEVICEINTERFACE)lParam;
switch (wParam)
{
case DBT_DEVICEARRIVAL:
std::cout << "Device Inserted\n";
break;
case DBT_DEVICEREMOVECOMPLETE:
std::cout << "Device Removed\n";
break;
}
}
}
return true;
}

int main()
{
RegisterListener();
}
Do you want me to write about how to detect the drives( e: / f: etc.) which gets created when a Usb pen drive is inserted? Let me know in the comments below.

Comments