简体   繁体   English

如何从ASP.NET应用程序中连接到服务器端USB(HID)设备?

[英]How can I connect to a Server Side USB (HID) Device from within an ASP.NET Application?

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). 我正在尝试为USB设备编写自己的控制器,而不是使用产品附带的SDK(我觉得sdk低于标准杆)。

The USB Device is plugged into the SAME SERVER that this application is running on. USB设备已插入运行此应用程序的SAME SERVER。

So I decided to head over to Nuget and grab the HidLibrary 所以我决定前往Nuget并抓住HidLibrary

PM> Install-Package hidlibrary PM> Install-Package hidlibrary

and I proceeded to follow the example found on GitHub . 然后我继续按照GitHub上的例子进行操作。

First I went into my control panel to verify the VendorID and the ProductID 首先,我进入控制面板验证VendorID和ProductID

VendorID和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 . 然后我在抓住设备的线上设置断点,但不幸的是它总是返回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. 我希望它是愚蠢的,而不是直接从IIS工作者连接到USB设备的一些突破性权限问题。

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 ). 如果您将从Hid库的GitHub浏览Mike O'Brien的代码,您将看到它调用位于以下内容的Win32 API函数:kernel32.dll,setupapi.dll,user32.dll,hid.dll( Native.cs )。

The enumeration itself it's done through setupapi.dll functions. 枚举本身是通过setupapi.dll函数完成的。 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? 所以...我认为使用匿名身份验证直接从IIS中的Web应用程序执行kernel32.dll代码是一个安全问题,不是吗?

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. 如果你真的需要与那个HID(谁知道它可能是温度传感器或其他东西)进行通信,我会做一个单独的Windows服务,IIS托管的Web应用程序将通过WCF与此服务进行通信。 This service would like a proxy. 这项服务想要代理。

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. 如果是环境,请尝试使用Process Monitor查看是否存在任何隐藏的访问错误。 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. 还要尝试枚举所有设备,而不仅仅是寻找你所追求的设备,只是为了看看你是否可以在ASP.NET中实现它。

@Chase, unless this is an experiment - it is best not to attempt connecting to a device from IIS process. @Chase,除非这是一个实验 - 最好不要尝试从IIS进程连接到设备。 [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. 最好的方法是让另一个(WCF)服务作为设备的代理,并公开您需要的服务,与您的应用程序连接。 Feel free to ask for an example if you think that would help. 如果您认为这会有所帮助,请随意提出一个例子。

I +1 @garzanti. 我+1 @garzanti。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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