📄 test3dlg.cpp
字号:
// test3Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "test3.h"
#include "test3Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define NUM_HCS_TO_CHECK 10//指定查找控制器的上限数
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg dialog
CTest3Dlg::CTest3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest3Dlg)
m_showMessage = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest3Dlg)
DDX_Text(pDX, IDC_EDIT1, m_showMessage);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTest3Dlg, CDialog)
//{{AFX_MSG_MAP(CTest3Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTest3Dlg message handlers
BOOL CTest3Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTest3Dlg::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 CTest3Dlg::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 CTest3Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTest3Dlg::ShowMessage(CString str)/////此调用在文本框中显示设备数据
{
m_showMessage += str;
SetDlgItemText(IDC_EDIT1,m_showMessage);
}
void CTest3Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_showMessage.Empty();//每次显示时清空上次数据
CString str;
str="查找所有USB设备:";
ShowMessage(str);
NumUsbAll();//查找调用函数
}
void CTest3Dlg::NumUsbAll()//查找内容
{
CString strShow;
char HCName[16];
HANDLE hHCDev;
PCHAR rootHubName;
PCHAR leafName;
// ULONG index;
int index;
BOOL success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
HANDLE hHubDevice;
for (int HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
{
wsprintf(HCName, "\\\\.\\HCD%d", HCNum);//计算机上的USB主控制器以HCD1,HCD2等命名
//打开控制器
hHCDev = CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hHCDev != INVALID_HANDLE_VALUE)//成功打开
{
strShow.Format("\r\n找到USB控制器:%d\r\n",HCNum+1);
ShowMessage(strShow);
}
else
break;
PCHAR driverKeyName, deviceDesc;
driverKeyName = GetHCDDriverKeyName(hHCDev);//调用GetHCDDriverKeyName函数
if(driverKeyName)
{ //想获得此设备的字符名,但不成功!!有高招请告诉我!
// deviceDesc = DriverNameToDeviceDesc(driverKeyName);
}
else
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
strShow.Format("USB控制器:%s\r\n",driverKeyName);
ShowMessage(strShow);
leafName = HCName + sizeof("\\\\.\\") - sizeof("");///////???
strShow.Format("USB控制器名称:%s\r\n",leafName);
ShowMessage(strShow);
ULONG nBytes;
rootHubName =(char*) GetRootHubName(hHCDev);//调用GetRootHubName函数
if(rootHubName==NULL)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
strShow.Format("USB Hub名称:\r\n%s\r\n",rootHubName);
ShowMessage(strShow);
PUSB_NODE_INFORMATION HubInfo;////////////////////////////???
HubInfo = (PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
PCHAR deviceName;
deviceName = (PCHAR)malloc(strlen(rootHubName) + sizeof("\\\\.\\"));
if (rootHubName != NULL)
{
strcpy(deviceName, "\\\\.\\");
strcpy(deviceName + sizeof("\\\\.\\") - 1, rootHubName);
//打开连接设备
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
free(deviceName);
if (hHubDevice == INVALID_HANDLE_VALUE)//打开失败
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
//得到控制器的驱动程序名,以及与它连接的HUB的名称
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL);
if (!success)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
}
int port;
port=HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;//控制器下的端口数
for (index=1; index <= port; index++)
{
ULONG nBytes;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);///???
if (connectionInfo == NULL)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
connectionInfo->ConnectionIndex = index;
///获得设备更详细的资料
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL);
if (!success)//信息获得失败
{
free(connectionInfo);
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
if(connectionInfo)
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
strShow.Format("第%d端口有USB设备连接\r\n",index);//信息符合
}
else
{
strShow.Format("第%d端口没有USB设备联接\r\n",index);
}
ShowMessage(strShow);
if (connectionInfo->DeviceIsHub)
{
strShow="HUB";
ShowMessage(strShow);
}
deviceDesc = NULL;
if (connectionInfo->ConnectionStatus != NoDeviceConnected)
{
driverKeyName = GetDriverKeyName(hHubDevice,index);//调用GetRootHubName函数
if (driverKeyName)
{
strShow.Format("状态:已连接!\r\n连接设备名:%s\r\n",driverKeyName);
ShowMessage(strShow);
free(driverKeyName);
}
}
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
ShowCenctInfo(connectionInfo);///调用ShowCenctInfo函数
}
}
end:;
}
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
}
PCHAR CTest3Dlg::GetHCDDriverKeyName(HANDLE HCD)///////??取得设备名
{
BOOL success;
ULONG nBytes;
USB_HCD_DRIVERKEY_NAME driverKeyName;
PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
&driverKeyName,
sizeof(driverKeyName),
&driverKeyName,
sizeof(driverKeyName),
&nBytes,
NULL);
if (!success)
{
goto GetHCDDriverKeyNameError;
}
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName))
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameW =(PUSB_HCD_DRIVERKEY_NAME) malloc(nBytes);
if (driverKeyNameW == NULL)
{
goto GetHCDDriverKeyNameError;
}
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
driverKeyNameW,
nBytes,
driverKeyNameW,
nBytes,
&nBytes,
NULL);
if (!success)
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
free(driverKeyNameW);
return driverKeyNameA;
GetHCDDriverKeyNameError:
if (driverKeyNameW != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -