相关文章推荐

1、头文件包含,库文件链接

在VC中开发HID应用程序时,需要包含setupapi.h和hidsdi.h,包含方式必须为extern "C",如下所示:

extern "C" {
// Declare the C libraries used
#include "setupapi.h"  // Must link in setupapi.lib
#include "hidsdi.h"   // Must link in hid.lib
}

然后在setting—>Link—>object/libary modules 中添加setupapi.lib hid.lib 两个库文件。

2、Windows操作HID设备的API

API函数

DLL

功能

HidD_GetHidGuid

Hid.dll

取得类别

SetupDiGetClassDevs

Setupapi.dll

获取一个设备信息群,包含指定类的所有设备

SetupDiEnumDeviceInterfaces

Setupapi.dll

获取信息群内一个设备的信息

SetupDiGetDeviceInterfaceDetail

Setupapi.dll

获取设备路径

HidD_GetAttributes

Hid.dll

获取厂商与产品,版本号

HidP_GetValueCaps

Hid.dll

获取描述设备能力的结构

CreateFile

ReadFile

Kernel32.dll

Kernel32.dl

开启设备通信

从设备读取一个报文

WriteFile

Kernel32.dll

发送一个报文给设备

CloseHandle

Kernel32.dll

释放CreateFile创建的资源

SetupDiDestroyDeviceInfoList

Setupapi.dll

释放SetupDiGetClassDevs使用的资源

HidD_GetPreparsedData

Hid.dll

获取保存设备能力信息的缓冲器的句柄

HidD_SetFeature

Hid.dll

发送一个特征报文给设备

HidD_GetFeature

Hid.dll

从设备获取特征报文

void __stdcall HidD_GetHidGuid(
__out  LPGUID HidGuid    //指针指向调用者分配的GUID的内存区域,通常返回HID设备的GUID
);

