8

I'm trying to write my own controller for a USB device instead of using the SDK that comes with the product (I feel the sdk is sub-par).

The USB Device is plugged into the SAME SERVER that this application is running on.

So I decided to head over to Nuget and grab the HidLibrary

PM> Install-Package hidlibrary

and I proceeded to follow the example found on GitHub.

First I went into my control panel to verify the VendorID and the ProductID

VendorID and ProductID

And I dropped it into my code.

Then I set a breakpoint on the line that grabs the device, but unfortunately it always comes back null.

using HidLibrary;
public class MyController : ApiController
{
 private const int VendorId = 0x0BC7;
 private const int ProductId = 0x0001;
 private static HidDevice _device;
 // POST api/<controller>
 public string Post(CommandModel command)
 {
 _device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
 if (_device != null)
 {
 // getting here means the device exists
 }
 else
 {
 // ending up here means the device doesn't exist
 throw new Exception("device not connected");
 }
 return null;
 }

I'm hoping it's something silly, and not some deal-breaking permissions issue regarding connecting to a USB device directly from an IIS worker.

asked Nov 16, 2012 at 20:55
2
  • You could try running your IIS worker process (and authenticated user account) as the local Administrator account, just to rule out permissions issues. Commented Nov 27, 2012 at 23:55
  • The idea behind this project is to build a purpose build "appliance" (for lack of a better term) that exposes a webUI on the front end, and controls hardware for automation purposes... similar to what Crestron does on their AV2 hardware. Commented Dec 3, 2012 at 21:43

3 Answers 3

9
+100

Despite your hopes to be something silly, it is not. You have some deal-breaking permission issues. If you will browse Mike O'Brien's code from GitHub of the Hid Library you will see that it calls Win32 API functions located in: kernel32.dll, setupapi.dll, user32.dll, hid.dll (Native.cs).

The enumeration itself it's done through setupapi.dll functions. It browse all the installed devices and filters what it's need it.

So... I think it's a security issue to execute kernel32.dll code directly from a web-app in IIS with anonymous authentication, don't you?

If you really need to communicate with that HID (who knows maybe it's a temperature sensor, or something else) I would do a separate Windows service and the IIS hosted web app would communication through WCF with this service. This service would like a proxy.

answered Nov 28, 2012 at 15:20
Sign up to request clarification or add additional context in comments.

4 Comments

I agree with the service approach - it's the practice I've always used for scenarios such as this.
please see the comment I posted on the original question.
"appliance" is a good term, and there is a Windows 2008 Embedded version for this kind of scenarios. But even so, WebUI should be isolated from the hardware services.
agreed. I'll look into this (haven't had a chance in the past few days). I was hoping for simple deployment for people who will want to use the project (It'll be up on github), and having to deploy the WebUI AND the Windows Service was sorta more effort for end users than I was going for... but it'll have to be that way I suppose.
5

Put the same code in a console application and run it. That will help you verify if it's your code or environment.

If it's environment, try using Process Monitor to see if there are any hidden access errors. Also try enumerating all devices, not just looking for the one device you're after, just to see if you can do it in ASP.NET.

answered Nov 28, 2012 at 0:00

Comments

1

@Chase, unless this is an experiment - it is best not to attempt connecting to a device from IIS process. [It's a Pandora's box if you start down this path].

Best way to do this is to have another (WCF) service as proxy to the device and expose just what you need out of the service, hook it up with your app. Feel free to ask for an example if you think that would help.

I +1 @garzanti.

answered Dec 3, 2012 at 21:20

1 Comment

please see the comment I posted on the original question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.