简体   繁体   中英

Visual Studio C# .exe runs, but USB HID device quits working

When I run my C# application in Visual Studio 2013 I can communicate with my custom USB HID device reliably.

When I run the .exe (release or debug) directly, the app runs, but there is no communication with the USB HID device.

I know there are manifest additions required to communicate with App Store applications, but I am building a Windows Forms app. I can't find any details on enabling USB permission for Windows Forms apps.

Without access to the debugger I don't know where to start looking.

VS是否有可能以admin身份运行,而您以非admin身份运行exe(这意味着该exe不具有提升的特权?)您是否可以发布代码或获取异常?

I found a solution, but I am not really happy with it. It appears to be timing related. It was not permission related.

I write a USB HID packet to my device, then read a response packet.

When running the .exe outside of Visual Studio it somehow thinks there is a response immediately after sending the write packet.

If I put:

System.Threading.Thread.Sleep(1)

between the write packet command and the read packet command it functions just like it does inside Visual Studio.

Is this enough for anyone to guess the root cause?

Write report code:

var success = Kernel32.WriteFile(
                deviceInformation.WriteHandle,
                outputReportBuffer,
                outputReportBuffer.Length,
                ref numberOfBytesWritten,
                IntPtr.Zero);

Debug.WriteLine(success ?"usbGenericHidCommunication:writeReportToDevice(): -> Write report succeeded"
                                : "usbGenericHidCommunication:writeReportToDevice(): -> Write report failed!");

Read Report Code:

nonManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
nonManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));
Marshal.StructureToPtr(hidOverlapped, nonManagedOverlapped, false);

// Read the input report buffer
   Debug.WriteLine("usbGenericHidCommunication:readReportFromDevice(): -> Attempting to ReadFile");
success = Kernel32.ReadFile(
                deviceInformation.ReadHandle,
                nonManagedBuffer,
                inputReportBuffer.Length,
                ref numberOfBytesRead,
                nonManagedOverlapped);

            if (!success)
            {
                // We are now waiting for the FileRead to complete
                Debug.WriteLine(
                    "usbGenericHidCommunication:readReportFromDevice(): -> ReadFile started, waiting for completion...");

                // Wait a maximum of 3 seconds for the FileRead to complete
                var result = Kernel32.WaitForSingleObject(eventObject, 3000);

                switch (result)
                {
                        // Has the ReadFile completed successfully?
                    case Constants.WaitObject0:

                        // Get the number of bytes transferred
                        Kernel32.GetOverlappedResult(deviceInformation.ReadHandle, nonManagedOverlapped, ref numberOfBytesRead, false);

                        Debug.WriteLine("usbGenericHidCommunication:readReportFromDevice(): -> ReadFile successful (overlapped) {0} bytes read", numberOfBytesRead);
                        break;

                        // Did the FileRead operation timeout?
                    case Constants.WaitTimeout:

                        // Cancel the ReadFile operation
                        Kernel32.CancelIo(deviceInformation.ReadHandle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM