libusbK 
3.0
Library Documentation
© 2011-2021 Travis Lee Robinson. All rights reserved.
hot-plug-monitor.c

Registers a hot plug handle for USB device notification of any supported device.

hot-plug-monitor example:
  1. Initializes a new HotK (hot-plug) handle.
  2. Writes arrival/removal event notifications to console output as they occur.
  3. Repeats step #2 until the Q key is pressed.
  4. Frees the HotK (hot-plug) handle created in step #1
Console Output
Initialize a HotK device notification event monitor..
Looking for devices with instances IDs matching the pattern '*'..
Press 'q' to exit..

HotK monitor initialized. ErrorCode: 00000000h

[ARRIVAL] Benchmark Device (Microchip Technology, Inc.) [libusbK]
  InstanceID          : USB\VID_04D8&PID_FA2E\LUSBW1
  DeviceInterfaceGUID : {716cdf1f-418b-4b80-a07d-1311dffdc8b8}
  DevicePath          : \\?\USB#VID_04D8&PID_FA2E#LUSBW1#{716cdf1f-418b-4b80-a07d-1311dffdc8b8}


[REMOVAL] Benchmark Device (Microchip Technology, Inc.) [libusbK]
  InstanceID          : USB\VID_04D8&PID_FA2E\LUSBW1
  DeviceInterfaceGUID : {716cdf1f-418b-4b80-a07d-1311dffdc8b8}
  DevicePath          : \\?\USB#VID_04D8&PID_FA2E#LUSBW1#{716cdf1f-418b-4b80-a07d-1311dffdc8b8}

HotK monitor closed. ErrorCode: 00000000h
#include "examples.h"
KHOT_HANDLE Handle,
KLST_DEVINFO_HANDLE DeviceInfo,
KLST_SYNC_FLAG NotificationType)
{
UNREFERENCED_PARAMETER(Handle);
// Write arrival/removal event notifications to console output as they occur.
printf(
"\n"
"[%s] %s (%s) [%s]\n"
" InstanceID : %s\n"
" DeviceInterfaceGUID : %s\n"
" DevicePath : %s\n"
" \n",
NotificationType == KLST_SYNC_FLAG_ADDED ? "ARRIVAL" : "REMOVAL",
DeviceInfo->DeviceDesc,
DeviceInfo->Mfg,
DeviceInfo->Service,
DeviceInfo->DeviceID,
DeviceInfo->DeviceInterfaceGUID,
DeviceInfo->DevicePath);
}
DWORD __cdecl main(int argc, char* argv[])
{
DWORD errorCode = ERROR_SUCCESS;
KHOT_HANDLE hotHandle = NULL;
KHOT_PARAMS hotParams;
CHAR chKey;
memset(&hotParams, 0, sizeof(hotParams));
hotParams.OnHotPlug = OnHotPlug;
// A "real world" application should set a specific device interface guid if possible.
// strcpy(hotParams.PatternMatch.DeviceInterfaceGUID, "{F676DCF6-FDFE-E0A9-FC12-8057DBE8E4B8}");
strcpy(hotParams.PatternMatch.DeviceInterfaceGUID, "*");
printf("Initialize a HotK device notification event monitor..\n");
printf("Looking for 'DeviceInterfaceGUID's matching the pattern '%s'..\n", hotParams.PatternMatch.DeviceInterfaceGUID);
// Initializes a new HotK handle.
if (!HotK_Init(&hotHandle, &hotParams))
{
errorCode = GetLastError();
printf("HotK_Init failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("HotK monitor initialized successfully!\n");
printf("Press 'q' to exit...\n\n");
for(;;)
{
if (_kbhit())
{
chKey = (CHAR)_getch();
if (chKey == 'q' || chKey == 'Q')
break;
chKey = '\0';
continue;
}
Sleep(100);
}
// Free the HotK handle.
if (!HotK_Free(hotHandle))
{
errorCode = GetLastError();
printf("HotK_Free failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("HotK monitor closed successfully!\n");
Done:
return errorCode;
}