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

📄 hiddlg.cpp

📁 该软件用于识别HID设备
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			sprintf(buff1,"%d",DetailDataBuffer->DevicePath);
			DevicePathName=buff1;
//			DevicePathName=(LPSTR)(LPCTSTR)DevicePathName;
//			DevicePathName=pwText;
			m_Result.AddString(DevicePathName);

			//调用CreateFile函数,获得设备句柄:HidDevice
			HidDevice=CreateFile(DetailDataBuffer->DevicePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
			DisplayResultOfAPICall("CreateFile");

			buff.Format("Returned handle: %0XH",HidDevice);
			m_Result.AddString(buff);

			//调用HidD_GetAttributes获得HID的VID、PID
			DeviceAttributes.Size=sizeof(DeviceAttributes);
			Result=HidD_GetAttributes(HidDevice,&DeviceAttributes);

			DisplayResultOfAPICall("HidD_GetAttributes");

			if(Result!=0)
				m_Result.AddString("HIDD_ATTRIBUTES structure filled without error.");
			else
				m_Result.AddString("Error is filling HIDD_ATTRIBUTES structure.");

			buff.Format("Structure size: %ld",DeviceAttributes.Size);
			m_Result.AddString(buff);
			buff.Format("Vendor ID: %04X",DeviceAttributes.VendorID);
			m_Result.AddString(buff);
			buff.Format("Product ID: %04X",DeviceAttributes.ProductID);
			m_Result.AddString(buff);
			buff.Format("Version Number: %0X",DeviceAttributes.VersionNumber);
			m_Result.AddString(buff);


			//看看是不是指定的VID、PID
			m_VendorID.GetWindowText(buff);
			MyVendorID=strtol(buff,NULL,16);
			m_ProductID.GetWindowText(buff);
			MyProductID=strtol(buff,NULL,16);
			if(DeviceAttributes.VendorID==MyVendorID && DeviceAttributes.ProductID==MyProductID)
			{
				m_Result.AddString("My device detected");
				m_Result.AddString("--------------------------------------------------------");
				MyDeviceDetected=true;
			}
			else
			{
				Result=CloseHandle(HidDevice);
				DisplayResultOfAPICall("CloseHandle");
				MyDeviceDetected=true;
			}
			//准备查找下一个
			MemberIndex++;

		}
	}	
}


CString CHIDDlg::GetErrorString(long LastError)
{
	long Bytes;
	char ErrorString[129];
	CString M_GetErrorString;

	Bytes=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,LastError,0,ErrorString,128,0);

	return M_GetErrorString;
}

/*CString CHIDDlg::GetDataString(long Address, long Bytes)
{

}*/

