VOID CloseHidDevices( IN PHID_DEVICE HidDevices, IN ULONG NumberDevices ) { ULONG Index; for (Index = 0; Index < NumberDevices; Index++) { CloseHidDevice(HidDevices+Index); } return; }
void SendReceiveData::threadProc() { //======================== OVERLAPPED overlappedRead; HANDLE events[2]; DWORD result; //======================== memset(&overlappedRead,0,sizeof(OVERLAPPED)); overlappedRead.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL); exitEvent = CreateEvent(NULL,TRUE,FALSE,NULL); events[0] = overlappedRead.hEvent; events[1] = exitEvent; setFeatures(); while(1) { //============== UCHAR buffer[5]; DWORD bytesRead; //============== if(!ReadFile(device.HidDevice,buffer,5,&bytesRead,&overlappedRead)) { //break; } result = WaitForMultipleObjects(2,events,FALSE,INFINITE); if(result==(WAIT_OBJECT_0)) { irCode = buffer[2]; if(irCode==lastValue) { repeats++; } else { repeats = 0; } //printf("irCode %i repeats %i\n",irCode,repeats); SetEvent(dataReadyEvent); } if(result==(WAIT_OBJECT_0+1)) { //printf("leaving thread \n"); break; } } restoreFeatures(); CloseHidDevice(&device); SAFE_CLOSE_HANDLE(exitEvent); SAFE_CLOSE_HANDLE(overlappedRead.hEvent); }
BOOLEAN OpenHidDevice ( IN PCHAR DevicePath, IN BOOL HasReadAccess, IN BOOL HasWriteAccess, IN BOOL IsOverlapped, IN BOOL IsExclusive, IN OUT PHID_DEVICE HidDevice ) /*++ RoutineDescription: Given the HardwareDeviceInfo, representing a handle to the plug and play information, and deviceInfoData, representing a specific hid device, open that device and fill in all the relivant information in the given HID_DEVICE structure. return if the open and initialization was successfull or not. --*/ { DWORD accessFlags = 0; DWORD sharingFlags = 0; BOOLEAN bSuccess; INT iDevicePathSize; HRESULT stringReturn; iDevicePathSize = strlen(DevicePath) + 1; HidDevice -> DevicePath = malloc(iDevicePathSize); if (NULL == HidDevice -> DevicePath) { return (FALSE); } stringReturn = StringCbCopy(HidDevice -> DevicePath, iDevicePathSize, DevicePath); if (HasReadAccess) { accessFlags |= GENERIC_READ; } if (HasWriteAccess) { accessFlags |= GENERIC_WRITE; } if (!IsExclusive) { sharingFlags = FILE_SHARE_READ | FILE_SHARE_WRITE; } // // The hid.dll api's do not pass the overlapped structure into deviceiocontrol // so to use them we must have a non overlapped device. If the request is for // an overlapped device we will close the device below and get a handle to an // overlapped device // HidDevice->HidDevice = CreateFile (DevicePath, accessFlags, sharingFlags, NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // Open device as non-overlapped so we can get data NULL); // No template file if (INVALID_HANDLE_VALUE == HidDevice->HidDevice) { free(HidDevice -> DevicePath); HidDevice -> DevicePath = INVALID_HANDLE_VALUE ; return FALSE; } HidDevice -> OpenedForRead = HasReadAccess; HidDevice -> OpenedForWrite = HasWriteAccess; HidDevice -> OpenedOverlapped = IsOverlapped; HidDevice -> OpenedExclusive = IsExclusive; // // If the device was not opened as overlapped, then fill in the rest of the // HidDevice structure. However, if opened as overlapped, this handle cannot // be used in the calls to the HidD_ exported functions since each of these // functions does synchronous I/O. // if (!HidD_GetPreparsedData (HidDevice->HidDevice, &HidDevice->Ppd)) { free(HidDevice -> DevicePath); HidDevice -> DevicePath = NULL ; CloseHandle(HidDevice -> HidDevice); HidDevice -> HidDevice = INVALID_HANDLE_VALUE ; return FALSE; } if (!HidD_GetAttributes (HidDevice->HidDevice, &HidDevice->Attributes)) { free(HidDevice -> DevicePath); HidDevice -> DevicePath = NULL; CloseHandle(HidDevice -> HidDevice); HidDevice -> HidDevice = INVALID_HANDLE_VALUE; HidD_FreePreparsedData (HidDevice->Ppd); HidDevice->Ppd = NULL; return FALSE; } if (!HidP_GetCaps (HidDevice->Ppd, &HidDevice->Caps)) { free(HidDevice -> DevicePath); HidDevice -> DevicePath = NULL; CloseHandle(HidDevice -> HidDevice); HidDevice -> HidDevice = INVALID_HANDLE_VALUE; HidD_FreePreparsedData (HidDevice->Ppd); HidDevice->Ppd = NULL; return FALSE; } // // At this point the client has a choice. It may chose to look at the // Usage and Page of the top level collection found in the HIDP_CAPS // structure. In this way it could just use the usages it knows about. // If either HidP_GetUsages or HidP_GetUsageValue return an error then // that particular usage does not exist in the report. // This is most likely the preferred method as the application can only // use usages of which it already knows. // In this case the app need not even call GetButtonCaps or GetValueCaps. // // In this example, however, we will call FillDeviceInfo to look for all // of the usages in the device. // bSuccess = FillDeviceInfo(HidDevice); if (FALSE == bSuccess) { CloseHidDevice(HidDevice); return (FALSE); } if (IsOverlapped) { CloseHandle(HidDevice->HidDevice); HidDevice->HidDevice = CreateFile (DevicePath, accessFlags, sharingFlags, NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags FILE_FLAG_OVERLAPPED, // Now we open the device as overlapped NULL); // No template file if (INVALID_HANDLE_VALUE == HidDevice->HidDevice) { CloseHidDevice(HidDevice); return FALSE; } } return (TRUE); }
void WM_COMMAND_hook ( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { PHID_DEVICE pDevice; BOOL status; switch (LOWORD(wParam)) { case IDC_FLD_CTRL_DEV: { if (BT_writeDevice.HidDevice != 0) { HWND hUART_DEV = GetDlgItem(hDlg_main, IDC_UART_DEVICE); SetWindowText(hUART_DEV, ""); HWND hBUTT = GetDlgItem(hDlg_main, IDC_FLD_CTRL_DEV); SetWindowText(hBUTT, "DEV OPEN"); CloseHidDevice(&BT_writeDevice); EnableWindow(GetDlgItem(hDlg, IDC_DEVICES), 1); // Set device change message to enable other controls PostMessage(hDlg, WM_COMMAND, IDC_DEVICES + (CBN_SELCHANGE<<16), (LPARAM) GetDlgItem(hDlg,IDC_DEVICES)); ZeroMemory(&BT_writeDevice, sizeof(BT_writeDevice)); } else { INT iIndex; pDevice = NULL; iIndex = (INT) SendDlgItemMessage(hDlg, IDC_DEVICES, CB_GETCURSEL, 0, 0); if (CB_ERR != iIndex) { pDevice = (PHID_DEVICE) SendDlgItemMessage(hDlg, IDC_DEVICES, CB_GETITEMDATA, iIndex, 0); } if (NULL == pDevice) break; status = OpenHidDevice(pDevice -> DevicePath, FALSE, TRUE, FALSE, FALSE, &BT_writeDevice); if (!status) { MessageBox(hDlg, "Couldn't open device for write access", HCLIENT_ERROR, MB_ICONEXCLAMATION); ZeroMemory(&BT_writeDevice, sizeof(BT_writeDevice)); } // Disable all other controls EnableWindow(GetDlgItem(hDlg, IDC_EXTCALLS), 0); EnableWindow(GetDlgItem(hDlg, IDC_READ), 0); EnableWindow(GetDlgItem(hDlg, IDC_WRITE), 0); EnableWindow(GetDlgItem(hDlg, IDC_FEATURES), 0); EnableWindow(GetDlgItem(hDlg, IDC_DEVICES), 0); StringCbPrintf(szTempBuff, SMALL_BUFF, "VID: 0x%04X PID: 0x%04X UsagePage: 0x%X Usage: 0x%X", BT_writeDevice.Attributes.VendorID, BT_writeDevice.Attributes.ProductID, BT_writeDevice.Caps.UsagePage, BT_writeDevice.Caps.Usage); HWND hUART_DEV = GetDlgItem(hDlg_main, IDC_UART_DEVICE); SetWindowText(hUART_DEV, szTempBuff); HWND hBUTT = GetDlgItem(hDlg_main, IDC_FLD_CTRL_DEV); SetWindowText(hBUTT, "DEV CLOSE"); } // End of Device open break; } // End of FLD_CTRL_DEV case } // End of wParam switch return; }