Hi..
yes i can do the same as you say ,
using create file i can open handle of all the devices enumerated with my system
but i suupose to not get that where to close that handles
take a look on my find hid code.
BOOL FindTheHID1()
{
WORD Stat, address,stat;
USHORT wrbuff[2]={0};
USHORT k,Lock;
//char UsageDescription;
char szLockType[4],strBuf[100]={0},lpBuff[500]={0};
int MemberIndex = 0,i;
int rc=0,sz=0;
LONG Result;
HIDD_ATTRIBUTES Attributes;
SP_DEVICE_INTERFACE_DATA devInfoData;
BOOL LastDevice = FALSE;
PHIDP_PREPARSED_DATA HidParsedData ;
Length = 0;
detailData = NULL;
DeviceHandle=NULL;
MyDeviceDetected = FALSE;
My_PAK = (LPMessage2)calloc(1,sizeof(LD));
for(i=0;i<8;i++)
{
lockinfo = GlobalAlloc(GPTR,sizeof(LI));
//HidHandle=NULL;
}
MyDeviceDetected = 0;
/*
API function: HidD_GetHidGuid
Get the GUID for all system HIDs.
Returns: the GUID in HidGuid.
*/
HidD_GetHidGuid(&HidGuid);
/*
API function: SetupDiGetClassDevs
Returns: a handle to a device information set for all installed devices.
Requires: the GUID returned by GetHidGuid.
*/
hDevInfo=SetupDiGetClassDevs
(&HidGuid,
NULL,
NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
devInfoData.cbSize = sizeof(devInfoData);
//Step through the available devices looking for the one we want.
//Quit on detecting the desired device or checking all available devices without success.
MemberIndex = 0;
while(SetupDiEnumDeviceInterfaces
(hDevInfo,
0,
&HidGuid,
MemberIndex,
&devInfoData))
{
if (!SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
NULL,
0,
&Length,
NULL))
{
//DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
// oDetail.Size = 5; // hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
//Allocate memory for the hDevInfo structure, using the returned Length.
detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
//Set cbSize in the detailData structure.
detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
detailData,
Length,
&Required,
NULL))
{
//sprintf(devicepath[MemberIndex]->data,"%s",detailData->DevicePath);
sprintf(lockinfo[MemberIndex]->Path,"%s",detailData->DevicePath);
/*
API function: CreateFile
Returns: a handle that enables reading and writing to the device.
Requires:
The DevicePath in the detailData structure
returned by SetupDiGetDeviceInterfaceDetail.
*/
//HidHandle[MemberIndex]
UsbHandle=CreateFile
(lockinfo[MemberIndex]->Path,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL);
/*
API function: HidD_GetAttributes
Requests information from the device.
Requires: the handle returned by CreateFile.
Returns: a HIDD_ATTRIBUTES structure containing
the Vendor ID, Product ID, and Product Version Number.
Use this information to decide if the detected device is
the one we're looking for.
*/
//Set the Size to the number of bytes in the structure.
Attributes.Size = sizeof(Attributes);
/*Result = HidD_GetAttributes
(HidHandle[MemberIndex],
&Attributes);*/
Result = HidD_GetAttributes
(UsbHandle,
&Attributes);
//Is it the desired device?
MyDeviceDetected = FALSE;
if (Attributes.VendorID == VendorID)//in second itration of MemberIndex = 1will get to start my devices
{ //(for first iteration atMemberIndex = 0;)i can get the attributes of my hid keyboard
if (Attributes.ProductID == HID_OLD || Attributes.ProductID == HID_NEW)
{
MyDeviceDetected = FALSE;
ghDeviceNew = CreateFile(lockinfo[MemberIndex]->Path,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
lockinfo[MemberIndex]->DevHandle=ghDeviceNew;
//ghDeviceNew = HidHandle[MemberIndex];
/* 6) Get the Device VID & PID to see if it's the device we want */
//if(ghDeviceNew != INVALID_HANDLE_VALUE)
if(lockinfo[MemberIndex]->DevHandle!= INVALID_HANDLE_VALUE)
{
gHID_Flag = HID_NEW;
if(detailData != NULL)
free(detailData);
/* get a handle to a buffer that describes the device's capabilities. This
line plus the following two lines of code extract the report length the
device is claiming to support */
HidD_GetPreparsedData(lockinfo[MemberIndex]->DevHandle, &HidParsedData);
/* extract the capabilities info */
HidP_GetCaps( HidParsedData ,&dev_Capabilities[MemberIndex]);
/* Free the memory allocated when getting the preparsed data */
HidD_FreePreparsedData(HidParsedData);
//return TRUE; /* found HID device */
MyDeviceDetected = TRUE;
}
}
}
}
}
MemberIndex++;
}
for(i=0;i<8;i++)//filling the structure with my information of all enumerated devices
{
if(lockinfo->DevHandle!=NULL)
{
set_ghDeviceNew(lockinfo->DevHandle);
set_capabilities(dev_Capabilities);
wrbuff[1]=SET_Fearture_Report(SentryRead,0x3e,00);
wrbuff[0]=SET_Fearture_Report(SentryRead,0x3f,00);
lockinfo->L_Serial=wrbuff[1];
lockinfo->L_Ext=wrbuff[0];
memcpy(&lockinfo->L_Serial,&wrbuff[1],8);
memcpy(&lockinfo->L_Ext,&wrbuff[0],8);
}
}
/*for(i=0;i<8;i++)
{
GlobalFree(lockinfo);
}*/
/* free HID device info list resources */
SetupDiDestroyDeviceInfoList(hDevInfo);
/* for(i=0;i<5;i++)
{
if(HidHandle != INVALID_HANDLE_VALUE)
{
CloseHandle(ghDeviceNew);
}
}*/
return MyDeviceDetected;
}