⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hidviewdlg.cpp

📁 這個程序使用HID類API讀取USB設備資料﹐只列舉出所有HID設備﹐對于非HID類的設備不起作用
💻 CPP
字号:
// HIDViewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HIDView.h"
#include "HIDViewDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define _MaxNoOfCap 50

/////////////////////////////////////////////////////////////////////////////
// CHIDViewDlg dialog

CHIDViewDlg::CHIDViewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHIDViewDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHIDViewDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHIDViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHIDViewDlg)
	DDX_Control(pDX, IDC_SUMMARY_ED, m_edSum);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHIDViewDlg, CDialog)
	//{{AFX_MSG_MAP(CHIDViewDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FIND_BN, OnFindBn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHIDViewDlg message handlers

BOOL CHIDViewDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CHIDViewDlg::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 CHIDViewDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

HANDLE CHIDViewDlg::GetDeviceHandle(GUID guid, HANDLE hDev, DWORD wDevice)
{
    SP_DEVICE_INTERFACE_DATA interfaceDev;
    interfaceDev.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    //Get interface
	DWORD wSize = 0;
    if(!SetupDiEnumDeviceInterfaces(hDev, NULL, &guid, wDevice, &interfaceDev) ||
 	   SetupDiGetDeviceInterfaceDetail(hDev, &interfaceDev, NULL, 0, &wSize, NULL))
		return INVALID_HANDLE_VALUE;

    //Create buffer
	SP_INTERFACE_DEVICE_DETAIL_DATA *pDeviceDetail;
	pDeviceDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*)malloc(wSize);
	pDeviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

    if(!SetupDiGetDeviceInterfaceDetail(hDev, &interfaceDev, pDeviceDetail, wSize, &wSize, NULL))
    {
        free(pDeviceDetail);
        return INVALID_HANDLE_VALUE;
    }
	
    //Get device handle
    HANDLE hDevice = CreateFile(pDeviceDetail->DevicePath, GENERIC_READ,
								FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    free(pDeviceDetail);

    return hDevice;
}

