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.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • You could try running your IIS worker process (and authenticated user account) as the local Administrator account, just to rule out permissions issues. – Dai Nov 27 '12 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. – Chase Florell Dec 03 '12 at 21:43

3 Answers3

10

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.

garzanti
  • 2,162
  • 1
  • 22
  • 30
  • 1
    I agree with the service approach - it's the practice I've always used for scenarios such as this. – LewisBenge Dec 03 '12 at 04:52
  • please see the comment I posted on the original question. – Chase Florell Dec 03 '12 at 21:44
  • "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. – garzanti Dec 04 '12 at 04:11
  • 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. – Chase Florell Dec 04 '12 at 16:09
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.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
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.

Bobby
  • 251
  • 1
  • 8