Our C++ application periodically checks for the presence of a USB device as a licensing mechanism. However, on Win11, USB devices now default to “Allow the computer to turn off this device to save power”. This is causing the required USB device to become invisible to our app, even though it is still plugged in.
To locate devices, we are doing this:
for ( int i = 0; i < NUM_HOST_CONTROLLERS; i++ )
{
swprintf_s ( hCName, BUFF_SIZE, L"\\.\HCD%d", i );
hDevice = CreateFileW (
hCName,
0,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL );
if ( hDevice != INVALID_HANDLE_VALUE )
{
// Do stuff here
}
}
This works at first, but once Windows has decided to suspend the USB device, subsequent calls to CreateFileW for that device return INVALID_HANDLE_VALUE. I expected the CreateFileW call itself to wake up the device, but apparently that isn’t what happens.
What’s the correct way to do this? (Bonus points if the solution works on Windows versions going back to Win7.)
Jeff Morrow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.