123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #include "stdafx.h"
- #include "Global.h"
- #include <tchar.h>
- #include <direct.h>
- #include <SetupAPI.h>
- #include <InitGuid.h>
- #include <WinIoCtl.h>
- #pragma comment(lib, "SetupAPI.lib")
- // 枚举所有USB设备;
- DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
- #define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
- DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8);
- #define GUID_CLASS_USB_HUB GUID_DEVINTERFACE_USB_HUB
- namespace Global
- {
- //////////////////////////////////////////////////////////////////////////
- // 全局变量;
- HINSTANCE g_hLanguageDLL = NULL;
- TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
- TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
- TCHAR g_szFna[MAX_PATH] = { 0 };
- TCHAR g_szConfig[MAX_PATH] = { 0 };
- TCHAR g_szResuorceCfg[MAX_PATH] = { 0 };
- STConfig g_Config;
- TCHAR g_szVersion[MAX_PATH] = _T("5.50.20200921");
- //////////////////////////////////////////////////////////////////////////
- // 全局函数;
- /************************************************************************/
- /* 函数:WriteTextLog[7/28/2009 Jeff];
- /* 描述:写文本日志;
- /* 参数:;
- /* [IN] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void WriteTextLog(const TCHAR* format, ...)
- {
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
- // 打开或创建文件;
- FILE* fp = NULL;
- //if (_taccess(szlogpath, 0) != -1)
- #ifndef UNICODE
- if (_access(szlogpath, 0) != -1)
- #else
- if (_taccess(szlogpath, 0) != -1)
- #endif
- {// 存在;
- if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
- // 移动到末尾;
- fseek(fp, 0, SEEK_END);
- }
- else
- {// 不存在;
- _tfopen_s(&fp, szlogpath, _T("w+"));
- }
- if (fp == NULL)
- return;
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR* buffer = NULL;
- va_start(args, format);
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
- // 将日志内容输入到文件中;
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
- // 关闭文件,释放资源并设置回原语言区域;
- free(buffer);
- fclose(fp);
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- void WriteTextLogEx(int nType, const TCHAR* format, ...)
- {
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s%s-%d.txt"), g_szCurModuleDir, g_szFna, nType);
- // 打开或创建文件;
- FILE* fp = NULL;
- //if (_taccess(szlogpath, 0) != -1)
- #ifndef UNICODE
- if (_access(szlogpath, 0) != -1)
- #else
- if (_taccess(szlogpath, 0) != -1)
- #endif
- {// 存在;
- if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
- // 移动到末尾;
- fseek(fp, 0, SEEK_END);
- }
- else
- {// 不存在;
- _tfopen_s(&fp, szlogpath, _T("w+"));
- }
- if (fp == NULL)
- return;
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR* buffer = NULL;
- va_start(args, format);
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
- // 将日志内容输入到文件中;
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
- // 关闭文件,释放资源并设置回原语言区域;
- free(buffer);
- fclose(fp);
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- int ReadReg(char* path, char* key, char* value)
- {
- HKEY hKey;
- int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
- if (ret != ERROR_SUCCESS)
- {
- return 1;
- }
- //读取KEY
- DWORD dwType = REG_SZ; //数据类型
- DWORD cbData = 256;
- ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData);
- if (ret != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return 1;
- }
- RegCloseKey(hKey);
- return 0;
- }
- // 通过注册表查找系统当前串口信息;
- BOOL GetSysSerialPort(std::vector<std::string>& vtports)
- {
- HKEY hKey;
- LSTATUS lReg = 0;
- DWORD dwMaxValLen = 0;
- DWORD dwValNum = 0;
- lReg = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Open Registry Error"));
- return FALSE;
- }
- lReg = RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValNum, &dwMaxValLen, NULL, NULL, NULL);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Getting Key Info Error"));
- return FALSE;
- }
- if (vtports.size())
- {
- vtports.clear();
- }
- LPSTR lpValName, lpComNum;
- DWORD dwValName, dwValSize = 6;
- for (DWORD i = 0; i < dwValNum; i++)
- {
- dwValName = dwMaxValLen + 1;
- dwValSize = 6;
- lpValName = (LPSTR)VirtualAlloc(NULL, dwValName, MEM_COMMIT, PAGE_READWRITE);
- lReg = RegEnumValueA(hKey, i, lpValName, &dwValName, NULL, NULL, NULL, NULL);
- if ((lReg != ERROR_SUCCESS) && (lReg != ERROR_NO_MORE_ITEMS))
- {
- //LOG4C((LOG_WARN, "Enum Registry Error or No More Items"));
- continue;
- }
- lpComNum = (LPSTR)VirtualAlloc(NULL, 6, MEM_COMMIT, PAGE_READWRITE);
- lReg = RegQueryValueExA(hKey, lpValName, NULL, NULL, (LPBYTE)lpComNum, &dwValSize);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Can not get the name of the port"));
- continue;
- }
- vtports.push_back(lpComNum);
- VirtualFree(lpValName, 0, MEM_RELEASE);
- VirtualFree(lpComNum, 0, MEM_RELEASE);
- }
- return TRUE;
- }
- // 读取配置文件-预留;
- void GetConfig()
- {
- TCHAR szConfigpath[MAX_PATH] = { 0 };
- _stprintf_s(szConfigpath, _T("%s%s.ini"), g_szCurModuleDir, g_szFna);
- _tcscpy_s(g_szConfig, szConfigpath);
- ASSERT(PathFileExists(szConfigpath));
- if ( !PathFileExists(szConfigpath) )
- {
- AfxMessageBox(_T("The default configuration file is missing"));
- return;
- }
- TCHAR szValue[MAX_PATH] = { 0 };
- g_Config.nLanguage = GetPrivateProfileInt(_T("SATHelper"), _T("Language"), 0, szConfigpath);
- //////////////////////////////////////////////////////////////////////////
- GetPrivateProfileString(_T("TestWizard"), _T("xmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twSignaldir = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("TestWizard"), _T("oldxmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twOldSignaldir = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("TestWizard"), _T("useSignal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twUseSignal = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("TestWizard"), _T("Com"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twPort = szValue;
- GetPrivateProfileString(_T("Switcher"), _T("Com"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.switchPort = szValue;
- if ( !PathFileExists(g_Config.twSignaldir.c_str()) )
- MKDIR(g_Config.twSignaldir.c_str());
- }
- void MKDIR(LPCTSTR dir)
- {
- //////////////////////////////////////////////////////////////////////////
- // 创建目录;
- int nleft = 0;
- int nIndex = -1;
- TString strdir = dir;
- if (strdir.at(strdir.size() - 1) != _T('\\'))
- strdir.append(_T("\\"));
- // 共享路径和硬盘盘符;
- if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
- nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
- else if (strdir.at(2) == _T('\\'))
- nleft = 3;
- do
- {
- nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
- if (nIndex != TString::npos)
- {
- if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
- {
- WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
- break;
- }
- nleft += nIndex + 1;
- }
- } while (nIndex != -1);
- }
- }
|