Kernel32.dll - DeviceIOControl returns false while trying to get String descriptor in Win 10 RRS feed

  • Question

  • I am currently using the DeviceIOControl API from kernel32.dll to get the String Descriptors of the list of connected USB devices.


        public static String GetStringDescriptor(IntPtr deviceHandle, Int32 ConnectionIndex, Byte DescriptorIndex, UInt16 LanguageID)



                Buffer.ConnectionIndex = ConnectionIndex;

                Buffer.SetupPacket.wValue = (UInt16)((USB_STRING_DESCRIPTOR_TYPE << 8) | DescriptorIndex);

                Buffer.SetupPacket.wIndex = LanguageID;

                Buffer.SetupPacket.wLength = MAXIMUM_USB_STRING_LENGTH;

                Int32 nBytesReturned;

                Boolean Status = DeviceIoControl(deviceHandle,


                        ref Buffer,


                        ref Buffer,


                        out nBytesReturned,


                if (Status)

                    return Buffer.Data.bString;


                    return null;



    We use this function to get the descriptor details such as Language ID, Serial number, Manufacturer and Product String. Only while requesting the Serial Number, the Status returned is TRUE and we get the expected values. But the status returns false for Language id, manufacturer and Product string.


    I checked the error status returned by the DeviceIoControl using:


        int error = Marshal.GetLastWin32Error();


    It returns 31 as the error code which means that the Device is not working properly/ the driver for the device is not properly installed.


    I tried all the obvious solutions like reinstalling the driver for the device and restarting the PC etc., but none seems to work. I am sure there are no issues in the device or the code because it works flawlessly in windows 7 PCs. Also, since I am able to get the serial number, I think the device handle is also proper.


    I am not able to proceed with any further debugging. Is there some update to the DeviceIoControl function in Windows 10? Or is the way to get the languageID, manufacturer and Product String changed in Windows 10?

    Tuesday, March 13, 2018 6:50 AM

All replies

  • Every time i see someone trying to hand marshal parameters in managed code my first suspicion is the type layout is incorrect. Does it fail in c/c++ ?

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Tuesday, March 13, 2018 4:32 PM
  • Thank you for the reply.

    We have tried this only in C# and not in C/C++, but is there any reason that you want us to try in C/C++?

    I think there may not be any issues with the type layout as the same code works consistently in Windows 7. It is only in Windows 10 that we are facing this issue. Please let me know your thoughts.

    Wednesday, March 14, 2018 6:27 AM
  • Then perhaps the underlying usb stack changed how the buffers are validated or data from the device is treated differently. Try capturing a usb etw trace and see what it says

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Wednesday, March 14, 2018 2:06 PM
  • How is USB_DESCRIPTOR_REQUEST declared, exactly?  In the C++ case, that's a variable-length structure.  The final member is a zero-length array, and the user has to allocate space after it to hold the string.  That doesn't work in C#.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, March 15, 2018 5:01 AM