HDEVINFO SetupDiGetClassDevs(
__in_opt  const GUID *ClassGuid,// 一个特定类别 GUID 的指针
__in_opt  PCTSTR Enumerator,     // 过滤梅举的内容
__in_opt  HWND hwndParent,        // 用于关联到集合成员中的用户接口的顶层窗口句柄
__in      DWORD Flags                      // 建立设备信息表的控制选项, DIGCF_PRESENT(当前存在的设备) DIGCF_ALLCLASSES(所有设备) DIGCF_PROFILE(当前硬件概况) ;

如成功,返回包含所有与指定参数匹配的已经安装设备信息句柄,否则返回 INVALID_HANDLE_VALUE

BOOL SetupDiEnumDeviceInterfaces(
__in      HDEVINFO DeviceInfoSet,  //一个指向设备信息集合的句柄,包含设备接口返回信息,通常是SetupDiGetClassDevs的返回
__in_opt  PSP_DEVINFO_DATA DeviceInfoData,  //指向特定设备的 SP_DEVINFO_DATA 类型的指针,
__in      const GUID *InterfaceClassGuid, //指向制定设备接口类的GUID指针
__in      DWORD MemberIndex,  //设备信息中接口列表的索引值(初始值为0)
__out     PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData //指向调用者分配的 SP_DEVICE_INTERFACE_DATA 类型的内存区域的指针,调用前必须先配置 DeviceInterfaceData . cbSize = sizeof (SP_DEVICE_INTERFACE_DATA)
);

如成功,返回TRUE,否则返回FALSE,并可以调用GetLastError(void)获取错误信息

BOOL SetupDiGetDeviceInterfaceDetail(
__in       HDEVINFO DeviceInfoSet,  //一个指向设备信息集合的句柄,包含设备接口返回信息,通常是SetupDiGetClassDevs的返回
__in       PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,  //指向 SP_DEVICE_INTERFACE_DATA 类型的内存区域的指针,SetupDiEnumDeviceInterfaces的返回
__out_opt  PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,// SP_DEVICE_INTERFACE_DETAIL_DATA 类型指针,用于获取路径,调用前必须先配置 DeviceInterfaceDetailData .cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA)
__in       DWORD DeviceInterfaceDetailDataSize, // DeviceInterfaceDetailData 的长度,
__out_opt  PDWORD RequiredSize, // DeviceInterfaceDetailData 的实际长度
__out_opt  PSP_DEVINFO_DATA DeviceInfoData  //PSP_DEVINFO_DATA 类型的指针,存放支持的设备接口信息, DeviceInfoData .cbSize = sizeof (SP_DEVINFO_DATA)
);

如成功,返回TRUE,否则返回FALSE,并可以调用GetLastError(void)获取错误信息

BOOLEAN __stdcall HidD_GetAttributes(
__in   HANDLE HidDeviceObject,  //HID设备句柄
__out  PHIDD_ATTRIBUTES Attributes  // HIDD_ATTRIBUTES 类型指针
);

HANDLE WINAPI CreateFile(
__in      LPCTSTR lpFileName,  //已打开的设备名称
__in      DWORD dwDesiredAccess, //操作权限,GENERIC_READ(读模式), GENERIC_WRITE(写模式), 或者both
__in      DWORD dwShareMode,  //共享模式,0(禁止访问),FILE_SHARE_DELETE(删除),FILE_SHARE_READ(读),FILE_SHARE_WRITE(写)

__in_opt  LPSECURITY_ATTRIBUTES lpSecurityAttributes,  // SECURITY_ATTRIBUTES 类型指针
__in      DWORD dwCreationDisposition, //打开方式,对于设备来说经常设为 OPEN_EXISTING
__in      DWORD dwFlagsAndAttributes,  //设备属性和标识,经常设为FILE_ATTRIBUTE_NORMAL
__in_opt  HANDLE hTemplateFile  //模板文件句柄
);

如成功,则返回一个文件或设备的句柄

BOOL WINAPI ReadFile(
__in         HANDLE hFile,  //文件或设备的句柄
__out        LPVOID lpBuffer,    //存放读取数据的指针

__in         DWORD nNumberOfBytesToRead,  //读取数据长度的最大值
__out_opt    LPDWORD lpNumberOfBytesRead,  //读取数据的实际长度
__inout_opt  LPOVERLAPPED lpOverlapped // OVERLAPPED 类型指针,如果该设备或文件被重复打开,需要传入次参数
);

如成功,返回TRUE,否则返回FALSE,并可以调用GetLastError(void)获取错误信息

BOOL WINAPI WriteFile(
__in         HANDLE hFile,  //文件或设备的句柄
__in         LPCVOID lpBuffer,  //存放写入数据的指针
__in         DWORD nNumberOfBytesToWrite,    //写入数据长度的最大值
__out_opt    LPDWORD lpNumberOfBytesWritten,   //写入数据的实际长度
__inout_opt  LPOVERLAPPED lpOverlapped  // OVERLAPPED 类型指针,如果该设备或文件被重复打开,需要传入次参数
);

如成功,返回TRUE,否则返回FALSE,并可以调用GetLastError(void)获取错误信息

BOOL SetupDiDestroyDeviceInfoList( __in  HDEVINFO DeviceInfoSet);  //释放SetupDiGetClassDevs使用的资源

3、代码示例

打开一个指定VendorID和ProductID的HID设备

PSP_DEVICE_INTERFACE_DETAIL_DATA HID_FindDevices(USHORT V_ID, USHORT P_ID, unsigned short *FIFO_Length)
{
GUID                             HidGuid;
HDEVINFO                         DevInfo;
HIDD_ATTRIBUTES      DevAttributes;
SP_DEVICE_INTERFACE_DATA         DevData;
PSP_DEVICE_INTERFACE_DETAIL_DATA DevDetail;
PHIDP_PREPARSED_DATA          PreparsedData;
HIDP_CAPS                   Capabilities;
ULONG                            Length;
int                              Index;

BOOL                             ok;
HANDLE DevHandle;
int DevCount = 0;
/* Get GUID for all System HIDs */

HidD_GetHidGuid(&HidGuid);

/* Get Device Information for all present devices */
DevInfo = SetupDiGetClassDevs(&HidGuid,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)
);

DevData.cbSize = sizeof(DevData);

DevDetail = NULL;

Index = -1;
*FIFO_Length = 0;
/* Scan all Devices */
do {

Index++;

/* Device Interface Element of a Device Information set */
ok = SetupDiEnumDeviceInterfaces(DevInfo,
0,
&HidGuid,
Index,
&DevData
);
if (!ok) break;

/* Get Device Interface Details - Get Length */
ok = SetupDiGetDeviceInterfaceDetail(DevInfo,
&DevData,
NULL,
0,
&Length,
NULL
);

/* Allocate memory for Device Detailed Data */
DevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(Length);

/* Set cbSize in the DevDetail structure */
DevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

/* Get Device Interface Details */
ok = SetupDiGetDeviceInterfaceDetail(DevInfo,
&DevData,
DevDetail,
Length,
NULL,
NULL
);
if (!ok)
{
free(DevDetail);
DevDetail = NULL;
continue;
}

/* Create File for Device Read/Write */
DevHandle = CreateFile(DevDetail->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL
);

if (DevHandle == INVALID_HANDLE_VALUE)
{
free(DevDetail);
DevDetail = NULL;
continue;
}

DevAttributes.Size = sizeof(DevAttributes);
ok = HidD_GetAttributes(DevHandle,&DevAttributes);
if(!ok)
{
free(DevDetail);
CloseHandle(DevHandle);
DevDetail = NULL;
continue;
}
if(DevAttributes.VendorID!=V_ID||DevAttributes.ProductID != P_ID)
{
free(DevDetail);
CloseHandle(DevHandle);
DevDetail = NULL;
continue;
}
/* Get Preparsed Data */
ok = HidD_GetPreparsedData(DevHandle, &PreparsedData);
if (!ok)
{
free(DevDetail);
CloseHandle(DevHandle);
DevDetail = NULL;
continue;
}

/* Get Device's Capabilities */
HidP_GetCaps(PreparsedData, &Capabilities);
*FIFO_Length = Capabilities.InputReportByteLength;

CloseHandle (DevHandle);
break;

} while (DevCount < 20);
SetupDiDestroyDeviceInfoList (DevInfo);

return DevDetail;

from: http://blog.csdn.net/shejiannan/article/details/16804221

1、头文件包含,库文件链接 在VC中开发HID应用程序时,需要包含setupapi.h和hidsdi.h,包含方式必须为extern "C",如下所示:extern "C" {// Declare the C libraries used#include "setupapi.h"  // Must link in setupapi.lib#include "hidsdi
1、头文件包含,库文件链接  在VC中开发 HID 应用程序时,需要包含setup api .h和 hid sdi.h,包含方式必须为extern "C",如下所示: extern "C" { // Declare the C libraries used #include "set
VC环境下_USB_ HID 类说明 1、头文件包含,库文件链接  在VC中开发 HID 应用程序时,需要包含setup api .h和 hid sdi.h,包含方式必须为extern "C",如下所示: extern "C" { // Declare the C libraries used #include "setup api .h"  // Must link in setup api .lib #include " hid sdi.h"   // Must link in hid .lib };然后在setting—>Link—>object/libary modules 中添加setup api .lib hid .lib 两个库文件。
1. 确认蓝牙适配器已经连接到计算机上,并且已经打开。 2. 使用 Windows .Devices.Bluetooth命名空间中的BluetoothLEDevice.FromBluetoothAddressAsync方法获取蓝牙 设备 对象。 3. 使用 Windows .Devices.Enumeration命名空间中的DeviceInformation.FindAllAsync方法查找 设备 服务。 4. 使用 Windows .Devices.Bluetooth.GenericAttributeProfile命名空间中的GattDeviceService.GetCharacteristicsAsync方法获取 设备 特性。 5. 使用 Windows .Devices.Bluetooth.GenericAttributeProfile命名空间中的GattCharacteristic.WriteValueAsync方法写入数据。 示例代码如下: ```csharp using Windows .Devices.Bluetooth; using Windows .Devices.Enumeration; using Windows .Devices.Bluetooth.GenericAttributeProfile; using System; using System.Threading.Tasks; namespace Bluetooth HID class Program static async Task Main(string[] args) var deviceSelector = BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected); var deviceInformationCollection = await DeviceInformation.FindAllAsync(deviceSelector); if (deviceInformationCollection.Count > 0) var bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(deviceInformationCollection[0].Id); if (bluetoothLeDevice != null) var servicesResult = await bluetoothLeDevice.GetGattServicesAsync(); if (servicesResult.Status == GattCommunicationStatus.Success) foreach (var service in servicesResult.Services) var characteristicsResult = await service.GetCharacteristicsAsync(); if (characteristicsResult.Status == GattCommunicationStatus.Success) foreach (var characteristic in characteristicsResult.Characteristics) if (characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write)) var writer = new Windows .Storage.Streams.DataWriter(); writer.WriteString("Hello World"); var result = await characteristic.WriteValueAsync(writer.DetachBuffer()); if (result == GattCommunicationStatus.Success) Console.WriteLine("Data written successfully"); 需要注意的是,要使用 Windows 11 API 实现 HID 蓝牙连接需要在 Windows 11 操作 系统中运行。
 
推荐文章