使用优雅的win32访问USB HID设备调用CreateFile和ReadFile [英] Access USB HID device using the elegant win32 calls CreateFile and ReadFile

查看:718
本文介绍了使用优雅的win32访问USB HID设备调用CreateFile和ReadFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

微软目前投入的努力是为了向后兼容UWP等新技术,我感到鼓舞。

I am encouraged by the amount of effort Microsoft is investing these days for backward compatibility in new technologies like UWP.

我们有自定义USB HID设备和非托管代码C ++ DLL和VB6中的UI与它进行交互。

We have a custom USB HID device and unmanaged code in C++ dll and a UI in VB6 to interact with it.

我们已经将VB6代码现代化为VB.Net UWP。但我们没有将非托管代码移植到托管代码的专业知识(因为它是希腊语和拉丁语)。非托管代码使用CreateFile和ReadFile与HID设备进行交互。新的UWP代码使用
PInvoke与我们的C ++ dll进行交互。我们的软件是侧载的。

We have modernized the VB6 code to VB.Net UWP. But we don't have the expertise to port the unmanaged code to managed code (because it is Greek and Latin). The unmanaged code uses CreateFile and ReadFile to interact with the HID device. The new UWP code uses PInvoke to interact with our C++ dll. Our software is side loaded.

我们正在使用此hack"授予对AppContainer进程的访问权限"  链接使CreateFile和ReadFile成功。

We are using this hack "Granting Access to AppContainer Processes" link to make CreateFile and ReadFile succeed.

但这非常麻烦,必须在每台计算机上完成。 对于无法学习新技巧的VB6 / C ++老狗,没办法!

But this is very cumbersome and it has to be done on each computer.  Is there no way for VB6/C++ old dogs that are unable to learn new tricks!

推荐答案

Hi
Sibi Sar

  >>
我们没有将非托管代码移植到托管代码的专业知识 

 >>But we don't have the expertise to port the unmanaged code to managed code 

如果您想使用托管代码访问HID设备,您可以查看" Windows.Devices.HumanInterfaceDevice "
NameSpace。此命名空间使您的UWP应用程序可以访问支持人机接口设备(HID)协议的设备。

您可以使用
< span style ="font-size:10.5pt; font-family:'Segoe UI',sans-serif"> HidDevice Class
打开与HID设备的连接。像这样:


 

        private async void EnumerateHidDevices()
        {
            UInt32 vendorId = 0x045E;
            UInt32 productId = 0x078F;
            UInt32 usagePage = 0xFF00;
            UInt32 usageId = 0x0001;

            // Create a selector that gets a HID device using VID/PID and a
            // VendorDefined usage
            string selector = HidDevice.GetDeviceSelector(usagePage, usageId,
                              vendorId, productId);

            // Enumerate devices using the selector
            var devices = await DeviceInformation.FindAllAsync(selector);

            if (devices.Count > 0)
            {
                // Open the target HID device
                HidDevice device = await HidDevice.FromIdAsync(devices.ElementAt(0).Id,
                                   FileAccessMode.ReadWrite);

                // At this point the device is available to communicate with
                // So we can send/receive HID reports from it or
                // query it for control descriptions
            }
            else
            {
                // There were no HID devices that met the selector criteria
                this.NotifyUser("MUTT HID device not found");
            }
        }




如果您仍想在UWP应用程序中使用C ++代码,您可能需要查看:
如何:使用现有的C ++通用Windows平台应用程序中的代码

祝你好运,

Roy


这篇关于使用优雅的win32访问USB HID设备调用CreateFile和ReadFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