void CHIDDlg::OncmdGetCaps() 
{
	// TODO: Add your control notification handler code here
	long Result;                      
	char ppData[29];
	CString buff;
//	HIDP_VALUE_CAPS ValueCaps;

	GetDlgItem(cmdTrans)->EnableWindow(true);
	//调用HidD_GetPreparesedData获得一个缓冲区指针
	Result=HidD_GetPreparsedData(HidDevice,&PreparsedData);
	DisplayResultOfAPICall("HidD_GetPreparesedData");

	RtlMoveMemory(ppData,PreparsedData,30);
	DisplayResultOfAPICall("RltMoveMemory");

	//调用HidP_GetCaps获得HID_CAPS结构数据

	Result=HidP_GetCaps(PreparsedData,&Capabilities);
	DisplayResultOfAPICall("HidP_GetCaps");

	buff.Format("Usage: %0X",Capabilities.Usage);
	m_Result.AddString(buff);

	buff.Format("Usage Page: %0X",Capabilities.UsagePage);
	m_Result.AddString(buff);

	buff.Format("Input Report Byte Length: %d",Capabilities.InputReportByteLength);
	m_Result.AddString(buff);

	buff.Format("Output Report Byte Length: %d",Capabilities.OutputReportByteLength);
	m_Result.AddString(buff);

	buff.Format("Feature Report Byte Length: %d",Capabilities.FeatureReportByteLength);
	m_Result.AddString(buff);

	buff.Format("Number of Link Collection Nodes: %d",Capabilities.NumberLinkCollectionNodes);
	m_Result.AddString(buff);

	buff.Format("Number of Input Button Caps: %d",Capabilities.NumberInputButtonCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Input Value Caps: %d",Capabilities.NumberInputValueCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Input Data Indices: %d",Capabilities.NumberInputDataIndices);
	m_Result.AddString(buff);

	buff.Format("Number of Output Button Caps: %d",Capabilities.NumberOutputButtonCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Output Value Caps: %d",Capabilities.NumberOutputValueCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Output Data Indices: %d",Capabilities.NumberOutputDataIndices);
	m_Result.AddString(buff);

	buff.Format("Number of Feature Button Caps: %d",Capabilities.NumberFeatureButtonCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Feature Value Caps: %d",Capabilities.NumberFeatureValueCaps);
	m_Result.AddString(buff);

	buff.Format("Number of Feature Data Indices: %d",Capabilities.NumberFeatureDataIndices);
	m_Result.AddString(buff);

	//调用HidP_GetValueCaps获得HID能力的数值

//	HidP_GetValueCaps(HidP_Input,&ValueCaps,( PUSHORT )&Capabilities.NumberInputValueCaps,PreparsedData);
	DisplayResultOfAPICall("HidP_GetValueCaps");
	m_Result.AddString("--------------------------------------------------------");




	
}

//从HID读取报表(需保证HID输出报表)
void CHIDDlg::OncmdReceive() 
{
	// TODO: Add your control notification handler code here
//	HWND hWnd;

	//得到HWND
//	hWnd=GetSafeHwnd();


	bend=true;

/*	hDlgWnd=GetSafeHwnd();   
  AfxBeginThread(Thread_Read,(LPVOID)hDlgWnd);   */


	//启动线程
	AfxBeginThread(Thread_Read,0);

}

//将当前日期和时间填写到界面的数据域
void CHIDDlg::OncmdNow() 
{
	// TODO: Add your control notification handler code here
	SYSTEMTIME st;
	CString buff;
	GetLocalTime(&st);


	if(st.wYear-2000<10)
		buff.Format("0%d",st.wYear-2000);
	else
		buff.Format("%d",st.wYear-2000);
	SetDlgItemText(txtYear,buff);

	if(st.wMonth<10)
		buff.Format("0%d",st.wMonth);
	else
		buff.Format("%d",st.wMonth);
	SetDlgItemText(txtMonth,buff);

	if(st.wDay<10)
		buff.Format("0%d",st.wDay);
	else
		buff.Format("%d",st.wDay);
	SetDlgItemText(txtDay,buff);

	if(st.wHour<10)
		buff.Format("0%d",st.wHour);
	else
		buff.Format("%d",st.wHour);
	SetDlgItemText(txtHour,buff);

	if(st.wMinute<10)
		buff.Format("0%d",st.wMinute);
	else
		buff.Format("%d",st.wMinute);
	SetDlgItemText(txtMinute,buff);

	if(st.wSecond<10)
		buff.Format("0%d",st.wSecond);
	else
		buff.Format("%d",st.wSecond);
	SetDlgItemText(txtSecond,buff);

	GetDlgItem(cmdFindHID)->EnableWindow(true); 
}

//输出报表到HID
void CHIDDlg::OncmdTrans() 
{
	// TODO: Add your control notification handler code here
	int Count;
	unsigned long NumberOfBytesWritten;
	BYTE *SendBuffer;
	CString buff;
	long Result;
	
	GetDlgItem(cmdClose)->EnableWindow(true); 
	GetDlgItem(cmdReceive)->EnableWindow(true); 

	SendBuffer=(BYTE*)malloc(Capabilities.InputReportByteLength);

	//填写报表数据到数组SendBuffer
	Count=0;
	SendBuffer[Count]=0;
	Count++;
	SendBuffer[Count]=0x55;
	Count++;
	SendBuffer[Count]=0XAA;
	Count++;
	SendBuffer[Count]=0X1;
	Count++;
	SendBuffer[Count]=0X8;
	Count++;
	m_txtR1.GetWindowText(buff);
	SendBuffer[Count]=(unsigned char)strtol(buff,NULL,16);
	Count++;
	m_txtR2.GetWindowText(buff);
	SendBuffer[Count]=(unsigned char)strtol(buff,NULL,16);
	Count++;
	m_txtYear.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	m_txtMonth.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	m_txtDay.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	m_txtHour.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	m_txtMinute.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	m_txtSecond.GetWindowText(buff);
	SendBuffer[Count]=atoi(buff);
	Count++;
	
	//调用WriteFile函数,发送报表
	NumberOfBytesWritten=0;
	Result=WriteFile(HidDevice,SendBuffer,Capabilities.OutputReportByteLength,&NumberOfBytesWritten,0);
	DisplayResultOfAPICall("WriteFile");
	buff.Format("Output Report: %d bytes",NumberOfBytesWritten);
	m_Result.AddString(buff);
	
}

//关闭设备,释放资源
void CHIDDlg::OncmdClose() 
{
	// TODO: Add your control notification handler code here
	//调用CloseHandle关闭HID
	long Result;

	Result=CloseHandle(HidDevice);
	DisplayResultOfAPICall("CloseHandle(HidDevice)");

	//调用SetupDiDestroyDeviceInfoList和HidD_FreePreparsedData释放占用的资源
	Result=SetupDiDestroyDeviceInfoList(DeviceInfoSet);
	DisplayResultOfAPICall("DestroyDeviceInfoList");
	Result=HidD_FreePreparsedData(PreparsedData);
	DisplayResultOfAPICall("HidD_FreePreparsedData");
	m_Result.ResetContent();

	GetDlgItem(cmdGetCaps)->EnableWindow(false); 
	GetDlgItem(cmdClose)->EnableWindow(false); 
	GetDlgItem(cmdTrans)->EnableWindow(false); 
	GetDlgItem(cmdReceive)->EnableWindow(false); 
	m_VendorID.EnableWindow(true);
	m_ProductID.EnableWindow(true);
	
	
}

UINT Thread_Read(LPVOID p)
{
	unsigned long NumberOfBytesRead;
	BYTE *ReadBuffer;
	CString buff;
	long Result;

	CHIDDlg*dlg=(CHIDDlg*)AfxGetApp()->GetMainWnd();
	dlg->GetDlgItem(cmdClear)->EnableWindow(true); 

	ReadBuffer=(BYTE*)malloc(Capabilities.InputReportByteLength);

	dlg->m_Result.AddString("ReadFile");
//	buff.Format("Input Report %d bytes",&NumberOfBytesRead);
//	dlg->m_Result.AddString(buff);

	while(bend){
	//调用ReadFile函数,读取报表
	Result=ReadFile(HidDevice,ReadBuffer,Capabilities.InputReportByteLength,&NumberOfBytesRead,0);
	
	//将输入报表的数据填写到显示界面的相应数据域
	buff.Format("%0X",ReadBuffer[5]);
	dlg->SetDlgItemText(txtR1,buff);

	buff.Format("%0X",ReadBuffer[6]);
	dlg->SetDlgItemText(txtR2,buff);

	if(!bend) break;

	if(ReadBuffer[7]<10)
		buff.Format("0%d",ReadBuffer[7]);	
	else
		buff.Format("%d",ReadBuffer[7]);
	dlg->SetDlgItemText(txtYear,buff);

	if(ReadBuffer[8]<10)
		buff.Format("0%d",ReadBuffer[8]);	
	else
		buff.Format("%d",ReadBuffer[8]);
	dlg->SetDlgItemText(txtMonth,buff);

	if(ReadBuffer[9]<10)
		buff.Format("0%d",ReadBuffer[9]);	
	else
		buff.Format("%d",ReadBuffer[9]);
	dlg->SetDlgItemText(txtDay,buff);

	if(ReadBuffer[10]<10)
		buff.Format("0%d",ReadBuffer[10]);	
	else
		buff.Format("%d",ReadBuffer[10]);
	dlg->SetDlgItemText(txtHour,buff);

	if(ReadBuffer[11]<10)
		buff.Format("0%d",ReadBuffer[11]);	
	else
		buff.Format("%d",ReadBuffer[11]);
	dlg->SetDlgItemText(txtMinute,buff);

	if(ReadBuffer[12]<10)
		buff.Format("0%d",ReadBuffer[12]);	
	else
	buff.Format("%d",ReadBuffer[12]);
	dlg->SetDlgItemText(txtSecond,buff);	
	}

	//终止线程
	AfxEndThread(0);	
	return 0;
}

⌨️ 快捷键说明

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