Global.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "StdAfx.h"
  2. #include "Global.h"
  3. #define VLEN 10
  4. namespace GLOBAL
  5. {
  6. TCHAR g_szModulePath[MAX_PATH] = _T(""); // 软件目录;
  7. TCHAR g_szModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  8. void Init()
  9. {
  10. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  11. TCHAR szDir[_MAX_DIR] = { 0 };
  12. TCHAR szFna[_MAX_DIR] = { 0 };
  13. TCHAR szExt[_MAX_DIR] = { 0 };
  14. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  15. _stprintf_s(g_szModuleFileName, _T("%s"), g_szModulePath);
  16. _tsplitpath_s(g_szModulePath, szDrive, szDir, szFna, szExt);
  17. _tcscpy_s(g_szModulePath, szDrive);
  18. _tcscat_s(g_szModulePath, szDir);
  19. }
  20. // 调用批处理,返回的是多字节字符;
  21. BOOL StartProcess(LPCTSTR lpApplicationName, LPCTSTR lpCommandLine, CString &strStdOutput)
  22. {
  23. HANDLE hRead,hWrite;
  24. SECURITY_ATTRIBUTES sa;
  25. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  26. sa.lpSecurityDescriptor = NULL; //使用系统默认的安全描述符
  27. sa.bInheritHandle = TRUE; //创建的进程继承句柄
  28. if (!CreatePipe(&hRead,&hWrite,&sa,0)) //创建匿名管道
  29. {
  30. MessageBox(NULL, _T("CreatePipe Failed!"), _T("提示"), MB_OK | MB_ICONWARNING);
  31. return FALSE;
  32. }
  33. STARTUPINFO si;
  34. PROCESS_INFORMATION pi;
  35. ZeroMemory(&si,sizeof(STARTUPINFO));
  36. si.cb = sizeof(STARTUPINFO);
  37. GetStartupInfo(&si);
  38. si.hStdError = hWrite;
  39. si.hStdOutput = hWrite; //新创建进程的标准输出连在写管道一端
  40. si.wShowWindow = SW_HIDE; //隐藏窗口
  41. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  42. //si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  43. TCHAR szCommandLine[MAX_PATH] = {0};
  44. if ( lpCommandLine ) _stprintf_s(szCommandLine, _T("%s"), lpCommandLine);
  45. if (!CreateProcess(lpApplicationName, lpCommandLine ? szCommandLine : NULL, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) //创建子进程
  46. {
  47. MessageBox(NULL, _T("CreateProcess Failed!"), _T("提示"), MB_OK | MB_ICONWARNING);
  48. return FALSE;
  49. }
  50. dprintf("进程ID=%ld", pi.dwProcessId);
  51. // 等待进程完成退出.
  52. WaitForSingleObject(pi.hProcess,INFINITE);
  53. CloseHandle(hWrite); //关闭管道句柄
  54. TCHAR szBuffer[4096] = {0};
  55. DWORD bytesRead;
  56. while (true)
  57. {
  58. if (ReadFile(hRead,szBuffer,4096*sizeof(TCHAR),&bytesRead,NULL) == NULL) //读取管道
  59. break;
  60. strStdOutput += szBuffer;
  61. Sleep(100);
  62. }
  63. dprintf("输出内容=%s", strStdOutput.GetString());
  64. CloseHandle(hRead);
  65. return TRUE;
  66. }
  67. CString ReadFileContent(LPCTSTR lpFileName)
  68. {
  69. CString strData = _T("");
  70. if ( !lpFileName || !PathFileExists(lpFileName) )
  71. return strData;
  72. CFile file;
  73. CFileException exp;
  74. std::string strRead;
  75. if ( file.Open(lpFileName, CFile::modeRead, &exp) )
  76. {
  77. DWORD dwRead = 0;
  78. DWORD dwCount = 0;
  79. TCHAR szRead[MAX_PATH] = {0};
  80. DWORD dwLen = file.GetLength();
  81. while ( dwCount = file.Read(szRead, MAX_PATH) )
  82. {
  83. strRead.append(szRead, dwCount);
  84. }
  85. file.Close();
  86. }
  87. return CString(strRead.c_str());
  88. }
  89. void GetStringList(CString strData, CString strSplit, CStringList &ListStr)
  90. {
  91. int nPos = -1;
  92. CString strTemp;
  93. ListStr.RemoveAll();
  94. do
  95. {
  96. nPos = strData.Find(strSplit);
  97. if ( nPos != -1 )
  98. {
  99. strTemp = strData.Mid(0, nPos);
  100. ListStr.AddTail(strTemp);
  101. strData = strData.Mid(nPos+strSplit.GetLength());
  102. }
  103. } while (nPos != -1);
  104. }
  105. BOOL FindString(CStringList &ListStr, CString strFind, CString &strResult)
  106. {
  107. CString strTemp;
  108. POSITION pos = ListStr.GetHeadPosition();
  109. while (pos != NULL)
  110. {
  111. strTemp = ListStr.GetNext(pos);
  112. if ( strTemp.Find(strFind) != -1 )
  113. {
  114. strResult = strTemp;
  115. return TRUE;
  116. }
  117. }
  118. return FALSE;
  119. }
  120. INT CheckListData(CStringList &listStr)
  121. {
  122. INT nPassCount = 0;
  123. INT nCount = listStr.GetSize();
  124. if ( nCount == 0 ) return nPassCount;
  125. CString strLevel = _T(""), strDist = _T("");
  126. POSITION pos = listStr.GetHeadPosition();
  127. while (pos != NULL)
  128. {
  129. strDist = listStr.GetNext(pos);
  130. if ( pos == NULL )
  131. return nPassCount;
  132. strLevel = listStr.GetNext(pos);
  133. TCHAR szDist[5][VLEN] = {0};
  134. TCHAR szLevel[5][VLEN] = {0};
  135. if (_stscanf_s(strDist.GetString(), _T("%*[^=]=%[^,],%[^,],%[^,],%[^,],%s"), szDist[0], VLEN, szDist[1], VLEN, szDist[2], VLEN, szDist[3], VLEN, szDist[4], VLEN) == 5 )
  136. {
  137. if (_stscanf_s(strLevel.GetString(), _T("%*[^=]=%[^,],%[^,],%[^,],%[^,],%s"), szLevel[0], VLEN, szLevel[1], VLEN, szLevel[2], VLEN, szLevel[3], VLEN, szLevel[4], VLEN) == 5 )
  138. {
  139. std::bitset<5> bitDist(0);
  140. std::bitset<5> bitLevel(0);
  141. int nValue = 0;
  142. for ( int i = 0; i < 5; i++ )
  143. {
  144. nValue = _tstoi(szDist[i]);
  145. bitDist[i] = (nValue > 80 && nValue < 130) ? 1 : 0;
  146. nValue = _tstoi(szLevel[i]);
  147. bitLevel[i] = (nValue > 25) ? 1 : 0;
  148. }
  149. int nDistValue = bitDist.to_ulong();
  150. int nLevelValue = bitLevel.to_ulong();
  151. if ( (nDistValue & nLevelValue) > 0 )
  152. {
  153. nPassCount++;
  154. dprintf("PASS");
  155. }
  156. else
  157. {
  158. dprintf(_T("dist=[%s][%s][%s][%s][%s],[%s],[%ld]"), szDist[0],szDist[1],szDist[2],szDist[3],szDist[4], bitDist.to_string().c_str(), bitDist.to_ulong());
  159. dprintf(_T("level=[%s][%s][%s][%s][%s],[%s],[%ld]"), szLevel[0],szLevel[1],szLevel[2],szLevel[3],szLevel[4], bitLevel.to_string().c_str(), bitLevel.to_ulong());
  160. dprintf("FAIL,%s,%s", bitDist.to_string().c_str(), bitLevel.to_string().c_str());
  161. }
  162. }
  163. else
  164. {
  165. dprintf("失败:%s", strLevel.GetString());
  166. }
  167. }
  168. else
  169. {
  170. dprintf("失败:%s", strDist.GetString());
  171. }
  172. }
  173. return nPassCount;
  174. }
  175. BOOL SaveList(CStringList &ListStr)
  176. {
  177. CFile file;
  178. INT nMark = 0;
  179. CFileException exp;
  180. TCHAR szSaveFile[MAX_PATH] = {0};
  181. _stprintf_s(szSaveFile, _T("%s%s"), GLOBAL::g_szModulePath, _T("result.csv"));
  182. if ( !PathFileExists(szSaveFile) )
  183. nMark = 1;
  184. if ( file.Open(szSaveFile, CFile::modeCreate|CFile::modeWrite, &exp) )
  185. {
  186. file.SeekToBegin();
  187. CString strValue = _T("DSN,Log,Percent,Result\r");
  188. file.Write(strValue.GetString(), strValue.GetLength()*sizeof(TCHAR));
  189. POSITION pos = ListStr.GetHeadPosition();
  190. while (pos != NULL)
  191. {
  192. strValue = ListStr.GetNext(pos) + _T("\r");
  193. file.Write(strValue.GetString(), strValue.GetLength()*sizeof(TCHAR));
  194. }
  195. file.Flush();
  196. file.Close();
  197. return TRUE;
  198. }
  199. return FALSE;
  200. }
  201. static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  202. {
  203. switch (uMsg)
  204. {
  205. case BFFM_INITIALIZED:
  206. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)GLOBAL::g_szModulePath);
  207. break;
  208. case BFFM_SELCHANGED:
  209. {
  210. TCHAR szFileName[MAX_PATH];
  211. LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
  212. SHGetPathFromIDList(pidlCurrent, szFileName);
  213. SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
  214. }
  215. break;
  216. // 这个不能让默认目录为桌面;
  217. // case BFFM_INITIALIZED:
  218. // {
  219. // // WParam is TRUE since you are passing a path.
  220. // // It would be FALSE if you were passing a pidl.
  221. // TCHAR szDir[MAX_PATH] = { 0 };
  222. // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
  223. // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
  224. // }
  225. // break;
  226. }
  227. return 0;
  228. }
  229. BOOL GetFilePath(OUT TCHAR *FilePath, IN HWND hParentWnd, IN TCHAR* InstructionString, IN UINT ulFlags)
  230. {
  231. BROWSEINFO bInfo;
  232. LPITEMIDLIST pidl, pidlDesktop;
  233. ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
  234. HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
  235. if ( hr != S_OK )
  236. {
  237. return FALSE;
  238. }
  239. bInfo.pidlRoot = pidlDesktop;
  240. bInfo.hwndOwner = hParentWnd;
  241. bInfo.pszDisplayName = FilePath;
  242. bInfo.lpszTitle = InstructionString;
  243. bInfo.ulFlags = ulFlags;
  244. bInfo.lpfn = BrowseCallbackProc;
  245. bInfo.lParam = 0;
  246. if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
  247. {
  248. return FALSE;
  249. }
  250. if (::SHGetPathFromIDList(pidl, FilePath) == FALSE)
  251. {
  252. return FALSE;
  253. }
  254. return TRUE;
  255. }
  256. };