WinUsb_Initialize Returns Error Code 8 in C# RRS feed

  • Question

  • I am trying to connect to a USB device. Here is my code:

            public override async Task InitializeAsync()
                if (string.IsNullOrEmpty(DeviceId))
                    throw new WindowsException($"{nameof(DeviceDefinition)} must be specified before {nameof(InitializeAsync)} can be called.");
                _DeviceHandle = APICalls.CreateFile(DeviceId, (APICalls.GenericWrite | APICalls.GenericRead), APICalls.FileShareRead | APICalls.FileShareWrite, IntPtr.Zero, APICalls.OpenExisting, APICalls.FileAttributeNormal | APICalls.FileFlagOverlapped, IntPtr.Zero);
                var errorCode = Marshal.GetLastWin32Error();
                if (errorCode > 0) throw new Exception($"Write handle no good. Error code: {errorCode}");
                if (_DeviceHandle.IsInvalid) throw new Exception("Device handle no good");
                var isSuccess = WinUsbApiCalls.WinUsb_Initialize(_DeviceHandle, out var interfaceHandle);
                errorCode = Marshal.GetLastWin32Error();
                if (!isSuccess) throw new Exception($"Initialization failed. Error code: {errorCode}");

     I am able to get a handle on the device with CreateFile, but WinUsb_Initialize fails with an error code of 8 (ERROR_NOT_ENOUGH_MEMORY). Why? The documentation ( says "Indicates that there is insufficient memory to perform the operation. ". How should I allocate the memory for it? I've tried this, but it does nothing:

    var pointerToBuffer = Marshal.AllocHGlobal(126);
    Thursday, December 27, 2018 3:15 AM