📄 hidrwdlg.cpp
字号:
// HIDRWDlg.cpp : implementation file
//
#include "stdafx.h"
#include "HIDRW.h"
#include "HIDRWDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define VID 0x0461
#define PID 0x4d03
/////////////////////////////////////////////////////////////////////////////
// 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)
m_TimeEdit = _T("");
//}}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_Text(pDX, IDC_EDIT_TIME, m_TimeEdit);
//}}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_BUTTONCREATE, OnButtoncreate)
ON_WM_TIMER()
ON_BN_CLICKED(IDOK, OnStop)
//}}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
//CTime cNow;
cNow=CTime::GetCurrentTime();
sNow=cNow.Format("%I:%M:%S");
GetDlgItem(IDC_EDIT_TIME)->SetWindowText(sNow);
// GetDlgItem(IDC_STATIC_USB)->EnableWindow(FALSE);
// SetTimer(2,1000,NULL);
// HWND hwnd=GetForegroundWindow();
/*
CDC *hdc=GetDC();
char buf[20];
memset(buf,0,20);
sprintf(buf,"x=%d,y=%d",3,5);
hdc->TextOut(0,0,buf);
ReleaseDC(hdc);
*/
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::OnButtoncreate()
{
for(int a=0;a<4;a++)
{
USBConnectToIthDevice(a);
}
}
void CHIDRWDlg::ShowMessage(CString str)
{
m_showMessage+= str;
SetDlgItemText(IDC_SHOWMESSAGE,m_showMessage);
}
int CHIDRWDlg::USBConnectToIthDevice(DWORD deviceIndex)
{
// GetDlgItem(IDC_STATIC_USB)->EnableWindow(TRUE);
CDC *cdc=GetDC();
char buf[20];
memset(buf,0,20);
sprintf(buf,"x=%d,y=%d",3,5);
cdc->SetTextColor(255);
cdc->TextOut(0,0,buf);
ReleaseDC(cdc);
// TODO: Add your control notification handler code here
HIDD_ATTRIBUTES deviceAttributes;
CString strShow;
m_ErrorCode=0;
GUID hidGUID;
HDEVINFO hardwareDeviceInfoSet;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
ULONG requiredSize;
DWORD result;
PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
PHIDP_PREPARSED_DATA PreparsedData;
//Get the HID GUID value - used as mask to get list of devices
HidD_GetHidGuid (&hidGUID);
//Get a list of devices matching the criteria (hid interface, present)
hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
NULL, // Define no enumerator (global)
NULL, // Define no
(DIGCF_PRESENT | // Only Devices present
DIGCF_DEVICEINTERFACE)); // Function class devices.
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
//Go through the list and get the interface data
result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
NULL, //infoData,
&hidGUID, //interfaceClassGuid,
deviceIndex,
&deviceInterfaceData);
/* Failed to get a device - possibly the index is larger than the number of devices */
if (result == FALSE)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
//MessageBox("fail to get HID guid!");
return 1;
}
else
ShowMessage("\r\nsuccess to get HID guid!\r\n");
//Get the details with null values to get the required size of the buffer
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);
ShowMessage("设备的GUID为:\r\n");
strShow.Format("%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n",
hidGUID.Data1,hidGUID.Data2,hidGUID.Data3,hidGUID.Data4[0],
hidGUID.Data4[1],hidGUID.Data4[2],hidGUID.Data4[3],hidGUID.Data4[4],
hidGUID.Data4[5],hidGUID.Data4[6],hidGUID.Data4[7]);
ShowMessage(strShow);
//Fill the buffer with the device details
if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
&deviceInterfaceData,
deviceDetail,
requiredSize,
&requiredSize,
NULL))
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
return 2;
}
//Open file on the device
//用0去开,条列所有的Device出来。
//strcpy(m_szDevicePath,(LPCSTR)deviceDetail->DevicePath);
m_hidhandle = CreateFile (deviceDetail->DevicePath,
// 0,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0,//FILE_FLAG_OVERLAPPED,
NULL); // No template file
if ( m_hidhandle == INVALID_HANDLE_VALUE)
{
//TRACE("Device List=>CreateFile open error!\n");
MessageBox("CreateFile open error!");
return 0;
}
else
ShowMessage("CreateFile open success!\r\n");
HidD_GetAttributes (m_hidhandle, &deviceAttributes);
CString vid;
vid.Format("VendorID:0x%X\r\n",deviceAttributes.VendorID);
ShowMessage(vid);
vid.Format("productID:0x%X\r\n",deviceAttributes.ProductID);
ShowMessage(vid);
vid.Format("VersionNumber:0x%X\r\n",deviceAttributes.VersionNumber);
ShowMessage(vid+"\r\n");
if (!HidD_GetPreparsedData(m_hidhandle,&PreparsedData))
{
ShowMessage("HidD_GetPreparsedData操作失败!\r\n");
return 0;
}
ShowMessage("HidD_GetPreparsedData操作成功!\r\n");
HIDP_CAPS Capabilities;
HidP_GetCaps(PreparsedData,&Capabilities);
ShowMessage("\r\nHidP_GetCaps操作成功!\r\n");
vid.Format("UsagePage:0x%x\r\n",Capabilities.UsagePage);
ShowMessage(vid);
vid.Format("Usage:0x%x\r\n",Capabilities.Usage);
ShowMessage(vid);
vid.Format("OutputReportByteLength:%d\r\n",Capabilities.OutputReportByteLength);
ShowMessage(vid);
vid.Format("InputReportByteLength:%d\r\n",Capabilities.InputReportByteLength);
ShowMessage(vid);
vid.Format("FeatureReportByteLength:%d\r\n",Capabilities.FeatureReportByteLength);
ShowMessage(vid);
HidD_FreePreparsedData(PreparsedData);
MessageBox("请用bushound查看传送的数据与所用的时间");
// HidP_GetValueCaps(HidP_Output,)
DWORD BWritten=0;
char OutPutReport[1025];
memset(OutPutReport,55,1023);
OutPutReport[0]=0;
char InputReport[1024];
InputReport[0]=0;
memset(InputReport,0,1024);
InputReport[0]=0;
OVERLAPPED HIDOverlapped;
HANDLE hEventObject;
hEventObject=CreateEvent(NULL,TRUE,TRUE,"");
HIDOverlapped.hEvent=hEventObject;
HIDOverlapped.Offset=0;
HIDOverlapped.InternalHigh=0;
/*
if(!WriteFile(m_hidhandle,OutPutReport,
Capabilities.OutputReportByteLength,&BWritten,NULL))
{
ShowMessage("WriteFile 失败!");
return 0;
}
*/
if(!HidD_SetFeature(m_hidhandle,OutPutReport,Capabilities.FeatureReportByteLength))
{
ShowMessage("下传1024 byte操作 失败!");
}
ShowMessage("下传1024 byte操作成功!\r\n");
//BOOL Rgsult=ReadFile(m_hidhandle,InputReport,9,&BWritten,(LPOVERLAPPED)&HIDOverlapped);
/*
if(!ReadFile(m_hidhandle,InputReport,9,&BWritten,NULL))//(LPOVERLAPPED)&HIDOverlapped))
{
ShowMessage("ReadFile 失败!");
return 0;
}
*/
if(!HidD_GetFeature(m_hidhandle,InputReport,Capabilities.FeatureReportByteLength))
{
ShowMessage("上传1024 byte操作失败!");
}
ShowMessage("上传1024 byte操作成功!\r\n");
//if(result==1)
//{
// if(Rgsult==1)
// {
// SetEvent(hEventObject);
/*
ShowMessage("接收上传1024 byte的数据如下!\r\n");
CString a;
for(int i=1;i<1024;i++)
{
a.Format("%x",InputReport[i]);
ShowMessage(a+" ");
}
*/
// CancelIo(m_hidhandle);
// }
// WaitForSingleObject(HIDOverlapped.hEvent,1000);
//}
ShowMessage("\r\n");
//SetTimer(1,1,NULL);
SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
CloseHandle(m_hidhandle);
return 0;
}
void CHIDRWDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)
{
USBConnectToIthDevice(0);
}
if(nIDEvent==2)
{
cNow=CTime::GetCurrentTime();
sNow=cNow.Format("%I:%M:%S");
SetDlgItemText(IDC_EDIT_TIME,sNow);
}
CDialog::OnTimer(nIDEvent);
}
void CHIDRWDlg::OnStop()
{
// TODO: Add your control notification handler code here
KillTimer(1);
m_showMessage="";
ShowMessage("");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -