📄 hidrwdlg.cpp
字号:
// HIDRWDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HIDRW.h"
#include "HIDRWDlg.h"
#include "objbase.h"
#include "stdio.h"
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CHIDRWDlg dialog
CHIDRWDlg::CHIDRWDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHIDRWDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHIDRWDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHIDRWDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHIDRWDlg)
DDX_Control(pDX, IDC_LIST_INPUTREPORT, m_ctrlList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHIDRWDlg, CDialog)
//{{AFX_MSG_MAP(CHIDRWDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
ON_BN_CLICKED(IDC_BUTTON_READFILE, OnButtonReadfile)
ON_BN_CLICKED(IDC_BUTTON_STOPREAD, OnButtonStopread)
ON_BN_CLICKED(IDC_BUTTON_STARTSCAN, OnButtonStartscan)
ON_BN_CLICKED(IDC_BUTTON_STOPSCAN, OnButtonStopscan)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHIDRWDlg message handlers
BOOL CHIDRWDlg::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
// TODO: Add extra initialization here
ApplicationActive = TRUE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CHIDRWDlg::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 CHIDRWDlg::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 CHIDRWDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHIDRWDlg::OnButtonTest()
{
struct _GUID GUID;
SP_INTERFACE_DEVICE_DATA DeviceInterfaceData;
struct {DWORD cbSize; char DevicePath[256];} FunctionClassDeviceData;
HIDD_ATTRIBUTES HIDAttributes;
SECURITY_ATTRIBUTES SecurityAttributes;
int Device;
HANDLE PnPHandle, HIDHandle;
ULONG BytesReturned;
bool Success, ManufacturerName, ProductName;
WCHAR ManufacturerBuffer[256], ProductBuffer[256];
const WCHAR NotSupplied[] = L"NULL";
// Say Hello to the user
printf("USB Design By Example: Display currently attached Human Interface Devices\n\n");
// Initialize the GUID array and setup the security attributes for Win2000
HidD_GetHidGuid(&GUID);
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = false;
// Get a handle for the Plug and Play node and request currently active devices
PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if (int(PnPHandle) == -1) {
printf("Could not attach to PnP node");
int iRetErr = GetLastError();
return ;
}
// Lets look for a maximum of 20 Devices
for (Device = 0; (Device < 20); Device++) {
// Initialize our data
DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
// Is there a device at this table entry
Success = SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, Device, &DeviceInterfaceData);
if (Success) {
// There is a device here, get it's name
FunctionClassDeviceData.cbSize = 5;
Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, &DeviceInterfaceData,
(PSP_INTERFACE_DEVICE_DETAIL_DATA)&FunctionClassDeviceData, 256, &BytesReturned, NULL);
if (!Success) {
printf("Could not find the system name for this device\n");
int iRetErr = GetLastError();
return ;
}
// Can now open this device
HIDHandle = CreateFile(FunctionClassDeviceData.DevicePath, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, &SecurityAttributes, OPEN_EXISTING, 0, NULL);
if (HIDHandle == INVALID_HANDLE_VALUE)
printf("Could not open HID #%d, Errorcode = %d\n", Device, GetLastError());
else {
// Get the information about this HID
Success = HidD_GetAttributes(HIDHandle, &HIDAttributes);
if (!Success) {
printf("Could not get HID attributes\n");
return; //GetLastError();
}
ManufacturerName = HidD_GetManufacturerString(HIDHandle, ManufacturerBuffer, 256);
ProductName = HidD_GetProductString(HIDHandle, ProductBuffer, 256);
// And display it!
printf("VID = %4.4x, Name = ", HIDAttributes.VendorID);
printf("%ws, ", ManufacturerName ? ManufacturerBuffer : NotSupplied);
printf("PID = %4.4x, Name = ", HIDAttributes.ProductID);
printf("%ws\n", ProductName ? ProductBuffer : NotSupplied);
PHIDP_PREPARSED_DATA PreparsedData;
Success = HidD_GetPreparsedData(HIDHandle, &PreparsedData);
HIDP_CAPS Capabilities;
HidP_GetCaps(PreparsedData, &Capabilities);
DWORD bRead;
CHAR InputReport[128];
Success = ReadFile(HIDHandle, InputReport, Capabilities.InputReportByteLength, &bRead, NULL);
/*
BYTE ReadBuffer[30];// = {};
memset(ReadBuffer, 0x55, 30);
//Success = HidD_SetFeature(HIDHandle, ReadBuffer, Capabilities.FeatureReportByteLength);
Success = HidD_GetFeature(HIDHandle, ReadBuffer, Capabilities.FeatureReportByteLength);
*/
CHAR OutputReport[25];
DWORD bWritten;
OutputReport[0] = 0x00;
OutputReport[1] = 0x11;
OutputReport[2] = 0x22;
OutputReport[3] = 0x33;
Success = WriteFile(HIDHandle, OutputReport, Capabilities.OutputReportByteLength, &bWritten, NULL);
//Success = WriteFile(HIDHandle, OutputReport, 1, &bWritten, NULL);
CloseHandle(HIDHandle);
}
} // if (SetupDiEnumDeviceInterfaces . .
} // for (Device = 0; (Device < 20); Device++)
SetupDiDestroyDeviceInfoList(PnPHandle);
}
void CHIDRWDlg::OnButtonReadfile()
{
/*
HANDLE ThreadHandle;
DWORD ThreadID;
GetDlgItem(IDC_BUTTON_READFILE)->EnableWindow(FALSE);
ThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)StaticIO_Thread,
this, 0, &ThreadID);
*/
}
DWORD CHIDRWDlg::StaticIO_Thread(LPVOID Param)
{
if(Param != NULL)
return ((CHIDRWDlg*)Param)->ReadReport();
else
return -1;
}
DWORD CHIDRWDlg::ReadReport()
{
ULONG Result;
int InputReportLength = Capabilities.InputReportByteLength;
BOOL DeviceDetected = TRUE;
BYTE InputReport[300];
DWORD bRead;
do
{
Result = ReadFile(HIDHandle, InputReport, Capabilities.InputReportByteLength,
&bRead, NULL);
CString str;
str.Format("%02X, %02X, %02X, %02X", InputReport[1], InputReport[2], InputReport[3], InputReport[4]);
m_ctrlList.AddString(str);
m_ctrlList.SetCurSel(m_ctrlList.GetCount()-1);
if(Result == 0)
{
// CloseHandle(HIDHandle);
DeviceDetected = FALSE;
}
}
while((DeviceDetected == TRUE) );//&& (ApplicationActive == TRUE));
/*
if(ApplicationActive == FALSE){
CHAR OutputReport[3];
DWORD bWritten;
OutputReport[0] = 0x00;
OutputReport[1] = 0x02;
bool Success = WriteFile(HIDHandle, OutputReport, Capabilities.OutputReportByteLength, &bWritten, NULL);
CloseHandle(HIDHandle);
SetupDiDestroyDeviceInfoList(PnPHandle);
}
GetDlgItem(IDC_BUTTON_STARTSCAN)->EnableWindow(TRUE);
*/
return 0;
}
void CHIDRWDlg::InitHIDDev()
{
struct _GUID GUID;
SP_INTERFACE_DEVICE_DATA DeviceInterfaceData;
struct {
DWORD cbSize;
char DevicePath[256];
} FunctionClassDeviceData;
HIDD_ATTRIBUTES HIDAttributes;
SECURITY_ATTRIBUTES SecurityAttributes;
int Device;
ULONG BytesReturned;
bool Success, ManufacturerName, ProductName;
WCHAR ManufacturerBuffer[256], ProductBuffer[256];
const WCHAR NotSupplied[] = L"NULL";
// Initialize the GUID array and setup the security attributes for Win2000
HidD_GetHidGuid(&GUID);
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = false;
// Get a handle for the Plug and Play node and request currently active devices
PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
if (int(PnPHandle) == -1) {
printf("Could not attach to PnP node");
int iRetErr = GetLastError();
return ;
}
// Lets look for a maximum of 20 Devices
for (Device = 0; (Device < 20); Device++) {
// Initialize our data
DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
// Is there a device at this table entry
Success = SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, Device, &DeviceInterfaceData);
if (Success) {
// There is a device here, get it's name
FunctionClassDeviceData.cbSize = 5;
Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, &DeviceInterfaceData,
(PSP_INTERFACE_DEVICE_DETAIL_DATA)&FunctionClassDeviceData, 256, &BytesReturned, NULL);
if (!Success) {
printf("Could not find the system name for this device\n");
int iRetErr = GetLastError();
return ;
}
// Can now open this device
HIDHandle = CreateFile(FunctionClassDeviceData.DevicePath, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, &SecurityAttributes, OPEN_EXISTING, 0, NULL);
if (HIDHandle == INVALID_HANDLE_VALUE)
printf("Could not open HID #%d, Errorcode = %d\n", Device, GetLastError());
else {
#if 0
// Get the information about this HID
Success = HidD_GetAttributes(HIDHandle, &HIDAttributes);
if (!Success) {
printf("Could not get HID attributes\n");
return; //GetLastError();
}
ManufacturerName = HidD_GetManufacturerString(HIDHandle, ManufacturerBuffer, 256);
ProductName = HidD_GetProductString(HIDHandle, ProductBuffer, 256);
// And display it!
printf("VID = %4.4x, Name = ", HIDAttributes.VendorID);
printf("%ws, ", ManufacturerName ? ManufacturerBuffer : NotSupplied);
printf("PID = %4.4x, Name = ", HIDAttributes.ProductID);
printf("%ws\n", ProductName ? ProductBuffer : NotSupplied);
#endif
Success = HidD_GetPreparsedData(HIDHandle, &PreparsedData);
HidP_GetCaps(PreparsedData, &Capabilities);
//CloseHandle(HIDHandle);
}
} // if (SetupDiEnumDeviceInterfaces . .
} // for (Device = 0; (Device < 20); Device++)
//SetupDiDestroyDeviceInfoList(PnPHandle);
}
void CHIDRWDlg::CloseHIDDev()
{
if(HIDHandle)
CloseHandle(HIDHandle);
if(PnPHandle)
SetupDiDestroyDeviceInfoList(PnPHandle);
/*
DWORD bRead;
CHAR InputReport[128];
Success = ReadFile(HIDHandle, InputReport, Capabilities.InputReportByteLength, &bRead, NULL);
BYTE ReadBuffer[30];// = {};
memset(ReadBuffer, 0x55, 30);
//Success = HidD_SetFeature(HIDHandle, ReadBuffer, Capabilities.FeatureReportByteLength);
Success = HidD_GetFeature(HIDHandle, ReadBuffer, Capabilities.FeatureReportByteLength);
CHAR OutputReport[3];
DWORD bWritten;
OutputReport[0] = 0;
//Success = WriteFile(HIDHandle, OutputReport, Capabilities.OutputReportByteLength, &bWritten, NULL);
Success = WriteFile(HIDHandle, OutputReport, 1, &bWritten, NULL);
*/
}
void CHIDRWDlg::OnButtonStopread()
{
// ApplicationActive = !ApplicationActive;
// GetDlgItem(IDC_BUTTON_READFILE)->EnableWindow(TRUE);
}
void CHIDRWDlg::OnButtonStartscan()
{
m_ctrlList.ResetContent();
InitHIDDev();
CHAR OutputReport[3];
DWORD bWritten;
OutputReport[0] = 0x00;
OutputReport[1] = 0x01;
bool Success = WriteFile(HIDHandle, OutputReport, Capabilities.OutputReportByteLength, &bWritten, NULL);
HANDLE ThreadHandle;
DWORD ThreadID;
GetDlgItem(IDC_BUTTON_STARTSCAN)->EnableWindow(FALSE);
ApplicationActive = TRUE;
ThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&StaticIO_Thread,
this, 0, &ThreadID);
// CloseHandle(HIDHandle);
// SetupDiDestroyDeviceInfoList(PnPHandle);
}
void CHIDRWDlg::OnButtonStopscan()
{
ApplicationActive = FALSE;
CloseHandle(HIDHandle);
SetupDiDestroyDeviceInfoList(PnPHandle);
GetDlgItem(IDC_BUTTON_STARTSCAN)->EnableWindow(TRUE);
InitHIDDev();
CHAR OutputReport[3];
DWORD bWritten;
OutputReport[0] = 0x00;
OutputReport[1] = 0x02;
bool Success = WriteFile(HIDHandle, OutputReport, Capabilities.OutputReportByteLength, &bWritten, NULL);
CloseHandle(HIDHandle);
SetupDiDestroyDeviceInfoList(PnPHandle);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -