Global.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include <SetupAPI.h>
  4. #include <InitGuid.h>
  5. #include <WinIoCtl.h>
  6. #include <direct.h>
  7. #pragma comment(lib, "SetupAPI.lib")
  8. namespace Global
  9. {
  10. //////////////////////////////////////////////////////////////////////////
  11. // 全局变量;
  12. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  13. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  14. TCHAR g_szFna[MAX_PATH] = { 0 };
  15. TCHAR g_szConfig[MAX_PATH] = { 0 };
  16. TCHAR g_szLogPath[MAX_PATH] = { 0 };
  17. bool g_bEnableLog = false;
  18. time_t g_time = time(NULL);
  19. //////////////////////////////////////////////////////////////////////////
  20. void MKDIR(LPCTSTR dir)
  21. {
  22. //////////////////////////////////////////////////////////////////////////
  23. // 创建目录;
  24. int nleft = 0;
  25. int nIndex = -1;
  26. string strdir = dir;
  27. strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
  28. if (strdir.at(strdir.size() - 1) != _T('\\'))
  29. strdir.append(_T("\\"));
  30. // 共享路径和硬盘盘符;
  31. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  32. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  33. else if (strdir.at(2) == _T('\\'))
  34. nleft = 3;
  35. do
  36. {
  37. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  38. if (nIndex != string::npos)
  39. {
  40. if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  41. {
  42. //WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  43. break;
  44. }
  45. nleft += nIndex + 1;
  46. }
  47. } while (nIndex != -1);
  48. };
  49. // 全局函数;
  50. /************************************************************************/
  51. /* 函数:WriteTextLog[7/28/2009 Jeff];
  52. /* 描述:写文本日志;
  53. /* 参数:;
  54. /* [IN] :;
  55. /* 返回:void;
  56. /* 注意:;
  57. /* 示例:;
  58. /*
  59. /* 修改:;
  60. /* 日期:;
  61. /* 内容:;
  62. /************************************************************************/
  63. void WriteTextLog(const TCHAR* format, ...)
  64. {
  65. // 将日志内容输入到文件中;
  66. // 获取今年年份;
  67. __time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  68. struct tm gmtm = { 0 };
  69. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  70. // 解析出日志路径;
  71. TCHAR szlogpath[MAX_PATH] = { 0 };
  72. _stprintf_s(szlogpath, _T("%sredratlog %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
  73. // 打开或创建文件;
  74. FILE* fp = NULL;
  75. MKDIR(szlogpath);
  76. //if (_taccess(szlogpath, 0) != -1)
  77. #ifndef UNICODE
  78. if (_access(szlogpath, 0) != -1)
  79. #else
  80. if (_taccess(szlogpath, 0) != -1)
  81. #endif
  82. { // 存在;
  83. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  84. // 移动到末尾;
  85. fseek(fp, 0, SEEK_END);
  86. }
  87. else
  88. { // 不存在;
  89. _tfopen_s(&fp, szlogpath, _T("w+"));
  90. }
  91. if (fp == NULL)
  92. return;
  93. // 格式化前设置语言区域;
  94. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  95. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  96. // 格式化日志内容;
  97. va_list args = NULL;
  98. int len = 0;
  99. TCHAR* buffer = NULL;
  100. va_start(args, format);
  101. // _vscprintf doesn't count. terminating '\0'
  102. len = _vsctprintf(format, args) + 1;
  103. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  104. _vstprintf_s(buffer, len, format, args);
  105. _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);
  106. // 关闭文件,释放资源并设置回原语言区域;
  107. free(buffer);
  108. fclose(fp);
  109. _tsetlocale(LC_CTYPE, old_locale);
  110. free(old_locale); //还原区域设定;
  111. }
  112. void WritePythonLog(const TCHAR* format, ...)
  113. {
  114. // 将日志内容输入到文件中;
  115. // 获取今年年份;
  116. __time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  117. struct tm gmtm = { 0 };
  118. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  119. // 解析出日志路径;
  120. //TCHAR szlogpath[MAX_PATH] = { 0 };
  121. //_stprintf_s(szlogpath, _T("%slog\\Serial Port Log %02d%02d.txt"), g_szCurModuleDir, gmtm.tm_mon + 1, gmtm.tm_mday);
  122. // 打开或创建文件;
  123. FILE* fp = NULL;
  124. MKDIR(g_szLogPath);
  125. //if (_taccess(szlogpath, 0) != -1)
  126. #ifndef UNICODE
  127. if (_access(g_szLogPath, 0) != -1)
  128. #else
  129. if (_taccess(g_szLogPath, 0) != -1)
  130. #endif
  131. { // 存在;
  132. if (0 == _tfopen_s(&fp, g_szLogPath, _T("a+")))
  133. // 移动到末尾;
  134. fseek(fp, 0, SEEK_END);
  135. }
  136. else
  137. { // 不存在;
  138. _tfopen_s(&fp, g_szLogPath, _T("w+"));
  139. }
  140. if (fp == NULL)
  141. return;
  142. // 格式化前设置语言区域;
  143. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  144. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  145. // 格式化日志内容;
  146. va_list args = NULL;
  147. int len = 0;
  148. TCHAR* buffer = NULL;
  149. va_start(args, format);
  150. // _vscprintf doesn't count. terminating '\0'
  151. len = _vsctprintf(format, args) + 1;
  152. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  153. _vstprintf_s(buffer, len, format, args);
  154. _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);
  155. // 关闭文件,释放资源并设置回原语言区域;
  156. free(buffer);
  157. fclose(fp);
  158. _tsetlocale(LC_CTYPE, old_locale);
  159. free(old_locale); //还原区域设定;
  160. }
  161. // 去除空格;
  162. std::string& trim(std::string& str)
  163. {
  164. int nIndex = 0;
  165. while ((nIndex = str.find_first_of(' ')) != std::string::npos)
  166. str.erase(nIndex, 1);
  167. return str;
  168. }
  169. void Init()
  170. {
  171. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  172. TCHAR szDir[_MAX_DIR] = { 0 };
  173. TCHAR szExt[_MAX_DIR] = { 0 };
  174. ::GetModuleFileName(NULL, g_szCurModulePath, sizeof(g_szCurModulePath) / sizeof(TCHAR));
  175. _tsplitpath_s(g_szCurModulePath, szDrive, szDir, g_szFna, szExt);
  176. _tcscpy_s(g_szCurModuleDir, szDrive);
  177. _tcscat_s(g_szCurModuleDir, szDir);
  178. SetCurrentDirectory(g_szCurModulePath);
  179. }
  180. // hModule 模块句柄 NULL表示当前模块;
  181. bool GetVersion(IN const TCHAR* fname, OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
  182. {
  183. VS_FIXEDFILEINFO* pVi = NULL;
  184. DWORD dwHandle = 0;
  185. int size = GetFileVersionInfoSize(fname, &dwHandle);
  186. if (size > 0)
  187. {
  188. BYTE* buffer = new BYTE[size];
  189. memset(buffer, 0, size);
  190. if (GetFileVersionInfo(fname, 0, size, buffer))
  191. {
  192. if (VerQueryValue(buffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& size))
  193. {
  194. pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
  195. pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
  196. pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
  197. pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
  198. pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
  199. pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
  200. pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
  201. pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
  202. delete[] buffer;
  203. return true;
  204. }
  205. }
  206. delete[] buffer;
  207. }
  208. return false;
  209. }
  210. BOOL GetVersion(IN HMODULE hModule, OUT DWORD(&dwFVArray)[4], OUT DWORD(&dwPVArray)[4])
  211. {
  212. TCHAR fname[MAX_PATH];
  213. VS_FIXEDFILEINFO* pVi;
  214. DWORD dwHandle;
  215. if (GetModuleFileName(hModule, fname, MAX_PATH))
  216. {
  217. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  218. if (nSize > 0)
  219. {
  220. BYTE* pBuffer = new BYTE[nSize];
  221. memset(pBuffer, 0, nSize);
  222. if (GetFileVersionInfo(fname, 0, nSize, pBuffer))
  223. {
  224. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID*)& pVi, (PUINT)& nSize))
  225. {
  226. dwFVArray[0] = HIWORD(pVi->dwFileVersionMS);
  227. dwFVArray[1] = LOWORD(pVi->dwFileVersionMS);
  228. dwFVArray[2] = HIWORD(pVi->dwFileVersionLS);
  229. dwFVArray[3] = LOWORD(pVi->dwFileVersionLS);
  230. dwPVArray[0] = HIWORD(pVi->dwProductVersionMS);
  231. dwPVArray[1] = LOWORD(pVi->dwProductVersionMS);
  232. dwPVArray[2] = HIWORD(pVi->dwProductVersionLS);
  233. dwPVArray[3] = LOWORD(pVi->dwProductVersionLS);
  234. delete[]pBuffer;
  235. return TRUE;
  236. }
  237. }
  238. if (pBuffer)
  239. delete[]pBuffer;
  240. }
  241. }
  242. return FALSE;
  243. }
  244. WCHAR* ASCII2UNICODE(IN LPCCH lpASCIIStr)
  245. {
  246. if (lpASCIIStr == NULL)
  247. return NULL;
  248. // 获取宽字符字节数;
  249. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  250. if (cchWideChar == 0)
  251. return NULL;
  252. // 转换成宽字符串;
  253. WCHAR* pWideChar = new WCHAR[cchWideChar + 1];
  254. memset(pWideChar, 0, sizeof(WCHAR) * (cchWideChar + 1));
  255. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pWideChar, cchWideChar);
  256. if (nWriteNum != cchWideChar)
  257. {
  258. if (pWideChar)
  259. delete[] pWideChar;
  260. return NULL;
  261. }
  262. return pWideChar;
  263. }
  264. BOOL UNICODE2UTF8(IN LPWCH lpUNICODEStr, OUT string& strResult)
  265. {
  266. if (lpUNICODEStr == NULL)
  267. return FALSE;
  268. // 获取多字节字符字节数;
  269. int cbMultiByte = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  270. if (cbMultiByte == 0)
  271. return FALSE;
  272. // 转换成多字节字符;
  273. CHAR* pResult = new CHAR[cbMultiByte];
  274. memset(pResult, 0, cbMultiByte);
  275. int nWriteNum = WideCharToMultiByte(CP_UTF8, 0, lpUNICODEStr, -1, pResult, cbMultiByte, NULL, NULL);
  276. if (nWriteNum != cbMultiByte)
  277. return FALSE;
  278. strResult = pResult;
  279. if (pResult)
  280. delete[] pResult;
  281. return TRUE;
  282. }
  283. BOOL ASCII2UTF8(IN LPCCH lpASCIIStr, OUT string& strResult)
  284. {
  285. // 将ASCII字符串转成UNICODE字符串;
  286. WCHAR* pWideChar = ASCII2UNICODE(lpASCIIStr);
  287. if (pWideChar == NULL)
  288. return FALSE;
  289. // 再将UICODE转成UTF8;
  290. BOOL bResult = UNICODE2UTF8(pWideChar, strResult);
  291. if (pWideChar)
  292. delete[] pWideChar;
  293. return bResult;
  294. }
  295. string EnCode_UTF8URL(IN const CHAR* pText)
  296. {
  297. string tt = "";
  298. string dd = "";
  299. ASCII2UTF8(pText, tt);
  300. size_t len = tt.length();
  301. for (size_t i = 0; i < len; i++)
  302. {
  303. if (isalnum((BYTE)tt.at(i)))
  304. {
  305. char tempbuff[2] = { 0 };
  306. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  307. dd.append(tempbuff);
  308. }
  309. else if (isspace((BYTE)tt.at(i)))
  310. {
  311. dd.append("+");
  312. }
  313. else
  314. {
  315. char tempbuff[4];
  316. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  317. dd.append(tempbuff);
  318. }
  319. }
  320. return dd;
  321. }
  322. void EnCode_UTF8URL(IN const CHAR* pText, OUT string& strResult)
  323. {
  324. string tt = "";
  325. ASCII2UTF8(pText, tt);
  326. size_t len = tt.length();
  327. for (size_t i = 0; i < len; i++)
  328. {
  329. if (isalnum((BYTE)tt.at(i)))
  330. {
  331. char tempbuff[2] = { 0 };
  332. sprintf_s(tempbuff, "%c", (BYTE)tt.at(i));
  333. strResult.append(tempbuff);
  334. }
  335. else if (isspace((BYTE)tt.at(i)))
  336. {
  337. strResult.append("+");
  338. }
  339. else
  340. {
  341. char tempbuff[4];
  342. sprintf_s(tempbuff, "%%%X%X", ((BYTE)tt.at(i)) >> 4, ((BYTE)tt.at(i)) % 16);
  343. strResult.append(tempbuff);
  344. }
  345. }
  346. }
  347. //做为解Url使用
  348. char CharToInt(char ch)
  349. {
  350. if (ch >= '0' && ch <= '9')return (char)(ch - '0');
  351. if (ch >= 'a' && ch <= 'f')return (char)(ch - 'a' + 10);
  352. if (ch >= 'A' && ch <= 'F')return (char)(ch - 'A' + 10);
  353. return -1;
  354. }
  355. char StrToBin(IN char(&str)[2])
  356. {
  357. char tempWord[2];
  358. char chn;
  359. tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
  360. tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
  361. chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
  362. return chn;
  363. }
  364. CHAR* UNICODE2ASCII(IN LPWCH lpUNICODEStr)
  365. {
  366. if (lpUNICODEStr == NULL)
  367. return NULL;
  368. // 获取多字节字符字节数;
  369. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  370. if (cbMultiByte == 0)
  371. return NULL;
  372. // 转换成多字节字符;
  373. CHAR *pMultiByteStr = new CHAR[cbMultiByte + 1];
  374. memset(pMultiByteStr, 0, cbMultiByte + 1);
  375. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pMultiByteStr, cbMultiByte, NULL, NULL);
  376. if (nWriteNum != cbMultiByte)
  377. {
  378. if (pMultiByteStr)
  379. delete[]pMultiByteStr;
  380. return NULL;
  381. }
  382. pMultiByteStr[cbMultiByte] = '\0';
  383. return pMultiByteStr;
  384. }
  385. BOOL UNICODE2ASCII(IN LPWCH lpUNICODEStr, OUT LPCH pASCIIStr, IN CONST INT& nASCIIStrLen)
  386. {
  387. if (lpUNICODEStr == NULL)
  388. return FALSE;
  389. // 获取多字节字符字节数;
  390. int cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, NULL, 0, NULL, NULL);
  391. if (cbMultiByte == 0 || cbMultiByte >= nASCIIStrLen)
  392. return FALSE;
  393. // 转换成多字节字符;
  394. memset((void*)pASCIIStr, 0, nASCIIStrLen);
  395. int nWriteNum = WideCharToMultiByte(CP_OEMCP, 0, lpUNICODEStr, -1, pASCIIStr, cbMultiByte, NULL, NULL);
  396. if (nWriteNum != cbMultiByte)
  397. {
  398. return FALSE;
  399. }
  400. return TRUE;
  401. }
  402. /************************************************************************/
  403. /* 函数:[7/26/2016 IT];
  404. /* 描述:;
  405. /* 参数:;
  406. /* [IN] :;
  407. /* [OUT] :;
  408. /* [IN/OUT] :;
  409. /* 返回:void;
  410. /* 注意:;
  411. /* 示例:;
  412. /*
  413. /* 修改:;
  414. /* 日期:;
  415. /* 内容:;
  416. /************************************************************************/
  417. string DeCode_URLUNICODE(IN const CHAR* pURLText)
  418. {
  419. string str = pURLText;
  420. string strResult = "";
  421. INT nIndex = 0;
  422. string strTemp = "";
  423. while (str.find("\\u", 0, 2) != string::npos)
  424. {
  425. nIndex = str.find("\\u", 0, 2);
  426. strResult.append(str.substr(0, nIndex));
  427. strTemp = str.substr(nIndex + 2, 4);
  428. str = str.substr(nIndex + 2 + 4);
  429. CHAR szReturn[10] = { 0 };
  430. union __UNION_VAR_INT {
  431. BYTE ch[2];
  432. int value;
  433. }unionVarInt;
  434. unionVarInt.ch[0] = (CharToInt(strTemp.at(2)) << 4) | (CharToInt(strTemp.at(3)) & 0x00FF);
  435. unionVarInt.ch[1] = (CharToInt(strTemp.at(0)) << 4) | (CharToInt(strTemp.at(1)) & 0x00FF);
  436. WCHAR szWide[2] = { 0 };
  437. szWide[0] = unionVarInt.value;
  438. UNICODE2ASCII(szWide, szReturn, 10);
  439. strResult.append(szReturn);
  440. }
  441. strResult.append(str);
  442. return strResult;
  443. }
  444. // 通过注册表查找系统当前串口信息;
  445. BOOL IsValidString(LPCTSTR lpszString)
  446. {
  447. if (lpszString == NULL)
  448. return FALSE;
  449. do
  450. {
  451. // ASCII可显示的字符;
  452. if (*lpszString < 32 || *lpszString > 126)
  453. {
  454. return FALSE;
  455. }
  456. } while (*++lpszString);
  457. return TRUE;
  458. }
  459. } // namespace Global