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;
}