2

I keep getting the Windows error 87 while calling the SetupDiGetDeviceInterfaceDetail (https://msdn.microsoft.com/en-us/library/ff551120.aspx), I keep getting the error even when I switch all the optional parameters to NULL, but I use the two first parameters in another function which works, so I don't understand what's incorrect about them.

HDEVINFO hdiInfo = SetupDiGetClassDevsW(&guid, NULL, NULL, 0x12);

SP_DEVICE_INTERFACE_DATA hidDevIData = SP_DEVICE_INTERFACE_DATA();


hidDevIData.cbSize = sizeof(hidDevIData);
bool isValid = SetupDiEnumDeviceInterfaces(hdiInfo, 0, &guid, i, &hidDevIData);

if (isValid)
{
    DWORD dwLength;
    PSP_DEVICE_INTERFACE_DETAIL_DATA hidDevIDetailData = PSP_DEVICE_INTERFACE_DETAIL_DATA();
    hidDevIData.cbSize = 8;
    hidDevIDetailData = new SP_DEVICE_INTERFACE_DETAIL_DATA();
    hidDevIDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

    SetupDiGetDeviceInterfaceDetail(hdiInfo, &hidDevIData, NULL, 0, &dwLength, NULL); //ERROR 87 : ERROR_INVALID_PARAMETER
}

I really don't know which parameter could be incorrect, as all the previous functions returns true (so the GUID is valid for example).

sh5164
  • 276
  • 2
  • 15
  • In `hidDevIData.cbSize = 8;` why did you put magic constant as a size? You didn't use the same parameters in those 2 calls. – Algirdas Preidžius May 19 '17 at 13:47
  • You might want to verify that the return value of SetupDiEnumDeviceInterfaces() is true before continuing execution. If for some reason this call returned false, your hidDevIData might contain invalid data to be passed to SetupDiGetDeviceInterfaceDetail() – Jorge Torres May 19 '17 at 13:53
  • @algirdas-preidžius The thing is putting 8 on both throws an error 1784 (The supplied user buffer is not valid for the requested operation) for SetupDiEnumDeviceInterfaces, and putting sizeof(hidDevIData) on both just makes the program crash. Like "myprogram.exe has just stopped". Plus this is an adaptation of a C# code for a particular USB device, so I try to respect the values as much as I can. – sh5164 May 19 '17 at 13:55
  • @JorgeTorres I didn't write if it returned true to lighten the code in the post but it does. I edit my question to make that clearer. – sh5164 May 19 '17 at 13:55

1 Answers1

1

For those struggling with the same issue, you just have to put the cbSize to sizeof(object):

hidDevIData.cbSize = sizeof(hidDevIData);
hidDevIDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
gsamaras
  • 71,951
  • 46
  • 188
  • 305
sh5164
  • 276
  • 2
  • 15