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

📄 hid.cpp

📁 獲得系統中所有的HID設備,本程序在VC6.0中編譯通過。適合初中級用戶使用
💻 CPP
字号:
// Hid.cpp: implementation of the CHid class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GetHid.h"
#include "Hid.h"

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



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHid::CHid()
{
	m_tempDeviceHandle = INVALID_HANDLE_VALUE;
	m_hWnd = NULL;
	m_hUsb = INVALID_HANDLE_VALUE;
}

CHid::~CHid()
{

}

void CHid::OpenHidDevice(HWND hWnd)
{
	m_hWnd =hWnd;
}

int CHid::UsbConnectToHost(DWORD deviceIndex)
{
	GUID hidGuid;
	HDEVINFO hardwareDeviceInfoSet;
	SP_DEVICE_INTERFACE_DATA deviceInterfaceData;

	ULONG requiredsize;
	DWORD result;
	PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
	HidD_GetHidGuid (&hidGuid);

	hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGuid,
												NULL,
												NULL,
												(DIGCF_PRESENT |
												DIGCF_DEVICEINTERFACE));
	deviceInterfaceData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);

	result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
											NULL,
											&hidGuid,
											deviceIndex,
											&deviceInterfaceData);

	if (FALSE == result)
	{
		SetupDiDestroyDeviceInfoList(hardwareDeviceInfoSet);
		return 1;
	}
	
/*
	SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
									&deviceInterfaceData,
									NULL,
									0,
									&requiredsize,
									0);

	deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredsize);
	deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

	if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
		&deviceInterfaceData,
		deviceDetail,
		requiredsize,
		NULL,
		NULL))
	{
		SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
		free (deviceDetail);
		return 2;
	}
*/
	SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
                                     &deviceInterfaceData,
                                     NULL, //interfaceDetail,
                                     0, //interfaceDetailSize,
                                     &requiredsize,
                                     0); //infoData))

    //Allocate the buffer
    deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredsize);
    deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
	
    //Fill the buffer with the device details
    if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
		&deviceInterfaceData,
		deviceDetail,
		requiredsize,
		&requiredsize,
		NULL)) 
    {
        SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
        free (deviceDetail);
		return 2;
    }


	strcpy(m_szDevicePath,(LPCSTR)deviceDetail->DevicePath);
	m_tempDeviceHandle = CreateFile (deviceDetail->DevicePath,
		0,
		FILE_SHARE_READ | FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		FILE_FLAG_OVERLAPPED,
		NULL);

	SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
	free (deviceDetail);

	return 0;
}

int CHid::EnumHidDevice (DWORD status)
{
	DWORD index = 0;
	HIDD_ATTRIBUTES deviceAttributes;
	PHIDP_PREPARSED_DATA PreparsedData;
	HIDP_CAPS m_Capabilities;

	m_deviceInfo.erase(m_deviceInfo.begin(),m_deviceInfo.end());
	int ErrorCode;
	while (1)
	{
		ErrorCode = 0;
		ErrorCode = UsbConnectToHost (index);
		if (1 == ErrorCode)
			break;

		if ( m_tempDeviceHandle != INVALID_HANDLE_VALUE)
		{
			if (!HidD_GetAttributes (m_tempDeviceHandle, &deviceAttributes))
			{
				CloseHandle (m_tempDeviceHandle);
			}
			else
			{
				if ( HidD_GetPreparsedData (m_tempDeviceHandle, &PreparsedData)) 
				{	
					status = HidP_GetCaps (PreparsedData, &m_Capabilities);
					if (HIDP_STATUS_SUCCESS  ==  status)
					{
						//

⌨️ 快捷键说明

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