Global.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include <tchar.h>
  4. #include <direct.h>
  5. #include <SetupAPI.h>
  6. #include <InitGuid.h>
  7. #include <WinIoCtl.h>
  8. #pragma comment(lib, "SetupAPI.lib")
  9. // 枚举所有USB设备;
  10. DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
  11. #define GUID_CLASS_USB_DEVICE GUID_DEVINTERFACE_USB_DEVICE
  12. DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB, 0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8);
  13. #define GUID_CLASS_USB_HUB GUID_DEVINTERFACE_USB_HUB
  14. namespace Global
  15. {
  16. //////////////////////////////////////////////////////////////////////////
  17. // 全局变量;
  18. HINSTANCE g_hLanguageDLL = NULL;
  19. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  20. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  21. TCHAR g_szFna[MAX_PATH] = { 0 };
  22. TCHAR g_szConfig[MAX_PATH] = { 0 };
  23. TCHAR g_szResuorceCfg[MAX_PATH] = { 0 };
  24. STConfig g_Config;
  25. TCHAR g_szVersion[MAX_PATH] = _T("5.50.20200921");
  26. //////////////////////////////////////////////////////////////////////////
  27. // 全局函数;
  28. /************************************************************************/
  29. /* 函数:WriteTextLog[7/28/2009 Jeff];
  30. /* 描述:写文本日志;
  31. /* 参数:;
  32. /* [IN] :;
  33. /* 返回:void;
  34. /* 注意:;
  35. /* 示例:;
  36. /*
  37. /* 修改:;
  38. /* 日期:;
  39. /* 内容:;
  40. /************************************************************************/
  41. void WriteTextLog(const TCHAR* format, ...)
  42. {
  43. // 解析出日志路径;
  44. TCHAR szlogpath[MAX_PATH] = { 0 };
  45. _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
  46. // 打开或创建文件;
  47. FILE* fp = NULL;
  48. //if (_taccess(szlogpath, 0) != -1)
  49. #ifndef UNICODE
  50. if (_access(szlogpath, 0) != -1)
  51. #else
  52. if (_taccess(szlogpath, 0) != -1)
  53. #endif
  54. {// 存在;
  55. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  56. // 移动到末尾;
  57. fseek(fp, 0, SEEK_END);
  58. }
  59. else
  60. {// 不存在;
  61. _tfopen_s(&fp, szlogpath, _T("w+"));
  62. }
  63. if (fp == NULL)
  64. return;
  65. // 格式化前设置语言区域;
  66. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  67. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  68. // 格式化日志内容;
  69. va_list args = NULL;
  70. int len = 0;
  71. TCHAR* buffer = NULL;
  72. va_start(args, format);
  73. // _vscprintf doesn't count. terminating '\0'
  74. len = _vsctprintf(format, args) + 1;
  75. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  76. _vstprintf_s(buffer, len, format, args);
  77. // 将日志内容输入到文件中;
  78. // 获取今年年份;
  79. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  80. struct tm gmtm = { 0 };
  81. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  82. _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);
  83. // 关闭文件,释放资源并设置回原语言区域;
  84. free(buffer);
  85. fclose(fp);
  86. _tsetlocale(LC_CTYPE, old_locale);
  87. free(old_locale);//还原区域设定;
  88. }
  89. void WriteTextLogEx(int nType, const TCHAR* format, ...)
  90. {
  91. // 解析出日志路径;
  92. TCHAR szlogpath[MAX_PATH] = { 0 };
  93. _stprintf_s(szlogpath, _T("%s%s-%d.txt"), g_szCurModuleDir, g_szFna, nType);
  94. // 打开或创建文件;
  95. FILE* fp = NULL;
  96. //if (_taccess(szlogpath, 0) != -1)
  97. #ifndef UNICODE
  98. if (_access(szlogpath, 0) != -1)
  99. #else
  100. if (_taccess(szlogpath, 0) != -1)
  101. #endif
  102. {// 存在;
  103. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  104. // 移动到末尾;
  105. fseek(fp, 0, SEEK_END);
  106. }
  107. else
  108. {// 不存在;
  109. _tfopen_s(&fp, szlogpath, _T("w+"));
  110. }
  111. if (fp == NULL)
  112. return;
  113. // 格式化前设置语言区域;
  114. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  115. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  116. // 格式化日志内容;
  117. va_list args = NULL;
  118. int len = 0;
  119. TCHAR* buffer = NULL;
  120. va_start(args, format);
  121. // _vscprintf doesn't count. terminating '\0'
  122. len = _vsctprintf(format, args) + 1;
  123. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  124. _vstprintf_s(buffer, len, format, args);
  125. // 将日志内容输入到文件中;
  126. // 获取今年年份;
  127. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  128. struct tm gmtm = { 0 };
  129. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  130. _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);
  131. // 关闭文件,释放资源并设置回原语言区域;
  132. free(buffer);
  133. fclose(fp);
  134. _tsetlocale(LC_CTYPE, old_locale);
  135. free(old_locale);//还原区域设定;
  136. }
  137. int ReadReg(char* path, char* key, char* value)
  138. {
  139. HKEY hKey;
  140. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
  141. if (ret != ERROR_SUCCESS)
  142. {
  143. return 1;
  144. }
  145. //读取KEY
  146. DWORD dwType = REG_SZ; //数据类型
  147. DWORD cbData = 256;
  148. ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData);
  149. if (ret != ERROR_SUCCESS)
  150. {
  151. RegCloseKey(hKey);
  152. return 1;
  153. }
  154. RegCloseKey(hKey);
  155. return 0;
  156. }
  157. // 通过注册表查找系统当前串口信息;
  158. BOOL GetSysSerialPort(std::vector<std::string>& vtports)
  159. {
  160. HKEY hKey;
  161. LSTATUS lReg = 0;
  162. DWORD dwMaxValLen = 0;
  163. DWORD dwValNum = 0;
  164. lReg = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
  165. if (lReg != ERROR_SUCCESS)
  166. {
  167. //LOG4C((LOG_WARN, "Open Registry Error"));
  168. return FALSE;
  169. }
  170. lReg = RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValNum, &dwMaxValLen, NULL, NULL, NULL);
  171. if (lReg != ERROR_SUCCESS)
  172. {
  173. //LOG4C((LOG_WARN, "Getting Key Info Error"));
  174. return FALSE;
  175. }
  176. if (vtports.size())
  177. {
  178. vtports.clear();
  179. }
  180. LPSTR lpValName, lpComNum;
  181. DWORD dwValName, dwValSize = 6;
  182. for (DWORD i = 0; i < dwValNum; i++)
  183. {
  184. dwValName = dwMaxValLen + 1;
  185. dwValSize = 6;
  186. lpValName = (LPSTR)VirtualAlloc(NULL, dwValName, MEM_COMMIT, PAGE_READWRITE);
  187. lReg = RegEnumValueA(hKey, i, lpValName, &dwValName, NULL, NULL, NULL, NULL);
  188. if ((lReg != ERROR_SUCCESS) && (lReg != ERROR_NO_MORE_ITEMS))
  189. {
  190. //LOG4C((LOG_WARN, "Enum Registry Error or No More Items"));
  191. continue;
  192. }
  193. lpComNum = (LPSTR)VirtualAlloc(NULL, 6, MEM_COMMIT, PAGE_READWRITE);
  194. lReg = RegQueryValueExA(hKey, lpValName, NULL, NULL, (LPBYTE)lpComNum, &dwValSize);
  195. if (lReg != ERROR_SUCCESS)
  196. {
  197. //LOG4C((LOG_WARN, "Can not get the name of the port"));
  198. continue;
  199. }
  200. vtports.push_back(lpComNum);
  201. VirtualFree(lpValName, 0, MEM_RELEASE);
  202. VirtualFree(lpComNum, 0, MEM_RELEASE);
  203. }
  204. return TRUE;
  205. }
  206. // 读取配置文件-预留;
  207. void GetConfig()
  208. {
  209. TCHAR szConfigpath[MAX_PATH] = { 0 };
  210. _stprintf_s(szConfigpath, _T("%s%s.ini"), g_szCurModuleDir, g_szFna);
  211. _tcscpy_s(g_szConfig, szConfigpath);
  212. ASSERT(PathFileExists(szConfigpath));
  213. if ( !PathFileExists(szConfigpath) )
  214. {
  215. AfxMessageBox(_T("The default configuration file is missing"));
  216. return;
  217. }
  218. TCHAR szValue[MAX_PATH] = { 0 };
  219. g_Config.nLanguage = GetPrivateProfileInt(_T("SATHelper"), _T("Language"), 0, szConfigpath);
  220. //////////////////////////////////////////////////////////////////////////
  221. GetPrivateProfileString(_T("TestWizard"), _T("xmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
  222. g_Config.twSignaldir = szValue;
  223. memset(szValue, 0, MAX_PATH);
  224. GetPrivateProfileString(_T("TestWizard"), _T("oldxmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
  225. g_Config.twOldSignaldir = szValue;
  226. memset(szValue, 0, MAX_PATH);
  227. GetPrivateProfileString(_T("TestWizard"), _T("useSignal"), NULL, szValue, MAX_PATH, szConfigpath);
  228. g_Config.twUseSignal = szValue;
  229. memset(szValue, 0, MAX_PATH);
  230. GetPrivateProfileString(_T("TestWizard"), _T("Com"), NULL, szValue, MAX_PATH, szConfigpath);
  231. g_Config.twPort = szValue;
  232. GetPrivateProfileString(_T("Switcher"), _T("Com"), NULL, szValue, MAX_PATH, szConfigpath);
  233. g_Config.switchPort = szValue;
  234. if ( !PathFileExists(g_Config.twSignaldir.c_str()) )
  235. MKDIR(g_Config.twSignaldir.c_str());
  236. }
  237. void MKDIR(LPCTSTR dir)
  238. {
  239. //////////////////////////////////////////////////////////////////////////
  240. // 创建目录;
  241. int nleft = 0;
  242. int nIndex = -1;
  243. TString strdir = dir;
  244. if (strdir.at(strdir.size() - 1) != _T('\\'))
  245. strdir.append(_T("\\"));
  246. // 共享路径和硬盘盘符;
  247. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  248. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  249. else if (strdir.at(2) == _T('\\'))
  250. nleft = 3;
  251. do
  252. {
  253. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  254. if (nIndex != TString::npos)
  255. {
  256. if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  257. {
  258. WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  259. break;
  260. }
  261. nleft += nIndex + 1;
  262. }
  263. } while (nIndex != -1);
  264. }
  265. }