BOOL CHIDViewDlg::GetInterfaceDetails()
{
    //Get HID GUID
    GUID guid;
    HidD_GetHidGuid(&guid);

    //Get all present HID interface
    HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&guid, NULL, NULL,
											   DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
	if(hDeviceInfo==INVALID_HANDLE_VALUE) return FALSE;

	Clear();
	for(DWORD w=0; w<20; w++)
	{
		HANDLE h;
		if((h = GetDeviceHandle(guid, hDeviceInfo, w))!=INVALID_HANDLE_VALUE)
		{

			HIDD_ATTRIBUTES att;
	        if(HidD_GetAttributes(h, &att))
			{
				Show("Vendor ID", att.VendorID);
				Show("Product ID", att.ProductID);
				Show("Version", att.VersionNumber);
				Show("", "");
			}

			wchar_t w[256];
			char c[256];
			//Get Manufacturers name
			if(HidD_GetManufacturerString(h, w, sizeof(w)))
			{
				wcstombs(c, w, 256);
				Show("Manufacturer", c);
			}

			//Get Product name
			if(HidD_GetProductString(h, w, sizeof(w)))
			{
				wcstombs(c, w, 256);
				Show("Product", c);
			}
			
			PHIDP_PREPARSED_DATA pPreData;
			if(HidD_GetPreparsedData(h, &pPreData))
			{
				HIDP_CAPS cap;
				if(HidP_GetCaps(pPreData, &cap)==HIDP_STATUS_SUCCESS)
				{
					Show(&cap);

					HIDP_BUTTON_CAPS BCap[_MaxNoOfCap];
					if(cap.NumberInputButtonCaps>0 &&
					   HidP_GetButtonCaps(HidP_Input, BCap, &cap.NumberInputButtonCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("InputButton", "");
						Show(BCap, cap.NumberInputButtonCaps);
					}

					if(cap.NumberOutputButtonCaps>0 &&
					   HidP_GetButtonCaps(HidP_Output, BCap, &cap.NumberOutputButtonCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("OutputButton", "");
						Show(BCap, cap.NumberOutputButtonCaps);
					}

					if(cap.NumberFeatureButtonCaps>0 &&
					   HidP_GetButtonCaps(HidP_Feature, BCap, &cap.NumberFeatureButtonCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("FeatureButton", "");
						Show(BCap, cap.NumberFeatureButtonCaps);
					}

					HIDP_VALUE_CAPS VCap[_MaxNoOfCap];
					if(cap.NumberInputValueCaps>0 &&
					   HidP_GetValueCaps(HidP_Input, VCap, &cap.NumberInputValueCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("InputValue", "");
						Show(VCap, cap.NumberInputValueCaps);
					}

					if(cap.NumberOutputValueCaps>0 &&
					   HidP_GetValueCaps(HidP_Output, VCap, &cap.NumberOutputValueCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("OutputValue", "");
						Show(VCap, cap.NumberOutputValueCaps);
					}

					if(cap.NumberFeatureValueCaps>0 &&
					   HidP_GetValueCaps(HidP_Feature, VCap, &cap.NumberFeatureValueCaps, pPreData)
					   ==HIDP_STATUS_SUCCESS)
					{
						Show("FeatureValue", "");
						Show(VCap, cap.NumberFeatureValueCaps);
					}
				}
				HidD_FreePreparsedData(pPreData);
			}
			CloseHandle(h);

			Show("", "");
		}
	}
    SetupDiDestroyDeviceInfoList(hDeviceInfo);

	return TRUE;
}

void CHIDViewDlg::OnFindBn() 
{
	// TODO: Add your control notification handler code here
	GetInterfaceDetails();
}

void CHIDViewDlg::Show(CString str, long n)
{
	CString strN;
	strN.Format("%d", n);
	m_edSum.ReplaceSel(str + ":" + strN + "\r\n");
}

void CHIDViewDlg::Show(CString str, CString s)
{
	m_edSum.ReplaceSel(str + ":" + s + "\r\n");
}

void CHIDViewDlg::Clear()
{
	int nStart, nStop;
	m_edSum.GetSel(nStart, nStop);
	m_edSum.SetSel(0, nStop);
	m_edSum.Clear();
}

void CHIDViewDlg::Show(HIDP_CAPS *pCap)
{
	Show("Usage", pCap->Usage);
	Show("UsagePage", pCap->UsagePage);
	Show("InputReportByteLength", pCap->InputReportByteLength);
	Show("OutputReportByteLength", pCap->OutputReportByteLength);
	Show("FeatureReportByteLength", pCap->FeatureReportByteLength);

	Show("NumberLinkCollectionNodes", pCap->NumberLinkCollectionNodes);
					
	Show("NumberInputButtonCaps", pCap->NumberInputButtonCaps);
	Show("NumberInputValueCaps", pCap->NumberInputValueCaps);
	Show("NumberInputDataIndices", pCap->NumberInputDataIndices);

	Show("NumberOutputButtonCaps", pCap->NumberOutputButtonCaps);
	Show("NumberOutputValueCaps", pCap->NumberOutputValueCaps);
	Show("NumberOutputDataIndices", pCap->NumberOutputDataIndices);
					
	Show("NumberFeatureButtonCaps", pCap->NumberFeatureButtonCaps);
	Show("NumberFeatureValueCaps", pCap->NumberFeatureValueCaps);
	Show("NumberFeatureDataIndices", pCap->NumberFeatureDataIndices);

	Show("", "");
}

void CHIDViewDlg::Show(HIDP_BUTTON_CAPS *pBCap, USHORT nNoOfBCap)
{
	for(long n=0; n<nNoOfBCap; n++)
	{
		Show("UsagePage", pBCap->UsagePage);
		Show("ReportID", pBCap->ReportID);
		Show("BitField", pBCap->BitField);
		Show("LinkCollection", pBCap->LinkCollection);
		Show("LinkUsage", pBCap->LinkUsage);
		Show("LinkUsagePage", pBCap->LinkUsagePage);
		Show("IsRange", pBCap->IsRange);
		Show("IsStringRange", pBCap->IsStringRange);
		Show("IsDesignatorRange", pBCap->IsDesignatorRange);
		Show("IsAbsolute", pBCap->IsAbsolute);

		Show("", "");
		pBCap++;
	}
}

void CHIDViewDlg::Show(HIDP_VALUE_CAPS *pVCap, USHORT nNoOfVCap)
{
	for(long n=0; n<nNoOfVCap; n++)
	{
		Show("UsagePage", pVCap->UsagePage);
		Show("ReportID", pVCap->ReportID);
		Show("BitField", pVCap->BitField);
		Show("LinkCollection", pVCap->LinkCollection);
		Show("LinkUsage", pVCap->LinkUsage);
		Show("LinkUsagePage", pVCap->LinkUsagePage);
		Show("IsRange", pVCap->IsRange);
		Show("IsStringRange", pVCap->IsStringRange);
		Show("IsDesignatorRange", pVCap->IsDesignatorRange);
		Show("IsAbsolute", pVCap->IsAbsolute);
		Show("HasNull", pVCap->HasNull);
		Show("BitSize", pVCap->BitSize);
		Show("ReportCount", pVCap->ReportCount);
		Show("LogicalMin", pVCap->LogicalMin);
		Show("LogicalMax", pVCap->LogicalMax);
		Show("PhysicalMin", pVCap->PhysicalMin);
		Show("PhysicalMax", pVCap->PhysicalMax);

		Show("", "");
		pVCap++;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -