winusb_starttrackingfortimesync fails

Yu Huang 1 Reputation point
2021-12-06T12:58:49.163+00:00

How to get winusb_starttrackingfortimesync working? I am running Win11 with latest SDK for VS2019. GetLastError returns 122 for failed winusb_starttrackingfortimesync. I tried to set TimeTrackingHandle to INAVLID_HANDLE_VALUE or NULL and set IsStartupDelayTolerable to TRUE and FALSE. None of the combinations works.

Windows Hardware Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,579 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yu Huang 1 Reputation point
    2021-12-08T13:38:14.963+00:00

    include <windows.h>

    include <initguid.h>

    include <setupapi.h>

    include <string.h>

    include <stdio.h>

    include <conio.h>

    include <usbiodef.h>

    include <usbioctl.h>

    include <winusb.h>

    include <Cfgmgr32.h>

    int main(int argc, char** argv)
    {
    GUID classGuid = GUID_DEVINTERFACE_USB_DEVICE;
    SP_DEVICE_INTERFACE_DATA devInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDevInterfaceDetailData;
    DWORD devInterfaceDetailDataSize;
    char devicePath[256] = { 0 };
    int index = 0;

    if (argc > 1) index = atoi(argv[1]);
    
    HDEVINFO devInfoSet = SetupDiGetClassDevs(&classGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (devInfoSet == INVALID_HANDLE_VALUE)
    {
        SetupDiDestroyDeviceInfoList(devInfoSet);
        printf("Error with SetupDiGetClassDevs");
        return -1;
    }
    
    devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    for (int i = index; SetupDiEnumDeviceInterfaces(devInfoSet, NULL, &classGuid, i, &devInterfaceData); i++)
    {
        devInterfaceDetailDataSize = 0;
        SetupDiGetDeviceInterfaceDetail(devInfoSet, &devInterfaceData, NULL, 0, &devInterfaceDetailDataSize, NULL);
    
        pDevInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_A)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, devInterfaceDetailDataSize);
        if (!pDevInterfaceDetailData)
        {
            SetupDiDestroyDeviceInfoList(devInfoSet);
            printf("Error allocating SP_DEVICE_INTERFACE_DETAIL_DATA");
            return -1;
        }
    
        pDevInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
        if (!SetupDiGetDeviceInterfaceDetail(devInfoSet, &devInterfaceData, pDevInterfaceDetailData, devInterfaceDetailDataSize, NULL, NULL))
        {
            SetupDiDestroyDeviceInfoList(devInfoSet);
            HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
            printf("Error retrieving SP_DEVICE_INTERFACE_DETAIL_DATA");
            return -1;
        }
    
        if (argc > 1)
        {
            strcpy_s(devicePath, 256, pDevInterfaceDetailData->DevicePath);
            break;
        }
        else printf("USB %i: %s\n", i, pDevInterfaceDetailData->DevicePath);
    
        HeapFree(GetProcessHeap(), NULL, pDevInterfaceDetailData);
    }
    SetupDiDestroyDeviceInfoList(devInfoSet);
    
    if (devicePath[0] != 0)
    {
        HANDLE deviceHandle;
        WINUSB_INTERFACE_HANDLE usbHandle;
        DWORD bytesReturned;
        USB_START_TRACKING_FOR_TIME_SYNC_INFORMATION trackStart;
        USB_STOP_TRACKING_FOR_TIME_SYNC_INFORMATION trackStop;
    
        printf("open :%s\n", devicePath);
    
        deviceHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
        if (deviceHandle == INVALID_HANDLE_VALUE)
        {
            printf("Can't open USB device. Program ended.\n");
            return -1;
        }
    
        if (!WinUsb_Initialize(deviceHandle, &usbHandle))
        {
            CloseHandle(deviceHandle);
            printf("Can't open USB device. Program ended.\n");
            return -1;
        }
    
        trackStart.TimeTrackingHandle = INVALID_HANDLE_VALUE;
        trackStart.IsStartupDelayTolerable = TRUE;
        if (!WinUsb_StartTrackingForTimeSync(usbHandle, &trackStart))
        {
            printf("Can't start USB time tracking (%i)\n", ::GetLastError());
            WinUsb_Free(usbHandle);
            CloseHandle(deviceHandle);
            return -1;
        }
    
        trackStop.TimeTrackingHandle = trackStart.TimeTrackingHandle;
        WinUsb_StopTrackingForTimeSync(usbHandle, &trackStop);
        WinUsb_Free(usbHandle);
        CloseHandle(deviceHandle);
    }
    
    return 0;
    

    }