Click here to Skip to main content
15,746,883 members
Articles / Programming Languages / C++
Posted 17 Jun 2023

Tagged as


34 bookmarked

How to Restart a USB Port

Rate me:
Please Sign up or sign in to vote.
5.00/5 (14 votes)
17 Jun 2023CPOL2 min read
Shows how to use IOCTL_USB_HUB_CYCLE_PORT to restart a USB port under Windows
Shows how to find a USB device in the Windows Device Management by a given Device Instance ID, determine the used USB port number, get its parent device (its USB hub), open the hub and perform the IOCTL_USB_HUB_CYCLE_PORT call.


Sometimes, USB devices stop working as expected and re-plugging them helps. Performing the re-plug by code might be handy.

If you want to try if restarting a USB port is helpful, you can try with my UsbTreeView or RestartUsbPort.


Microsoft says IOCTL_USB_HUB_CYCLE_PORT power-cycles the USB port and thus initiates a re-enumeration of the attached device.

The function is available under XP but usually only for hubs which run with the Microsoft standard driver. Third party drivers usually return ERROR_UNKNOWN_FUNCTION.

Under Vista and Windows 7, it is no more supported by the Windows standard drivers, it always fails with ERROR_NOT_SUPPORTED.

Since Windows 8, it works again but in contrast to XP, admin privileges are required. Without admin privileges, it fails as under Vista and Win7 with ERROR_NOT_SUPPORTED or since Win10 version 1903 with ERROR_GEN_FAILURE, both of which are quite misleading here since it's a privilege issue.

Using the Code

I have put everything in one single function which expects the USB device's Device Instance ID as only parameter.

bool CycleUsbDevice(char* pszUsbDeviceId)
    // Step 1: find the USB device in the device manager
    DEVINST DevInst = 0;
    if ( CR_SUCCESS != CM_Locate_DevNode(&DevInst, pszUsbDeviceId, 0) ) {
        return false; //----

    // Step 2: Determine the USB port number. 
    // Since Vista it can be read reliably from the device location info:
    char szLocation[64] = "";
    DWORD dwType = 0;
    ULONG uLen = sizeof(szLocation);
    if ( CR_SUCCESS != CM_Get_DevNode_Registry_Property
       (DevInst, CM_DRP_LOCATION_INFORMATION, &dwType, szLocation, &uLen, 0) ) {
        return false; //----

    //               0123456789
    // must be like "Port_#0004.Hub_#0014"
    if ( 0 != strncmp(szLocation, "Port_#", 6) ) {
        return false; //----

    int PortNumber = atoi(szLocation + 6); // leading zeros are ok with atoi

    // Step 3: the USB hub is the parent device
    DEVINST DevInstHub = 0;
    if ( CR_SUCCESS != CM_Get_Parent(&DevInstHub, DevInst, 0) ) {
        return false; //----
    // Step 4: scan all USB hubs for this DEVINST to get its DevicePath
    // which we need to open the hub
    char szHubDevPath[MAX_PATH] = "";


    HDEVINFO hDevInfo = SetupDiGetClassDevs

    if ( INVALID_HANDLE_VALUE == hDevInfo ) {
        return false; //----

    char DataBuf[1024];
    SP_DEVINFO_DATA                  DevInfoData = { sizeof(SP_DEVINFO_DATA) };
    DWORD                            dwSize;

    for ( DWORD dwIndex = 0; SetupDiEnumDeviceInterfaces
        (hDevInfo, NULL, DevIfGuid, dwIndex, &DevIfData); dwIndex++ ) {

        pDevIfDetailData->cbSize = 
            sizeof(*pDevIfDetailData); // yes, 5 (or 6 if UNICODE)

        dwSize = sizeof(DataBuf);
        if ( SetupDiGetDeviceInterfaceDetail(hDevInfo, 
             &DevIfData, pDevIfDetailData, dwSize, &dwSize, &DevInfoData) ) {
            if ( DevInfoData.DevInst == DevInstHub ) {
                // hub found, keep its DevicePath
                strcpy(szHubDevPath, pDevIfDetailData->DevicePath);


    if ( ! szHubDevPath[0] ) {
        return false; //----
    // Step 5: open the hub
    HANDLE hHub = CreateFile(szHubDevPath, GENERIC_WRITE, 

    if ( INVALID_HANDLE_VALUE == hHub ) {
        return false; //----

    // Step 6: call IOCTL_USB_HUB_CYCLE_PORT
    typedef struct _USB_CYCLE_PORT_PARAMS {
        ULONG ConnectionIndex;
        ULONG StatusReturned;

    USB_CYCLE_PORT_PARAMS CyclePortParams  = { PortNumber, 0 }; // in and out

    DWORD dwBytes;
    int res = DeviceIoControl(hHub, IOCTL_USB_HUB_CYCLE_PORT,
                              &CyclePortParams, sizeof(CyclePortParams), 
                              &CyclePortParams, sizeof(CyclePortParams), 
                              &dwBytes, NULL);
    return ( (0 != res) && (0 == CyclePortParams.StatusReturned) );

Points of Interest

Microsoft's documentation for IOCTL_USB_HUB_CYCLE_PORT is wrong: It says "Output buffer: None" which is wrong. The StatusReturned struct member leads to the conclusion that the USB_CYCLE_PORT_PARAMS struct is intended as in and out buffer. It works that way and dwBytes contains 8 afterwards, which is the expected sizeof(USB_CYCLE_PORT_PARAMS).

What makes it fail

In contrast to an attempt to restart a device by means of the Device Manager IOCTL_USB_HUB_CYCLE_PORT does not ask for permission, so no driver can veto it and so it always succeeds.
But the port must have a USB device attached and a driver installed. Otherwise it fails with error 433 (ERROR_NO_SUCH_DEVICE) or since Windows 10 21H2 with error 50 (ERROR_NOT_SUPPORTED) which again is missleading.
Windows 11 comes with a proper error 1167 (ERROR_DEVICE_NOT_CONNECTED).


  • 18th June, 2023 - First release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionMany thanks, highly helpful Pin
BugDigger18-Jun-23 23:40
BugDigger18-Jun-23 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.