// DeviceHid2Dlg.cpp : implementation file // //#define WINVER 0x0500 #include "stdafx.h" #include "DeviceHid2.h" #include "DeviceHid2Dlg.h" #include #include #include "WinUser.h" #ifndef WM_DEVICECHANGE #define WM_DEVICECHANGE #endif #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //#define WINVER 0x0500 extern "C"{ #include "dbt.h" #include "hidsdi.h" #include "setupapi.h" } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDeviceHid2Dlg dialog CDeviceHid2Dlg::CDeviceHid2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDeviceHid2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDeviceHid2Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDeviceHid2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDeviceHid2Dlg) DDX_Control(pDX, IDC_LIST1, m_ResultsList); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDeviceHid2Dlg, CDialog) //{{AFX_MSG_MAP(CDeviceHid2Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDeviceHid2Dlg message handlers BOOL CDeviceHid2Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // hSelectDev = NULL; // TODO: Add extra initialization here Main_OnDeviceChange(WPARAM wParam, LPARAM lParam); //FindDevice(); return TRUE; // return TRUE unless you set the focus to a control } void CDeviceHid2Dlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDeviceHid2Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDeviceHid2Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //Variables globales unsigned int VendorID,ProductID; char temp_buffer[100]; unsigned char DataBuffer[9],BufferLength; int HIDCounter; bool MyDeviceDetected; CString MyDevicePathName; GUID hidGuid; HDEVINFO AllHIDDeviceInfo; HANDLE HIDDevice,ReadHIDDevice; HIDD_ATTRIBUTES Attributes; PSP_DEVICE_INTERFACE_DETAIL_DATA ClassDeviceData; SP_INTERFACE_DEVICE_DATA deviceInfoData; ULONG neededLength,requiredLength; DWORD bytesRead; void CDeviceHid2Dlg::OnButton1() { // TODO: Add your control notification handler code here CDeviceHid2Dlg::FindDevice(); } bool CDeviceHid2Dlg::FindJB8Device() { // This routine uses the Windows API funcitons for HID devices. // It searches the list of HID devices and tries to match the // specified vendor and product IDs. ClassDeviceData = NULL; HIDDevice=NULL; deviceInfoData.cbSize = sizeof(deviceInfoData); // This API call retrieves the GUID for HIDs from the OS. HidD_GetHidGuid(&hidGuid); // This API call gets the device information for all HIDs. AllHIDDeviceInfo=SetupDiGetClassDevs(&hidGuid,NULL,NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); HIDCounter=0; while (TRUE) { // This API call places a detected device in deviceInfoData. // HIDCounter allows the call to iterate through all HID devices. // If the call returns 0, then no more HID devices found. if (!SetupDiEnumDeviceInterfaces(AllHIDDeviceInfo,0,&hidGuid, HIDCounter,&deviceInfoData)) { // No HID devices found for vendor and product ID. // Free device information on HIDs. SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo); return FALSE; } else { // A HID device has been found. Get details on device. // This API call places the details structure in ClassDeviceData. // The first call to SetupDiGetDeviceInterfaceDetail retrieves // the correct structure size in requiredLength, but returns // false. SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData, NULL,0,&requiredLength,NULL); neededLength=requiredLength; ClassDeviceData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(neededLength); ClassDeviceData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); //SetupDiGetDeviceInterfaceDetail function is called again, // with the correct size in neededLength. if (!SetupDiGetDeviceInterfaceDetail(AllHIDDeviceInfo,&deviceInfoData, ClassDeviceData,neededLength,&requiredLength,NULL)) { free(ClassDeviceData); SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo); return FALSE; } // This API call gets a handle to the HID device. HIDDevice=CreateFile(ClassDeviceData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL); // This API call reads the attributes of the device such // as vendor and product IDs/ HidD_GetAttributes(HIDDevice,&Attributes); if ((Attributes.VendorID == VendorID) && (Attributes.ProductID == ProductID)) { // The HID device was found for the corresponding // vendor and product IDs. Set a handle to the HID // device for reading information from it. RegisterForDeviceNotifications(); ReadHIDDevice=CreateFile(ClassDeviceData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL); free(ClassDeviceData); SetupDiDestroyDeviceInfoList(AllHIDDeviceInfo); return TRUE; } else CloseHandle(HIDDevice); free(ClassDeviceData); HIDCounter = HIDCounter+1; } } //return(0); } void CDeviceHid2Dlg::FindDevice() { //m_ResultsList.AddString("USB Hid Demo"); //m_ResultsList.AddString(""); VendorID = 0x0000; ProductID = 0x0000; if (FindJB8Device()==true) { if (FindJB8Device()==true) { //GetDlgItem(ID_READDATA)->EnableWindow(TRUE); //GetDlgItem(ID_FINDDEVICE)->EnableWindow(FALSE); sprintf(temp_buffer,"Device detected for Vendor ID=%04lX Product ID=%04lX.",VendorID,ProductID); m_ResultsList.AddString(temp_buffer); } } else { sprintf(temp_buffer,"Device Vendor ID= Product ID= not detected!",VendorID,ProductID); m_ResultsList.AddString(temp_buffer); //m_ResultsList.AddString(""); //return; } } void CDeviceHid2Dlg::RegisterForDeviceNotifications() { DEV_BROADCAST_DEVICEINTERFACE NotificationFilter; HDEVNOTIFY DeviceNotificationHandle; ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) ); NotificationFilter.dbcc_size = sizeof(NotificationFilter); NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; for(int i=0; iGetSafeHwnd(), &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); } } LRESULT CDeviceHid2Dlg::Main_OnDeviceChange(WPARAM wParam, LPARAM lParam) { //m_ResultsList.AddString("Device change detected."); PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; switch(wParam) { // Find out if a device has been attached or removed. // If yes, see if the name matches the device path name of the device we want to access. case DBT_DEVICEARRIVAL: m_ResultsList.AddString("A device has been attached."); if (DeviceNameMatch(lParam)) { m_ResultsList.AddString("My device has been attached."); } return TRUE; case DBT_DEVICEREMOVECOMPLETE: m_ResultsList.AddString("A device has been removed."); if (DeviceNameMatch(lParam)) { m_ResultsList.AddString("My device has been removed."); // Look for the device on the next transfer attempt. MyDeviceDetected = false; } return TRUE; default: return TRUE; } } BOOL CDeviceHid2Dlg::DeviceNameMatch(LPARAM lParam) { // Compare the device path name of a device recently attached or removed // with the device path name of the device we want to communicate with. PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; m_ResultsList.AddString("MyDevicePathName = " + MyDevicePathName); if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { PDEV_BROADCAST_DEVICEINTERFACE lpdbi = (PDEV_BROADCAST_DEVICEINTERFACE)lParam; CString DeviceNameString; //The dbch_devicetype parameter indicates that the event applies to a device interface. //So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure, //which begins with a DEV_BROADCAST_HDR. //The dbcc_name parameter of DevBroadcastDeviceInterface contains the device name. //Compare the name of the newly attached device with the name of the device //the application is accessing (myDevicePathName). DeviceNameString = lpdbi->dbcc_name; m_ResultsList.AddString("DeviceNameString = " + DeviceNameString); if ((DeviceNameString.CompareNoCase(MyDevicePathName)) == 0) { //The name matches. return true; } else { //It's a different device. return false; } } else { return false; } }