📄 usbdetectid.cpp
字号:
// UsbDetectID.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "UsbDetectID.h"
//#include "UsbDetectIDDlg.h"
#include "GUIDEMO.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struUSB_ID *g_pUsbID;
UINT g_nTotalDetectNum;
UINT g_nCurUsbId = 0;
/*
struBUTTONSET *g_pButtonSet;
UINT g_nMaxButtonNum;
struMENUSET *g_pMenuSet;
char g_szBKfile[_MAX_PATH] = ""; // 背景图片名称
char g_szExitfile[_MAX_PATH] = ""; // 退出按钮文件名
char g_szSound[_MAX_PATH] = ""; // 声音文件名
*/
BOOL g_bIsOpened = FALSE;
BOOL g_bIsOpenAbout = FALSE;
CPtrArray g_Usb;
VOID EnumerateHostControllers (ULONG *DevicesConnected);
/////////////////////////////////////////////////////////////////////////////
// CUsbDetectIDApp
BEGIN_MESSAGE_MAP(CUsbDetectIDApp, CWinApp)
//{{AFX_MSG_MAP(CUsbDetectIDApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUsbDetectIDApp construction
CUsbDetectIDApp::CUsbDetectIDApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CUsbDetectIDApp object
CUsbDetectIDApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CUsbDetectIDApp initialization
BOOL CUsbDetectIDApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
HANDLE hMutex = ::CreateMutex(NULL, TRUE, "__USB__DETECT_ID__WALLACE_");
if (::WaitForSingleObject(hMutex, 100) != WAIT_OBJECT_0 && GetLastError() == ERROR_ALREADY_EXISTS)
{
return FALSE;
}
char CurrentDirectory[256];
GetCurrentDirectory(255, CurrentDirectory);
char *szTmp = strstr( m_lpCmdLine, "-d");
if (szTmp)
{
if (!SetCurrentDirectory(szTmp+2)) return FALSE;
}
ReadIni();
DWORD WinVersion = GetSystemVersion();
if (WinVersion == WINVER_WIN2K ||
WinVersion == WINVER_WINNT)
{
SearchID();
}
else
{
//CGUIDEMO dlg;
//dlg.DoModal();
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void ReadIni()
{
// 读取usb检测的pid, vid
char *szProfileName = ".\\UsbDetectID.ini";
g_nTotalDetectNum = ::GetPrivateProfileInt ("MAIN", "groupnum", 0, szProfileName);
if (g_nTotalDetectNum < 1 || g_nTotalDetectNum > 10)
{
AfxMessageBox("Read ini file error!");
}
g_pUsbID = new struUSB_ID[g_nTotalDetectNum];
UINT i = 0;
for (i = 0; i < g_nTotalDetectNum; ++i)
{
memset (&g_pUsbID[i], 0, sizeof(struUSB_ID));
}
for (i = 0; i < g_nTotalDetectNum; i++)
{
char szGroupNum[20];
sprintf(szGroupNum, "group%d", i+1);
g_pUsbID[i].m_nVID = GetPrivateProfileInt(szGroupNum, "vid", 0, szProfileName);
g_pUsbID[i].m_nPID = GetPrivateProfileInt(szGroupNum, "pid", 0, szProfileName);
GetPrivateProfileString(szGroupNum, "ini", "", g_pUsbID[i].m_szIniFileName, sizeof(g_pUsbID[i].m_szIniFileName), szProfileName);
g_pUsbID[i].m_bFound = false;
FILE *test_file = fopen (g_pUsbID[i].m_szIniFileName, "r");
if (!test_file)
{
CString strMsg;
strMsg.Format ("Can't open ini file in [%s]", szGroupNum);
AfxMessageBox(strMsg);
exit(99);
}
fclose (test_file);
// 读取背景图
char *szIniName = g_pUsbID[i].m_szIniFileName;
GetPrivateProfileString("MAIN", "background", "", g_pUsbID[i].m_szBKfile, sizeof(g_pUsbID[i].m_szBKfile), szIniName);
// 读取退出按钮文件名
GetPrivateProfileString("MAIN", "exit", "", g_pUsbID[i].m_szExitfile, sizeof(g_pUsbID[i].m_szExitfile), szIniName);
// 读取声音文件
GetPrivateProfileString("MAIN", "sound", "", g_pUsbID[i].m_szSound, sizeof(g_pUsbID[i].m_szSound), szIniName);
// 读取各个功能按钮的设置
g_pUsbID[i].m_nMaxButtonNum = 10;
g_pUsbID[i].m_pButtonSet = new struBUTTONSET[g_pUsbID[i].m_nMaxButtonNum];
for (int j = 0; j < g_pUsbID[i].m_nMaxButtonNum; ++j)
{
memset (&g_pUsbID[i].m_pButtonSet[j], 0, sizeof(struBUTTONSET));
}
for (j = 0; j < g_pUsbID[i].m_nMaxButtonNum; j++)
{
char szGroupNum[20];
sprintf(szGroupNum, "group%d", j+1);
GetPrivateProfileString(szGroupNum, "file", "", g_pUsbID[i].m_pButtonSet[j].m_szBmpFileName, sizeof(g_pUsbID[i].m_pButtonSet[j].m_szBmpFileName), szIniName);
GetPrivateProfileString(szGroupNum, "action", "", g_pUsbID[i].m_pButtonSet[j].m_szact, sizeof(g_pUsbID[i].m_pButtonSet[j].m_szact), szIniName);
}
// 读取各个功能按钮的菜单设置
g_pUsbID[i].m_pMenuSet = new struMENUSET[g_pUsbID[i].m_nMaxButtonNum];
for (j = 0; j < g_pUsbID[i].m_nMaxButtonNum; j++)
{
if (!strncmp (g_pUsbID[i].m_pButtonSet[j].m_szact, "MENU", 4))
{
char szGroupNum[20];
sprintf(szGroupNum, "group%d", j+1);
bool bReadOk;
UINT nMenuIdx = 0;
do
{
bReadOk = false;
char szMenuItemNum[20];
sprintf(szMenuItemNum, "item%d", nMenuIdx+1);
char szMenuActNum[20];
sprintf(szMenuActNum, "act%d", nMenuIdx+1);
char szTmpMenuItem[_MAX_PATH] = "";
char szTmpMenuAct[_MAX_PATH] = "";
GetPrivateProfileString (szGroupNum, szMenuItemNum, "", szTmpMenuItem, sizeof(szTmpMenuItem), szIniName);
GetPrivateProfileString (szGroupNum, szMenuActNum, "", szTmpMenuAct, sizeof(szTmpMenuAct), szIniName);
if (szTmpMenuItem[0] && szTmpMenuAct[0])
{
g_pUsbID[i].m_pMenuSet[j].m_aMenuStr.Add(szTmpMenuItem);
g_pUsbID[i].m_pMenuSet[j].m_aMenuAct.Add(szTmpMenuAct);
bReadOk = true;
nMenuIdx++;
}
}while (bReadOk);
}
}
}
}
void CUsbDetectIDApp::SearchID()
{
g_Usb.RemoveAll();
ULONG devicesConnected;
EnumerateHostControllers ( &devicesConnected);
CString strMsg;
strMsg.Format("Can't find any USB device:\n");
bool bFoundedAnyUsb = false;
for (UINT j = 0; j < g_nTotalDetectNum; ++j)
{
bool bFoundThis = false;
for (UINT i = 0; i < g_Usb.GetSize(); ++i)
{
PUSB_NODE_CONNECTION_INFORMATION pConnectInfo = (PUSB_NODE_CONNECTION_INFORMATION)g_Usb[i];
if (pConnectInfo->ConnectionStatus == DeviceConnected)
{
if (pConnectInfo->DeviceDescriptor.idVendor == g_pUsbID[j].m_nVID &&
pConnectInfo->DeviceDescriptor.idProduct == g_pUsbID[j].m_nPID)
{
// 原先没有的,则置成有新发现
if (!g_pUsbID[j].m_bFound)
{
bFoundThis = true;
g_pUsbID[j].m_bFound = true;
CString strThisMsg;
strThisMsg.Format("Vid = 0x%04x, Pid = 0x%04x\n", g_pUsbID[j].m_nVID, g_pUsbID[j].m_nPID);
strMsg += strThisMsg;
bFoundedAnyUsb = true;
}
// 原先已经有了,不变,表示发现了
else
{
bFoundThis = true;
}
}
}
}
// 如果没有找到这一个,则设置为没有
if (!bFoundThis)
{
g_pUsbID[j].m_bFound = false;
}
}
if (bFoundedAnyUsb)
{
if (!g_bIsOpened)
{
g_bIsOpened = TRUE;
CGUIDEMO dlg;
dlg.DoModal();
g_bIsOpened = FALSE;
}
}
}
#include <windows.h>
#include <stdio.h>
DWORD CUsbDetectIDApp::GetSystemVersion()
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// which is supported on Windows 2000.
//
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return WINVER_WINOTHER;
}
switch (osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
// Test for the product.
if ( osvi.dwMajorVersion <= 4 )
return WINVER_WINNT;
if ( osvi.dwMajorVersion == 5 )
return WINVER_WIN2K;
return WINVER_WINNT;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if ((osvi.dwMajorVersion > 4) ||
((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
{
return WINVER_WIN98;
}
else
return WINVER_WIN95;
break;
case VER_PLATFORM_WIN32s:
return WINVER_WINOTHER;
break;
}
return WINVER_WINOTHER;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -