WHY THE CODE PRINTF("LINE 29") WON'T RUN

鑫 陈 0 Reputation points
2023-07-28T06:37:38.6133333+00:00
BOOL OpenUsbDevice(HANDLE* handle, WINUSB_INTERFACE_HANDLE* winusbHandle) {
    // ENUM DVICE
    printf("line 18 ");
    GUID guid = { 0x4d36e978, 0xe325, 0x11ce, {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18} };
    HDEVINFO devInfo;
    devInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (devInfo == INVALID_HANDLE_VALUE)
        printf("info null ");
    // FIND DEVICE
    SP_DEVICE_INTERFACE_DATA interfaceData;
    interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    DWORD memberIndex = 0;

    while (SetupDiEnumDeviceInterfaces(devInfo, NULL, &guid, memberIndex, &interfaceData)) {
        printf("line 29 ");// 获取接口详细信息
        SP_DEVICE_INTERFACE_DETAIL_DATA* detailData;
        DWORD bufferSize = 0;
        SetupDiGetDeviceInterfaceDetail(devInfo, &interfaceData, NULL, 0, &bufferSize, NULL);

        detailData = (SP_DEVICE_INTERFACE_DETAIL_DATA*)malloc(bufferSize);
        detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        SP_DEVINFO_DATA devData;
        devData.cbSize = sizeof(SP_DEVINFO_DATA);

        if (SetupDiGetDeviceInterfaceDetail(devInfo, &interfaceData, detailData, bufferSize, NULL, &devData)) {
            // OPEN
            *handle = CreateFile(detailData->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
            if (*handle == INVALID_HANDLE_VALUE) {
                // IF FAILED
                free(detailData);
                memberIndex++;
                continue;
            }

            //GET HANDLE
            if (!WinUsb_Initialize(*handle, winusbHandle)) {
                CloseHandle(*handle);
                free(detailData);
                memberIndex++;
                continue;
            }

            // CHECK VID PID
            USB_DEVICE_DESCRIPTOR deviceDescriptor;
            if (WinUsb_GetDescriptor(*winusbHandle, USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, (PBYTE)&deviceDescriptor, sizeof(deviceDescriptor), NULL)) {
                if (deviceDescriptor.idVendor == USB_VENDOR_ID && deviceDescriptor.idProduct == USB_PRODUCT_ID) {
                    free(detailData);
                    SetupDiDestroyDeviceInfoList(devInfo);
                    return TRUE;
                }
            }

            // ONCE MATCH FAILED,FREE THE HANDLE
            WinUsb_Free(*winusbHandle);
            CloseHandle(*handle);
        }

        free(detailData);
        memberIndex++;
    }

    SetupDiDestroyDeviceInfoList(devInfo);
    return FALSE;
}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,358 questions
{count} votes