C++ HID设备编程-是否与C++;(SETUPAPI.dll和HID.dll)
我对问@Stackoverflow有点陌生,但这是我最接近圣经的东西(除了里奇的C书),特别是在我大部分科目的期末项目中。不管怎样,我的问题是关于一个应用程序需要与HID设备通信的库,以及用C++来做的可能性。 我不需要固件方面的帮助,我的设备已经按照我期望的方式工作了。然而,我唯一的编程HID设备的经验是使用C(Windows-VS2010),现在我正在完成编译器类的最后一个项目,我们将信息发送到带有矩阵屏幕的设备,以显示“东西”。然而,我的合作伙伴需要在C++中做一些事情,这可以节省我们很多时间(考虑到两天后到期,这件事很不错) 问题的关键在于,是否可以像我在C中已经做过的那样(进行了明显的调整)和C++中的部分代码,仍然可以兼容SetupApi.DLL和HID.DLL 我们非常感谢任何形式的建议、指示或指示。我包括我迄今为止使用的代码 请原谅我可能会用西班牙语和脏话发表的所有评论C++ HID设备编程-是否与C++;(SETUPAPI.dll和HID.dll),c++,dll,device-driver,hid,setupapi,C++,Dll,Device Driver,Hid,Setupapi,我对问@Stackoverflow有点陌生,但这是我最接近圣经的东西(除了里奇的C书),特别是在我大部分科目的期末项目中。不管怎样,我的问题是关于一个应用程序需要与HID设备通信的库,以及用C++来做的可能性。 我不需要固件方面的帮助,我的设备已经按照我期望的方式工作了。然而,我唯一的编程HID设备的经验是使用C(Windows-VS2010),现在我正在完成编译器类的最后一个项目,我们将信息发送到带有矩阵屏幕的设备,以显示“东西”。然而,我的合作伙伴需要在C++中做一些事情,这可以节省我们很多
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#include <SETUPAPI.H>
//----------------------------------------------
#define RICH_VENDOR_ID 0x0000
#define RICH_USBHID_GENIO_ID 0x2012
#define INPUT_REPORT_SIZE 5
#define OUTPUT_REPORT_SIZE 2
//----------------------------------------------
char c=0x0;
DWORD BytesRead = 0;
DWORD BytesWritten = 0;
unsigned char reporteEntrada[INPUT_REPORT_SIZE+1];
unsigned char reporteSalida[OUTPUT_REPORT_SIZE+1];
int status = 0;
static unsigned char dato = 0x01;
static unsigned char numLED = 1;
unsigned char palabra[192]={0};
int desplegar,valorled;
desplegar=0;
valorled=0;
unsigned char temporal;
//unsigned char ch;
//unsigned int index;
typedef struct _HIDD_ATTRIBUTES {
ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
} HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
typedef VOID (__stdcall *PHidD_GetProductString)(HANDLE, PVOID, ULONG);
typedef VOID (__stdcall *PHidD_GetHidGuid)(LPGUID);
typedef BOOLEAN (__stdcall *PHidD_GetAttributes)(HANDLE, PHIDD_ATTRIBUTES);
typedef BOOLEAN (__stdcall *PHidD_SetFeature)(HANDLE, PVOID, ULONG);
typedef BOOLEAN (__stdcall *PHidD_GetFeature)(HANDLE, PVOID, ULONG);
//----------------------------------------------
HINSTANCE hHID = NULL;
PHidD_GetProductString HidD_GetProductString = NULL;
PHidD_GetHidGuid HidD_GetHidGuid = NULL;
PHidD_GetAttributes HidD_GetAttributes = NULL;
PHidD_SetFeature HidD_SetFeature = NULL;
PHidD_GetFeature HidD_GetFeature = NULL;
HANDLE DeviceHandle = INVALID_HANDLE_VALUE;
unsigned int moreHIDDevices=TRUE;
unsigned int HIDDeviceFound=FALSE;
unsigned int terminaAbruptaEInstantaneamenteElPrograma=0;
void Load_HID_Library (void) {
hHID = LoadLibrary("HID.DLL");
if (!hHID) {
printf("Failed to load HID.DLL\n");
return;
}
HidD_GetProductString = (PHidD_GetProductString) GetProcAddress(hHID, "HidD_GetProductString");
HidD_GetHidGuid = (PHidD_GetHidGuid) GetProcAddress(hHID, "HidD_GetHidGuid");
HidD_GetAttributes = (PHidD_GetAttributes) GetProcAddress(hHID, "HidD_GetAttributes");
HidD_SetFeature = (PHidD_SetFeature) GetProcAddress(hHID, "HidD_SetFeature");
HidD_GetFeature = (PHidD_GetFeature) GetProcAddress(hHID, "HidD_GetFeature");
if ( !HidD_GetProductString
|| !HidD_GetAttributes
|| !HidD_GetHidGuid
|| !HidD_SetFeature
|| !HidD_GetFeature ) {
printf("Couldn't find one or more HID entry points\n");
return;
}
}
int Open_Device(void) {
HDEVINFO DeviceInfoSet;
GUID InterfaceClassGuid;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
HIDD_ATTRIBUTES Attributes;
DWORD Result;
DWORD MemberIndex = 0;
DWORD Required;
//Validar si se "cargó" la biblioteca (DLL)
if (!hHID)
return (0);
//Obtener el Globally Unique Identifier (GUID) para dispositivos HID
HidD_GetHidGuid (&InterfaceClassGuid);
//Sacarle a Windows la información sobre todos los dispositivos HID instalados y activos en el sistema
// ... almacenar esta información en una estructura de datos de tipo HDEVINFO
DeviceInfoSet = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
return (0);
//Obtener la interfaz de comunicación con cada uno de los dispositivos para preguntarles información específica
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
while (!HIDDeviceFound) {
// ... utilizando la variable MemberIndex ir preguntando dispositivo por dispositivo ...
moreHIDDevices = SetupDiEnumDeviceInterfaces (DeviceInfoSet, NULL, &InterfaceClassGuid,
MemberIndex,&DeviceInterfaceData);
if (!moreHIDDevices) {
// ... si llegamos al fin de la lista y no encontramos al dispositivo ==> terminar y marcar error
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
return (0); //No more devices found
} else {
//Necesitamos preguntar, a través de la interfaz, el PATH del dispositivo, para eso ...
// ... primero vamos a ver cuántos caracteres se requieren (Required)
Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,&DeviceInterfaceData,NULL,0,&Required,NULL);
pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Required);
if (pDeviceInterfaceDetailData == NULL) {
printf("Error en SetupDiGetDeviceInterfaceDetail\n");
return (0);
}
//Ahora si, ya que el "buffer" fue preparado (pDeviceInterfaceDetailData{DevicePath}), vamos a preguntar PATH
pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
Result = SetupDiGetDeviceInterfaceDetail (DeviceInfoSet,&DeviceInterfaceData,pDeviceInterfaceDetailData,
Required,NULL,NULL);
if (!Result) {
printf("Error en SetupDiGetDeviceInterfaceDetail\n");
free(pDeviceInterfaceDetailData);
return(0);
}
//Para este momento ya sabemos el PATH del dispositivo, ahora hay que preguntarle ...
// ... su VID y PID, para ver si es con quien nos interesa comunicarnos
printf("Found? ==> ");
printf("Device: %s\n",pDeviceInterfaceDetailData->DevicePath);
//Obtener un "handle" al dispositivo
DeviceHandle = CreateFile (pDeviceInterfaceDetailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL);
if (DeviceHandle == INVALID_HANDLE_VALUE) {
printf("¡¡¡Error en el CreateFile!!!\n");
} else {
//Preguntar por los atributos del dispositivo
Attributes.Size = sizeof(Attributes);
Result = HidD_GetAttributes (DeviceHandle,&Attributes);
if (!Result) {
printf("Error en HIdD_GetAttributes\n");
CloseHandle(DeviceHandle);
free(pDeviceInterfaceDetailData);
return(0);
}
//Analizar los atributos del dispositivo para verificar el VID y PID
printf("MemberIndex=%d,VID=%04x,PID=%04x\n",MemberIndex,Attributes.VendorID,Attributes.ProductID);
if ((Attributes.VendorID == RICH_VENDOR_ID) && (Attributes.ProductID == RICH_USBHID_GENIO_ID)) {
printf("USB/HID GenIO ==> ");
printf("Device: %s\n",pDeviceInterfaceDetailData->DevicePath);
HIDDeviceFound=TRUE;
} else
CloseHandle(DeviceHandle);
}
MemberIndex++;
free(pDeviceInterfaceDetailData);
if (HIDDeviceFound) {
printf("Dispositivo HID solicitado ... ¡¡¡localizado!!!, presione <ENTER>\n");
getchar();
}
}
}
return(1);
}
void Close_Device (void) {
if (DeviceHandle != NULL) {
CloseHandle(DeviceHandle);
DeviceHandle = NULL;
}
}
int chToIndex(unsigned char ch){
if((ch>='0')&&(ch<='9')){
return ch-48;
}else if((ch>='A')&&(ch<='Z')){
return ch-55;
}else if((ch>='a')&&(ch<='z')){
return ch-61;
}else{
switch (ch){
case '*': return 62;
// break;
case '+': return 63;
// break;
case '-': return 64;
//break;
case '/': return 65;
//break;
case '=': return 66;
// break;
case '>': return 67;
// break;
case '<': return 68;
// break;
case '!': return 69;
// break;
case '.': return 70;
// break;
case '&': return 71;
// break;
case '^': return 72;
// break;
case '(': return 73;
// break;
case ')': return 74;
// break;
case '[': return 75;
// break;
case ']': return 76;
// break;
case ',': return 77;
// break;
case ';': return 78;
// break;
case ':': return 79;
// break;
case 0x20: return 80;
default: return 81;
// break;
}
}
}
int Touch_Device (void) {
int z;
int index;
int col,lin;
unsigned char ch;
if (DeviceHandle == NULL) //Validar que haya comunicacion con el dispositivo
return 0;
if(c != '27' )
{
printf("Selecciona accion:\n1) Limpiar Pantalla\n2) Imprimir todos caracteres\n3) Escribir caracter\n4) Escribir string\n5) Salir\n");
desplegar=getch();
desplegar=getch();
if((desplegar=='5')||(desplegar==(char)'27')){
terminaAbruptaEInstantaneamenteElPrograma=1;
}
else if(desplegar=='4'){
lin=0;
col=0;
index=0;
printf("Escribe texto (termina con Enter):\n");
scanf("%s",palabra);
while(palabra[index]!='\0'){
z++;
}
printf("Longitud de palabra: %d",z);
while((palabra[index]!='\0')){
if((palabra[index]==NULL)&&(palabra[index+1]!=NULL))
palabra[index]=0x20;
reporteSalida[0]=0x00;
reporteSalida[1]=0x04;
reporteSalida[2]=(char)chToIndex(palabra[index]);
reporteSalida[3]=(char)lin;
reporteSalida[4]=(char)col;
status = WriteFile(DeviceHandle,reporteSalida,INPUT_REPORT_SIZE+1,&BytesWritten,NULL);
if (!status)
printf("Error en el WriteFile %d %d\n",GetLastError(),BytesWritten);
else
printf("Se enviaron %d bytes al dispositivo (posX=%d, posY=%d, dato=%d)\n",BytesWritten,reporteSalida[4],reporteSalida[3],reporteSalida[2]);
if(col>=23){
lin++;
col=0;
}
if(lin>=7){
lin=0;
}
col++;
index++;
}
}
else if (desplegar=='3'){
reporteSalida[0]=0x00;
reporteSalida[1]=0x04;
printf("Que caracter deseas desplegar: ");
ch=getch();
printf("\n");
z=chToIndex(ch);
reporteSalida[2]=(char)z;
do{
printf("En que renglon en y (0-7): ");
scanf("%d",&desplegar);
printf("\n");
}while((desplegar<0)&&(desplegar>7));
reporteSalida[3]=(char)desplegar;
do{
printf("En que columna en x (0-23): ");
scanf("%d",&desplegar);
printf("\n");
}while((desplegar<0)&&(desplegar>23));
reporteSalida[4]=(char)desplegar;
printf("ReporteSalida: %02X %02X %02X %02X %02X \n",(unsigned char)reporteSalida[0],
(unsigned char)reporteSalida[1],
(unsigned char)reporteSalida[2],
(unsigned char)reporteSalida[3],
(unsigned char)reporteSalida[4]);
status = WriteFile(DeviceHandle,reporteSalida,INPUT_REPORT_SIZE+1,&BytesWritten,NULL);
if (!status)
printf("Error en el WriteFile %d %d\n",GetLastError(),BytesWritten);
else
printf("Se enviaron %d bytes al dispositivo (posX=%d, posY=%d, dato=%d)\n",BytesWritten,reporteSalida[4],reporteSalida[3],reporteSalida[2]);
}
else if (desplegar=='2'){
lin=0;
col=0;
for (index=0;index<=80;index++){
reporteSalida[0]=0x00;
reporteSalida[1]=0x04;
reporteSalida[2]=(char)index;
reporteSalida[3]=(char)lin;
reporteSalida[4]=(char)col;
status = WriteFile(DeviceHandle,reporteSalida,INPUT_REPORT_SIZE+1,&BytesWritten,NULL);
if (!status)
printf("Error en el WriteFile %d %d\n",GetLastError(),BytesWritten);
else
printf("Se enviaron %d bytes al dispositivo (posX=%d, posY=%d, dato=%d)\n",BytesWritten,reporteSalida[4],reporteSalida[3],reporteSalida[2]);
if(col>=23){
lin++;
col=0;
}
if(lin>=7){
lin=0;
}
col++;
}
}
else if(desplegar=='1'){
printf("Limpiando pantalla ...\n");
reporteSalida[0]=0x00;
reporteSalida[1]=0x03;
reporteSalida[2]=0;
status = WriteFile(DeviceHandle,reporteSalida,INPUT_REPORT_SIZE+1,&BytesWritten,NULL);
if (!status)
printf("Error en el WriteFile %d %d\n",GetLastError(),BytesWritten);
else
printf("Escritos %d\n",BytesWritten);
}
else{
Touch_Device();
}
}
return status;
}
void main () {
Load_HID_Library();
if (Open_Device()) {
printf("Vamos bien\n");
while ((c=getch()!=27)
&&(!terminaAbruptaEInstantaneamenteElPrograma)) {
Touch_Device();
Sleep(500);
}
} else {
printf(">:(\n");
}
Close_Device();
}
#包括
#包括
#包括
#包括
#包括
//----------------------------------------------
#定义富\u供应商\u ID 0x0000
#定义RICH_USBHID_GENIO_ID 0x2012
#定义输入\报告\大小5
#定义输出报告大小2
//----------------------------------------------
字符c=0x0;
DWORD字节读=0;
DWORD字节数=0;
unsigned char reporteEntrada[INPUT_REPORT_SIZE+1];
无符号字符报告SALIDA[输出报告大小+1];
int status=0;
静态无符号字符dato=0x01;
静态无符号字符numLED=1;
无符号字符palabra[192]={0};
内德斯普莱加,瓦洛利德;
desplegar=0;
valorled=0;
无符号字符时态;
//无符号字符ch;
//无符号整数索引;
typedef struct\u HIDD\u属性{
ULONG Size;/=sizeof(结构HIDD属性)
乌肖特·本多里德;
USHORT-ProductID;
USHORT版本号;
}HIDD_属性,*PHIDD_属性;
typedef VOID(uu stdcall*PHidD_GetProductString)(句柄、PVOID、ULONG);
typedef VOID(uu stdcall*PHidD_GetHidGuid)(LPGUID);
typedef布尔值(uu stdcall*PHidD_GetAttributes)(句柄,PHidD_属性);
typedef布尔值(uu stdcall*PHidD_SetFeature)(句柄、PVOID、ULONG);
typedef布尔值(uu stdcall*PHidD_GetFeature)(句柄、PVOID、ULONG);
//----------------------------------------------
HINSTANCE hHID=NULL;
PHidD_GetProductString HidD_GetProductString=NULL;
PHidD_GetHidGuid HidD_GetHidGuid=NULL;
PHidD_GetAttributes HidD_GetAttributes=NULL;
PHidD_SetFeature HidD_SetFeature=NULL;
PHidD_GetFeature HidD_GetFeature=NULL;
句柄设备句柄=无效的句柄值;
unsigned int=TRUE;
unsigned int-hiddevicefund=FALSE;
无符号int-terminaabruptaeinnstantaneamenteelprograma=0;
无效加载\u隐藏\u库(无效){
hHID=LoadLibrary(“HID.DLL”);
如果(!hHID){
printf(“未能加载HID.DLL\n”);
返回;
}
HidD_GetProductString=(PHidD_GetProductString)GetProcAddress(hHID,“HidD_GetProductString”);
HidD_GetHidGuid=(PHidD_GetHidGuid)GetProcAddress(hHID,“HidD_GetHidGuid”);
HidD_GetAttributes=(PHidD_GetAttributes)GetProcAddress(hHID,“HidD_GetAttributes”);
HidD_SetFeature=(PHidD_SetFeature)GetProcAddress(hHID,“HidD_SetFeature”);
HidD_GetFeature=(PHidD_GetFeature)GetProcAddress(hHID,“HidD_GetFeature”);
如果(!HidD_GetProductString
||!HidD_GetAttributes
||!HidD_GetHidGuid
||!HidD_SetFeature
||!HidD_GetFeature){
printf(“找不到一个或多个HID入口点\n”);
返回;
}
}
int Open_设备(无效){
HDEVINFO设备信息集;
GUID接口classguid;
SP_设备_接口_数据设备接口数据;
PSP_设备_接口_详情_数据pDeviceInterfaceDetailData;
HIDD_属性;
德沃德结果;
DWORD MemberIndex=0;
德沃德要求;
//Validar si se“cargó”la biblioteca(DLL)
如果(!hHID)
返回(0);
//Obtener el全局唯一标识符(GUID)para dispositivos HID
HidD_GetHidGuid(&InterfaceClassGuid);
//一个信息窗口隐藏了系统中的安装和活动
//…我们的信息来源于我们的信息结构
DeviceInfo=SetupDiGetClassDevs(&InterfaceClassGuid,NULL,NULL,DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if(DeviceInfo==无效的\u句柄\u值)
返回(0);
//关于特别是在怀孕前的信息处理方面的信息交流
DeviceInterfaceData.cbSize=sizeof(SP\u设备\u接口\u数据);
而(!hiddevicefund){
//…使用变量成员索引ir preguntando DISPITIVO por DISPITIVO。。。
moreHIDDevices=SetupDienumDeviceInterface(DeviceInfo集、NULL和InterfaceClassGuid,
成员索引和设备接口数据);
如果(!设备){
//这是一个错误,没有任何处置权==>terminar y marcar error
SetupDiDestroyDeviceInfo列表(DeviceInfo集);
返回(0);//找不到更多设备
}否则{
//前过渡时期的必要性,国际交流的途径,处置道路,第eso段。。
#ifdef __cplusplus
extern "C" {
#endif
int declaration_of_c_function(...);
#ifdef __cplusplus
}
#endif