如何在Visual Studio 2017项目中包含Windows.Devices.HumanInterfaceDevice.dll

时间:2019-03-27 04:17:55

标签: .net visual-studio

我想在Visual Studio的C#项目中使用HID Device class

我正在尝试将dll添加到参考购买中,右键单击解决方案资源管理器中的参考部分>添加参考,然后浏览到参考位置(C:/Windows/System32/Windows.Devices.HumanInterfaceDevice.dll) 。我收到一个错误消息,询问我是否对该文件具有正确的权限,或者该文件是否为有效的COM程序集。

我应该如何正确导入此dll?

1 个答案:

答案 0 :(得分:0)

您在此处引用的类是UWP类。如果您有UWP项目,它将自动显示在您的项目中,但是我怀疑您没有在从事UWP项目。

以下是在包装库中使用隐藏设备类的示例: https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Hid.Net.UWP/UWPHidDevice.cs。请注意,如果要将Hid与UWP一起使用,则需要在软件包清单中定义设备。参见here

但是,如果您不使用UWP,例如.NET core,则需要使用其他类或库来访问Hid设备。这是包装Windows API的类

代码:https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Hid.Net/Windows/HidAPICalls.cs

public static class HidAPICalls
    {
        #region Private Static Fields
        private static Guid? _HidGuid;
        #endregion

        #region Constants
        private const int HIDP_STATUS_SUCCESS = 0x110000;
        #endregion

        #region API Calls

        [DllImport("hid.dll", SetLastError = true)]
        private static extern bool HidD_GetPreparsedData(SafeFileHandle hidDeviceObject, out IntPtr pointerToPreparsedData);

        [DllImport("hid.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        private static extern bool HidD_GetManufacturerString(SafeFileHandle hidDeviceObject, IntPtr pointerToBuffer, uint bufferLength);

        [DllImport("hid.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        private static extern bool HidD_GetProductString(SafeFileHandle hidDeviceObject, IntPtr pointerToBuffer, uint bufferLength);

        [DllImport("hid.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
        private static extern bool HidD_GetSerialNumberString(SafeFileHandle hidDeviceObject, IntPtr pointerToBuffer, uint bufferLength);

        [DllImport("hid.dll", SetLastError = true)]
        private static extern int HidP_GetCaps(IntPtr pointerToPreparsedData, out HidCollectionCapabilities hidCollectionCapabilities);

        [DllImport("hid.dll", SetLastError = true)]
        private static extern bool HidD_GetAttributes(SafeFileHandle hidDeviceObject, out HidAttributes attributes);

        [DllImport("hid.dll", SetLastError = true)]
        private static extern void HidD_GetHidGuid(out Guid hidGuid);

        [DllImport("hid.dll", SetLastError = true)]
        private static extern bool HidD_FreePreparsedData(ref IntPtr pointerToPreparsedData);

        private delegate bool GetString(SafeFileHandle hidDeviceObject, IntPtr pointerToBuffer, uint bufferLength);

        #endregion

        #region Private Static Methods
        private static string GetHidString(SafeFileHandle safeFileHandle, GetString getString)
        {
            var pointerToBuffer = Marshal.AllocHGlobal(126);
            var isSuccess = getString(safeFileHandle, pointerToBuffer, 126);
            Marshal.FreeHGlobal(pointerToBuffer);
            WindowsDeviceBase.HandleError(isSuccess, "Could not get Hid string");
            return Marshal.PtrToStringUni(pointerToBuffer);
        }
        #endregion

        #region Public Static Methods
        public static HidAttributes GetHidAttributes(SafeFileHandle safeFileHandle)
        {
            var isSuccess = HidD_GetAttributes(safeFileHandle, out var hidAttributes);
            WindowsDeviceBase.HandleError(isSuccess, "Could not get Hid Attributes");
            return hidAttributes;
        }

        public static HidCollectionCapabilities GetHidCapabilities(SafeFileHandle readSafeFileHandle)
        {
            var isSuccess = HidD_GetPreparsedData(readSafeFileHandle, out var pointerToPreParsedData);
            WindowsDeviceBase.HandleError(isSuccess, "Could not get pre parsed data");

            var result = HidP_GetCaps(pointerToPreParsedData, out var hidCollectionCapabilities);
            if (result != HIDP_STATUS_SUCCESS)
            {
                throw new Exception($"Could not get Hid capabilities. Return code: {result}");
            }

            isSuccess = HidD_FreePreparsedData(ref pointerToPreParsedData);
            WindowsDeviceBase.HandleError(isSuccess, "Could not release handle for getting Hid capabilities");

            return hidCollectionCapabilities;
        }

        public static string GetManufacturer(SafeFileHandle safeFileHandle)
        {
            return GetHidString(safeFileHandle, HidD_GetManufacturerString);
        }

        public static string GetProduct(SafeFileHandle safeFileHandle)
        {
            return GetHidString(safeFileHandle, HidD_GetProductString);
        }

        public static string GetSerialNumber(SafeFileHandle safeFileHandle)
        {
            return GetHidString(safeFileHandle, HidD_GetSerialNumberString);
        }

        public static Guid GetHidGuid()
        {
            if (_HidGuid.HasValue)
            {
                return _HidGuid.Value;
            }

            HidD_GetHidGuid(out var hidGuid);

            _HidGuid = hidGuid;

            return hidGuid;
        }
        #endregion
}

以上内容是Device.Net库的一部分,它使您可以在多个平台上使用Hid。