Global.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. #include "StdAfx.h"
  2. #include "Global.h"
  3. // 获取文件版本号函数头文件;
  4. #include <WinVer.h>
  5. #pragma comment(lib,"version.lib")
  6. using namespace std;
  7. #include <psapi.h>
  8. #pragma comment(lib,"Psapi.lib")
  9. #include <locale.h>
  10. #include <io.h>//_access头文件;
  11. // 获取本机ip地址;
  12. #include <Iphlpapi.h>
  13. #pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
  14. namespace GLOBAL
  15. {
  16. CDataImpl g_db;
  17. CIOCPPipe g_IOCP;
  18. CONFIG g_config;
  19. TCHAR g_szModulePath[MAX_PATH] = _T(""); // 软件目录;
  20. TCHAR g_szModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  21. TCHAR g_szConfigFile[MAX_PATH] = _T("");
  22. PROC_WND_INFO g_procWndInfo;
  23. // DLL注入状态;
  24. BOOL g_bInjected = FALSE;
  25. #define TRACE4(sz, p1, p2, p3, p4) TRACE(_T(sz), p1, p2, p3, p4)
  26. BOOL GetConfigInfo(LPCTSTR lpIniDir /* = NULL */, LPCTSTR lpConfigName /* = NULL */)
  27. {
  28. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  29. TCHAR szDir[_MAX_DIR] = { 0 };
  30. TCHAR szFna[_MAX_DIR] = { 0 };
  31. TCHAR szExt[_MAX_DIR] = { 0 };
  32. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  33. _stprintf_s(g_szModuleFileName, _T("%s"), g_szModulePath);
  34. _tsplitpath_s(g_szModulePath, szDrive, szDir, szFna, szExt);
  35. _tcscpy_s(g_szModulePath, szDrive);
  36. _tcscat_s(g_szModulePath, szDir);
  37. if (lpIniDir != NULL && lpConfigName != NULL)
  38. _stprintf_s(g_szConfigFile, _T("%s%s"), lpIniDir, lpConfigName);
  39. else
  40. _stprintf_s(g_szConfigFile, _T("%s%s.ini"), g_szModulePath, szFna);
  41. HANDLE hFile = CreateFile(g_szConfigFile, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  42. if (ERROR_FILE_NOT_FOUND == GetLastError())
  43. {
  44. return FALSE;
  45. }
  46. CloseHandle(hFile);
  47. hFile = NULL;
  48. #pragma region 获取配置文件信息;
  49. TCHAR szValue[MAX_PATH] = {0};
  50. // LOGIN;
  51. GetPrivateProfileString(_T("LOGIN"), _T("account"), _T(""), g_config.szAccount, MAX_PATH, g_szConfigFile);
  52. GetPrivateProfileString(_T("LOGIN"), _T("password"), _T(""), g_config.szPassword, MAX_PATH, g_szConfigFile);
  53. GetPrivateProfileString(_T("LOGIN"), _T("line"), _T(""), g_config.szLine, MAX_PATH, g_szConfigFile);
  54. g_config.nOffline = GetPrivateProfileInt(_T("LOGIN"), _T("offline"), 0, g_szConfigFile);
  55. // SYSTEM;
  56. g_config.nStdOut = GetPrivateProfileInt(_T("SYSTEM"), _T("stdprint"), 0, g_szConfigFile);
  57. GetPrivateProfileString(_T("SYSTEM"), _T("OGCToolPath"), _T(""), szValue, MAX_PATH, g_szConfigFile);
  58. if ( _tcslen(szValue) > 8 )
  59. {
  60. if ( szValue[1] != _T(':') )
  61. {
  62. _stprintf_s(g_config.szOGCToolProgram, _T("%s%s"), g_szModulePath, szValue);
  63. }
  64. else
  65. {
  66. memcpy(g_config.szOGCToolProgram, szValue, sizeof(TCHAR)*MAX_PATH);
  67. }
  68. }
  69. TCHAR *pBuff = _tcsstr(g_config.szOGCToolProgram, _T("\\Demo.exe"));
  70. if ( pBuff )
  71. {
  72. //int len = pBuff - g_config.szOGCToolProgram;
  73. memcpy(g_config.szOGCToolPath, g_config.szOGCToolProgram, sizeof(TCHAR)*(pBuff - g_config.szOGCToolProgram));
  74. }
  75. // BATCH;
  76. g_config.nDeltaEType = GetPrivateProfileInt(_T("BATCH"), _T("EType"), -1, g_szConfigFile);
  77. GetPrivateProfileString(_T("BATCH"), _T("EValue"), _T(""), g_config.szDeltaEValue, MAX_PATH, g_szConfigFile);
  78. g_config.dDeltaEValue = _tstof(g_config.szDeltaEValue);
  79. GetPrivateProfileString(_T("BATCH"), _T("batchNumber"), _T(""), g_config.szBatchNumber, MAX_PATH, g_szConfigFile);
  80. #pragma endregion
  81. if ( g_config.nStdOut ) {
  82. AllocConsole(); // 开辟控制台;
  83. SetConsoleTitle(_T("调试输出")); // 设置控制台窗口标题;
  84. freopen("CONOUT$", "w+t", stdout); // 重定向输出;
  85. freopen("CONIN$", "r+t", stdin); // 重定向输入;
  86. HWND hWnd = NULL;
  87. again:
  88. hWnd = ::FindWindow(NULL, _T("调试输出"));
  89. if( hWnd ) {
  90. if (!::SetWindowPos(hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE)) {
  91. _tprintf_s(_T("前置设置失败\n"));
  92. } else {
  93. _tprintf_s(_T("前置设置成功\n"));
  94. }
  95. } else {
  96. goto again;
  97. }
  98. }
  99. return TRUE;
  100. }
  101. std::string GetFileData(LPCTSTR lpFile)
  102. {
  103. if ( !lpFile || lpFile[0] == '\0' || !PathFileExists(lpFile) )
  104. return std::string();
  105. CFile file;
  106. CFileException exp;
  107. if ( file.Open(lpFile, CFile::modeRead, &exp) )
  108. {
  109. DWORD dwRead = 0;
  110. DWORD dwCount = 0;
  111. std::string strData;
  112. char szRead[MAX_PATH] = {0};
  113. DWORD dwLen = file.GetLength();
  114. while ( dwCount < dwLen )
  115. {
  116. dwRead = file.Read(szRead, MAX_PATH);
  117. strData.append(szRead, dwRead);
  118. dwCount += dwRead;
  119. }
  120. file.Close();
  121. return strData;
  122. }
  123. return std::string();
  124. }
  125. std::string GetFileLine(std::string &strFileData, int nLine)
  126. {
  127. int nPos=0;
  128. int nIndex = 0;
  129. std::string strTemp = strFileData;
  130. while ( (nPos=strTemp.find('\r\n')) != std::string::npos )
  131. {
  132. if ( nIndex++ == nLine )
  133. {
  134. strTemp = strTemp.substr(0, nPos);
  135. break;
  136. }
  137. strTemp = strTemp.substr(nPos+2);
  138. }
  139. return nIndex < nLine ? std::string() : strTemp;
  140. }
  141. void GetStringList(std::string strData, std::string strSplit, std::vector<std::string> &vtString)
  142. {
  143. int nPos = -1;
  144. std::string strTemp;
  145. vtString.clear();
  146. do
  147. {
  148. nPos = strData.find(strSplit);
  149. if ( nPos != std::string::npos )
  150. {
  151. strTemp = strData.substr(0, nPos);
  152. vtString.push_back(strTemp);
  153. strData = strData.substr(nPos+strSplit.size());
  154. }
  155. } while (nPos != -1);
  156. }
  157. BOOL FindString(std::vector<std::string> &vtString, std::string strFind, std::string &strResult)
  158. {
  159. std::string strTemp;
  160. for (std::vector<std::string>::iterator it = vtString.begin(); it != vtString.end(); it++ )
  161. {
  162. if ( strTemp.find(strFind) != -1 )
  163. {
  164. strResult = strTemp;
  165. return TRUE;
  166. }
  167. }
  168. return FALSE;
  169. }
  170. void CheckOutData(std::vector<std::string> &vtData, std::vector<RGB_PAT> &vtResult)
  171. {
  172. if ( vtData.size() < 3 )
  173. return;
  174. int nIndex = 0;
  175. for (std::vector<std::string>::iterator it = vtData.begin(); it != vtData.end(); it++ )
  176. {
  177. std::vector<std::string> vtItem;
  178. GetStringList(it->c_str(), ",", vtItem);
  179. if ( vtItem.size() >= 20 && nIndex > 1)
  180. {
  181. // R,G,B,76,94,00
  182. // 1,2,3,13,14,15
  183. RGB_PAT rgb_pat;
  184. rgb_pat.R = vtItem.at(0);
  185. rgb_pat.G = vtItem.at(1);
  186. rgb_pat.B = vtItem.at(2);
  187. rgb_pat.deltaE76 = vtItem.at(12);
  188. rgb_pat.deltaE94 = vtItem.at(13);
  189. rgb_pat.deltaE00 = vtItem.at(14);
  190. vtResult.push_back(rgb_pat);
  191. }
  192. nIndex++;
  193. }
  194. }
  195. BOOL IsDebugPass(std::vector<RGB_PAT> &vtDebugRGB, INT nDeltaEType, DOUBLE dDeltaEValue, DOUBLE &dDeltaEAVGValue)
  196. {
  197. BOOL bRet = FALSE;
  198. DOUBLE dAllDeltaEValue = 0.0;
  199. INT nDeltaETypeIndex = 3 + nDeltaEType;
  200. for (std::vector<RGB_PAT>::iterator it = vtDebugRGB.begin(); it != vtDebugRGB.end(); it++)
  201. {
  202. if ( nDeltaEType == DeltaE76 )
  203. dAllDeltaEValue += atof(it->deltaE76.c_str());
  204. if ( nDeltaEType == DeltaE94 )
  205. dAllDeltaEValue += atof(it->deltaE94.c_str());
  206. if ( nDeltaEType == DeltaE00 )
  207. dAllDeltaEValue += atof(it->deltaE00.c_str());
  208. }
  209. dDeltaEAVGValue = dAllDeltaEValue/vtDebugRGB.size();
  210. if ( dDeltaEValue >= dDeltaEAVGValue )
  211. bRet = TRUE;
  212. return bRet;
  213. }
  214. DWORD FindProcess(LPCTSTR lpProName)
  215. {
  216. ASSERT(lpProName!=NULL);
  217. DWORD dwProcId = 0;
  218. PROCESSENTRY32 pe32 = { 0 };
  219. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  220. if (hSnapshot == NULL)
  221. {
  222. return 0;
  223. }
  224. pe32.dwSize = sizeof(PROCESSENTRY32);
  225. if (Process32First(hSnapshot, &pe32))
  226. {
  227. do {
  228. if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
  229. {
  230. dwProcId = pe32.th32ProcessID;
  231. break;
  232. }
  233. } while (Process32Next(hSnapshot, &pe32));
  234. }
  235. CloseHandle(hSnapshot);
  236. return dwProcId;
  237. }
  238. vector<DWORD> FindAllProcess(LPCTSTR lpProName)
  239. {
  240. ASSERT(lpProName!=NULL);
  241. vector<DWORD> vtPID;
  242. PROCESSENTRY32 pe32 = { 0 };
  243. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  244. if (hSnapshot == NULL)
  245. return vector<DWORD>();
  246. pe32.dwSize = sizeof(PROCESSENTRY32);
  247. if (Process32First(hSnapshot, &pe32))
  248. {
  249. do {
  250. if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
  251. {
  252. vtPID.push_back(pe32.th32ProcessID);
  253. }
  254. } while (Process32Next(hSnapshot, &pe32));
  255. }
  256. CloseHandle(hSnapshot);
  257. return vtPID;
  258. }
  259. void FindAllProcess(std::vector<PROC_INFO> &vtProInfo)
  260. {
  261. PROCESSENTRY32 pe32 = { 0 };
  262. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  263. if (hSnapshot == NULL)
  264. return;
  265. pe32.dwSize = sizeof(PROCESSENTRY32);
  266. if (!Process32First(hSnapshot, &pe32))
  267. goto end;
  268. do
  269. {
  270. PROC_INFO proinfo;
  271. proinfo.dwProcId = pe32.th32ProcessID;
  272. proinfo.strProcName = pe32.szExeFile;
  273. vtProInfo.push_back(proinfo);
  274. } while (Process32Next(hSnapshot, &pe32));
  275. end:
  276. CloseHandle(hSnapshot);
  277. }
  278. HANDLE FindModule(LPCTSTR lpModuleName, DWORD dwProcId)
  279. {
  280. ASSERT(lpModuleName!=NULL);
  281. DWORD dwMID = 0;
  282. MODULEENTRY32 me32 = { 0 };
  283. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, dwProcId);
  284. if (hSnapshot == NULL)
  285. return NULL;
  286. me32.dwSize = sizeof(MODULEENTRY32);
  287. if (Module32First(hSnapshot, &me32))
  288. {
  289. do {
  290. if (_tcsicmp(lpModuleName, me32.szModule) == 0)
  291. {
  292. break;
  293. }
  294. } while (Module32Next(hSnapshot, &me32));
  295. }
  296. CloseHandle(hSnapshot);
  297. return me32.hModule;
  298. }
  299. LPCTSTR GetModulePath(LPCTSTR lpModuleName, DWORD dwProcId)
  300. {
  301. ASSERT(lpModuleName!=NULL);
  302. DWORD dwMID = 0;
  303. MODULEENTRY32 me32 = { 0 };
  304. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, dwProcId);
  305. if (hSnapshot == NULL)
  306. return NULL;
  307. BOOL bFound = FALSE;
  308. me32.dwSize = sizeof(MODULEENTRY32);
  309. if (Module32First(hSnapshot, &me32)) {
  310. do
  311. {
  312. if (_tcsicmp(lpModuleName, me32.szModule) == 0) {
  313. bFound = TRUE;
  314. break;
  315. }
  316. } while (Module32Next(hSnapshot, &me32));
  317. }
  318. CloseHandle(hSnapshot);
  319. return bFound ? me32.szExePath : NULL;
  320. }
  321. HANDLE FindModuleEx(LPCTSTR lpModuleName, DWORD dwProcId)
  322. {
  323. HMODULE hMods[1024] = {0};
  324. DWORD cbNeeded = 0;
  325. TCHAR szModName[MAX_PATH];
  326. BOOL Wow64Process;
  327. HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwProcId);
  328. IsWow64Process(hProcess, &Wow64Process); //判断是32位还是64位进程
  329. if ( EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, Wow64Process?LIST_MODULES_32BIT:LIST_MODULES_64BIT) )
  330. {
  331. for (UINT i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
  332. {
  333. GetModuleFileNameEx(hProcess, hMods[i], szModName, _countof(szModName));
  334. #ifdef _DEBUG
  335. WriteTextLog(_T("目标=%s, 原始=%s, 地址=%p"), szModName, lpModuleName, hMods[i]);
  336. #endif
  337. if (_tcsicmp(lpModuleName, szModName) == 0)
  338. {
  339. CloseHandle(hProcess);
  340. WriteTextLog(_T("【目标=%s, 原始=%s, 地址=%p】"), szModName, lpModuleName, hMods[i]);
  341. return hMods[i];
  342. }
  343. }
  344. }
  345. CloseHandle(hProcess);
  346. return NULL;
  347. }
  348. // WINDOWS NT 以上的内核需要提权,才能对系统进行高级管理;
  349. BOOL GetDebugPriv()
  350. {
  351. // 返回的访问令牌指针;
  352. HANDLE hToken;
  353. // 接收所返回的制定特权名称的信息;
  354. LUID sedebugnameValue;
  355. // 新特权信息的指针(结构体);
  356. TOKEN_PRIVILEGES tkp;
  357. //DWORD dwCurProcId = GetCurrentProcessId();
  358. // 要修改访问权限的进程句柄;
  359. HANDLE hCurProc = ::GetCurrentProcess();
  360. //hCurProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurProcId);
  361. if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  362. {
  363. return FALSE;
  364. }
  365. if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
  366. {
  367. CloseHandle(hToken);
  368. return FALSE;
  369. }
  370. tkp.PrivilegeCount = 1;
  371. tkp.Privileges[0].Luid = sedebugnameValue;
  372. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  373. if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
  374. {
  375. CloseHandle(hToken);
  376. return FALSE;
  377. }
  378. CloseHandle(hCurProc);
  379. CloseHandle(hToken);
  380. return TRUE;
  381. }
  382. /************************************************************************/
  383. /*
  384. 函数:GetFileVersion
  385. 描述:获取可执行文件的文件版号;
  386. 参数:
  387. hModule[IN] 可执行文件模块句柄;
  388. dwArray[OUT] 返回的文件版本号;
  389. 返回:
  390. 成功返回TRUE,失败返回FALSE;
  391. 注意:
  392. 当hModule为空时,表示要获取的可执行文件为本程序的文件版本号;
  393. */
  394. /************************************************************************/
  395. BOOL GetFileVersion( IN HMODULE hModule, OUT DWORD (&dwArray)[4])
  396. {
  397. TCHAR fname[MAX_PATH];
  398. VS_FIXEDFILEINFO *pVi;
  399. DWORD dwHandle;
  400. if ( GetModuleFileName(hModule, fname, MAX_PATH))
  401. {
  402. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  403. if (nSize > 0)
  404. {
  405. BYTE *pBuffer = new BYTE[nSize];
  406. memset(pBuffer, 0, nSize);
  407. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  408. {
  409. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  410. {
  411. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  412. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  413. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  414. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  415. delete[]pBuffer;
  416. return TRUE;
  417. }
  418. }
  419. if ( pBuffer )
  420. delete[]pBuffer;
  421. }
  422. }
  423. return FALSE;
  424. }
  425. /************************************************************************/
  426. /*
  427. 函数:GetFileVersion
  428. 描述:获取可执行文件的文件版号;
  429. 参数:
  430. lpFileName[IN] 可执行文件名全路径;
  431. dwArray[OUT] 返回的文件版本号;
  432. 返回:
  433. 成功返回TRUE,失败返回FALSE;
  434. 注意:
  435. */
  436. /************************************************************************/
  437. BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  438. {
  439. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  440. {
  441. OutputDebugString(_T("文件名错误或文件不存在\n"));
  442. return FALSE;
  443. }
  444. DWORD dwHandle = 0;
  445. VS_FIXEDFILEINFO *pVi = NULL;
  446. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  447. if ( nSize > 0 )
  448. {
  449. BYTE *pBuffer = new BYTE[nSize];
  450. memset(pBuffer, 0, nSize);
  451. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  452. {
  453. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  454. {
  455. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  456. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  457. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  458. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  459. if (pBuffer)
  460. delete[]pBuffer;
  461. return TRUE;
  462. }
  463. }
  464. if (pBuffer)
  465. delete[]pBuffer;
  466. }
  467. return FALSE;
  468. }
  469. /************************************************************************/
  470. /*
  471. 函数:GetProductVersion
  472. 描述:获取可执行文件的产品版号;
  473. 参数:
  474. hModule[IN] 可执行文件模块句柄;
  475. dwArray[OUT] 返回的产品版本号;
  476. 返回:
  477. 成功返回TRUE,失败返回FALSE;
  478. 注意:
  479. 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号;
  480. */
  481. /************************************************************************/
  482. BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
  483. {
  484. TCHAR fname[MAX_PATH];
  485. VS_FIXEDFILEINFO *pVi;
  486. DWORD dwHandle;
  487. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  488. {
  489. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  490. if (nSize > 0)
  491. {
  492. BYTE *pBuffer = new BYTE[nSize];
  493. memset(pBuffer, 0, nSize);
  494. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  495. {
  496. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  497. {
  498. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  499. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  500. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  501. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  502. if(pBuffer)
  503. delete[]pBuffer;
  504. return TRUE;
  505. }
  506. }
  507. if(pBuffer)
  508. delete[]pBuffer;
  509. }
  510. }
  511. return FALSE;
  512. }
  513. /************************************************************************/
  514. /*
  515. 函数:GetProductVersion
  516. 描述:获取可执行文件的产品版号;
  517. 参数:
  518. lpFileName[IN] 可执行文件名全路径;
  519. dwArray[OUT] 返回的产品版本号;
  520. 返回:
  521. 成功返回TRUE,失败返回FALSE;
  522. 注意:
  523. */
  524. /************************************************************************/
  525. BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  526. {
  527. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  528. {
  529. OutputDebugString(_T("文件名错误或文件不存在\n"));
  530. return FALSE;
  531. }
  532. DWORD dwHandle = 0;
  533. VS_FIXEDFILEINFO *pVi = NULL;
  534. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  535. if ( nSize > 0 )
  536. {
  537. BYTE *pBuffer = new BYTE[nSize];
  538. memset(pBuffer, 0, nSize);
  539. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  540. {
  541. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  542. {
  543. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  544. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  545. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  546. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  547. if (pBuffer)
  548. delete[]pBuffer;
  549. return TRUE;
  550. }
  551. }
  552. if (pBuffer)
  553. delete[]pBuffer;
  554. }
  555. return FALSE;
  556. }
  557. /************************************************************************/
  558. /* 函数:WriteTextLog[7/28/2016 IT];
  559. /* 描述:写文本日志;
  560. /* 参数:;
  561. /* [IN] :;
  562. /* 返回:void;
  563. /* 注意:;
  564. /* 示例:;
  565. /*
  566. /* 修改:;
  567. /* 日期:;
  568. /* 内容:;
  569. /************************************************************************/
  570. void WriteTextLog(const TCHAR *format, ...)
  571. {
  572. #if 0
  573. try
  574. {
  575. //static ThreadSection _critSection;
  576. //AutoThreadSection aSection(&_critSection);
  577. // 解析出日志路径;
  578. TCHAR szlogpath[MAX_PATH] = {0};
  579. static TCHAR szModulePath[MAX_PATH] = {0};
  580. static TCHAR szFna[_MAX_DIR] = { 0 };
  581. if ( szModulePath[0] == _T('\0') )
  582. {
  583. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  584. TCHAR szDir[_MAX_DIR] = { 0 };
  585. TCHAR szExt[_MAX_DIR] = { 0 };
  586. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  587. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  588. _tcscpy_s(szModulePath, szDrive);
  589. _tcscat_s(szModulePath, szDir);
  590. }
  591. _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]"));
  592. // 打开或创建文件;
  593. CStdioFile fp;
  594. if (PathFileExists(szlogpath))
  595. {
  596. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  597. {
  598. return;
  599. }
  600. fp.SeekToEnd();
  601. }
  602. else
  603. {
  604. fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite);
  605. }
  606. // 格式化前设置语言区域;
  607. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  608. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  609. // 格式化日志内容;
  610. va_list args = NULL;
  611. int len = 0;
  612. TCHAR *buffer = NULL;
  613. va_start( args, format );
  614. // _vscprintf doesn't count. terminating '\0'
  615. len = _vsctprintf( format, args ) + 1;
  616. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  617. _vstprintf_s( buffer, len, format, args ); // C4996
  618. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  619. // 将日志内容输入到文件中;
  620. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  621. fp.WriteString(buffer);
  622. fp.WriteString(_T("\n"));
  623. // 关闭文件,释放资源并设置回原语言区域;
  624. free( buffer );
  625. _tsetlocale(LC_CTYPE, old_locale);
  626. free(old_locale);//还原区域设定;
  627. fp.Close();
  628. }
  629. catch (CException *e)
  630. {
  631. e->ReportError();
  632. e->Delete();
  633. }
  634. #else
  635. // 解析出日志路径;
  636. TCHAR szlogpath[MAX_PATH] = { 0 };
  637. static TCHAR szModulePath[MAX_PATH] = { 0 };
  638. static TCHAR szFna[MAX_PATH] = { 0 };
  639. if (szModulePath[0] == _T('\0'))
  640. {
  641. TCHAR szDrive[MAX_PATH] = { 0 };
  642. TCHAR szDir[MAX_PATH] = { 0 };
  643. TCHAR szExt[MAX_PATH] = { 0 };
  644. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  645. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  646. _tcscpy_s(szModulePath, szDrive);
  647. _tcscat_s(szModulePath, szDir);
  648. }
  649. _stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
  650. // 打开或创建文件;
  651. FILE *fp = NULL;
  652. //if (_taccess(szlogpath, 0) != -1)
  653. #ifndef UNICODE
  654. if (_access(szlogpath, 0) != -1)
  655. #else
  656. if (_taccess(szlogpath, 0) != -1)
  657. #endif
  658. {// 存在;
  659. fp = _tfopen(szlogpath, _T("a+"));
  660. // 移动到末尾;
  661. fseek(fp, 0, SEEK_END);
  662. }
  663. else
  664. {// 不存在;
  665. fp = _tfopen(szlogpath, _T("w+"));
  666. }
  667. if (fp == NULL)
  668. return;
  669. // 格式化前设置语言区域;
  670. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  671. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  672. // 格式化日志内容;
  673. va_list args = NULL;
  674. int len = 0;
  675. TCHAR *buffer = NULL;
  676. va_start(args, format);
  677. // _vscprintf doesn't count. terminating '\0'
  678. len = _vsctprintf(format, args) + 1;
  679. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  680. _vstprintf_s(buffer, len, format, args);
  681. // 将日志内容输入到文件中;
  682. // 获取今年年份;
  683. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  684. struct tm gmtm = { 0 };
  685. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  686. _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);
  687. // 关闭文件,释放资源并设置回原语言区域;
  688. free(buffer);
  689. fclose(fp);
  690. _tsetlocale(LC_CTYPE, old_locale);
  691. free(old_locale);//还原区域设定;
  692. #endif
  693. }
  694. //---------------------------------------------------------------------
  695. // add by Jeff 2014.10.27
  696. // 函数:全局函数IsDirectoryLegitimate,多字节版本,非UNICODE
  697. // 描述:判断一个目录路径字符串,是否属于合法的、可创建的目录路径。
  698. // 参数:strDirectory 被验证的路径字符串;
  699. //
  700. // 返回:合法路径返回TRUE;
  701. //---------------------------------------------------------------------
  702. BOOL IsDirectoryLegitimate(const CString &strDirectory)
  703. {
  704. if (strDirectory.Find('/') != -1 ||
  705. strDirectory.Find('\\') != -1 ||
  706. strDirectory.Find(':') != -1 ||
  707. strDirectory.Find('*') != -1 ||
  708. strDirectory.Find('?') != -1 ||
  709. strDirectory.Find('\"') != -1 ||
  710. strDirectory.Find('>') != -1 ||
  711. strDirectory.Find('<') != -1 ||
  712. strDirectory.Find('|') != -1
  713. )
  714. return FALSE;
  715. return TRUE;
  716. }
  717. //--------------------------------------------------------------------------------
  718. // Jeff add 2014.06.23;
  719. // 函数:ErrorExit
  720. // 描述:
  721. // 参数:
  722. // lpszFunction:函数名;
  723. // dwError:错误码;
  724. //
  725. //--------------------------------------------------------------------------------
  726. void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError)
  727. {
  728. #if 1
  729. LPVOID lpMsgBuf;
  730. BOOL fOk = FormatMessage(
  731. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  732. FORMAT_MESSAGE_FROM_SYSTEM |
  733. FORMAT_MESSAGE_IGNORE_INSERTS,
  734. NULL,
  735. dwError,
  736. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  737. (LPTSTR)&lpMsgBuf,
  738. 0, NULL);
  739. if (!fOk)
  740. {
  741. // Is it a network-related error?
  742. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  743. if (hDll != NULL)
  744. {
  745. FormatMessage(
  746. FORMAT_MESSAGE_FROM_HMODULE |
  747. FORMAT_MESSAGE_FROM_SYSTEM |
  748. FORMAT_MESSAGE_IGNORE_INSERTS,
  749. hDll,
  750. dwError,
  751. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  752. (LPTSTR)&lpMsgBuf,
  753. 0,
  754. NULL);
  755. FreeLibrary(hDll);
  756. }
  757. }
  758. if (lpMsgBuf != NULL)
  759. {
  760. CString strDisplay;
  761. strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
  762. //WriteLog(strDisplay);
  763. LocalFree(lpMsgBuf);
  764. }
  765. else
  766. {
  767. //WriteLog(strDescription);
  768. }
  769. #else
  770. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  771. // Get the error code's textual description
  772. BOOL fOk = FormatMessage(
  773. FORMAT_MESSAGE_FROM_SYSTEM |
  774. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  775. FORMAT_MESSAGE_IGNORE_INSERTS,
  776. NULL,
  777. dwError,
  778. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  779. (PTSTR)&hlocal,
  780. 0,
  781. NULL);
  782. if (!fOk)
  783. {
  784. // Is it a network-related error?
  785. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  786. if (hDll != NULL)
  787. {
  788. FormatMessage(
  789. FORMAT_MESSAGE_FROM_HMODULE |
  790. FORMAT_MESSAGE_FROM_SYSTEM |
  791. FORMAT_MESSAGE_IGNORE_INSERTS,
  792. hDll,
  793. dwError,
  794. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  795. (PTSTR)&hlocal,
  796. 0,
  797. NULL);
  798. FreeLibrary(hDll);
  799. }
  800. }
  801. if (hlocal != NULL)
  802. {
  803. CString strDisplay;
  804. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  805. //WriteLog(strDisplay);
  806. LocalFree(hlocal);
  807. }
  808. else
  809. {
  810. //WriteLog("Error number not found.");
  811. }
  812. #endif
  813. }
  814. // The system displays a dialog box with a custom message and a message to the user to close applications within the specified time-out interval (30 seconds).
  815. // After the time-out interval elapses, the system is restarted.
  816. //The application must enable the SE_SHUTDOWN_NAME privilege before calling InitiateSystemShutdown
  817. BOOL MySystemShutdown(LPTSTR lpMsg)
  818. {
  819. HANDLE hToken; // handle to process token
  820. TOKEN_PRIVILEGES tkp; // pointer to token structure
  821. BOOL fResult; // system shutdown flag
  822. // Get the current process token handle so we can get shutdown
  823. // privilege.
  824. if (!OpenProcessToken(GetCurrentProcess(),
  825. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  826. return FALSE;
  827. // Get the LUID for shutdown privilege.
  828. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  829. &tkp.Privileges[0].Luid);
  830. tkp.PrivilegeCount = 1; // one privilege to set
  831. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  832. // Get shutdown privilege for this process.
  833. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  834. (PTOKEN_PRIVILEGES)NULL, 0);
  835. // Cannot test the return value of AdjustTokenPrivileges.
  836. if (GetLastError() != ERROR_SUCCESS)
  837. return FALSE;
  838. // Display the shutdown dialog box and start the countdown.
  839. fResult = InitiateSystemShutdown(
  840. NULL, // shut down local computer
  841. lpMsg, // message for user
  842. 30, // time-out period, in seconds
  843. FALSE, // ask user to close apps
  844. TRUE); // reboot after shutdown
  845. if (!fResult)
  846. return FALSE;
  847. // Disable shutdown privilege.
  848. tkp.Privileges[0].Attributes = 0;
  849. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  850. (PTOKEN_PRIVILEGES)NULL, 0);
  851. return TRUE;
  852. }
  853. // If the AbortSystemShutdown function is executed in the time-out period specified by InitiateSystemShutdown,
  854. // the system does not shut down. For example, if PreventSystemShutdown is called after MySystemShutdown,
  855. // the system closes the dialog box and does not restart the system.
  856. BOOL PreventSystemShutdown()
  857. {
  858. HANDLE hToken; // handle to process token
  859. TOKEN_PRIVILEGES tkp; // pointer to token structure
  860. // Get the current process token handle so we can get shutdown
  861. // privilege.
  862. if (!OpenProcessToken(GetCurrentProcess(),
  863. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  864. return FALSE;
  865. // Get the LUID for shutdown privilege.
  866. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  867. &tkp.Privileges[0].Luid);
  868. tkp.PrivilegeCount = 1; // one privilege to set
  869. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  870. // Get shutdown privilege for this process.
  871. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  872. (PTOKEN_PRIVILEGES)NULL, 0);
  873. if (GetLastError() != ERROR_SUCCESS)
  874. return FALSE;
  875. // Prevent the system from shutting down.
  876. if (!AbortSystemShutdown(NULL))
  877. return FALSE;
  878. // Disable shutdown privilege.
  879. tkp.Privileges[0].Attributes = 0;
  880. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  881. (PTOKEN_PRIVILEGES)NULL, 0);
  882. return TRUE;
  883. }
  884. // Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer
  885. // The application must first enable the SE_SHUTDOWN_NAME privilege.
  886. // The final parameter in the call to ExitWindowsEx indicates that the system was shut down for a planning update of the operating system.
  887. BOOL MySystemShutdown()
  888. {
  889. HANDLE hToken;
  890. TOKEN_PRIVILEGES tkp;
  891. // Get a token for this process.
  892. if (!OpenProcessToken(GetCurrentProcess(),
  893. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  894. return(FALSE);
  895. // Get the LUID for the shutdown privilege.
  896. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  897. &tkp.Privileges[0].Luid);
  898. tkp.PrivilegeCount = 1; // one privilege to set
  899. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  900. // Get the shutdown privilege for this process.
  901. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  902. (PTOKEN_PRIVILEGES)NULL, 0);
  903. if (GetLastError() != ERROR_SUCCESS)
  904. return FALSE;
  905. // Shut down the system and force all applications to close.
  906. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  907. SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
  908. SHTDN_REASON_MINOR_UPGRADE |
  909. SHTDN_REASON_FLAG_PLANNED))
  910. return FALSE;
  911. return TRUE;
  912. }
  913. HWND GetProcessMainWnd(const DWORD& dwTagetProcessId, LPCTSTR lpTagetWndName)
  914. {
  915. DWORD dwCurPorcessId = 0;
  916. HWND hTagetProcessWnd = NULL;
  917. TCHAR szWndName[MAX_PATH] = { 0 };
  918. TCHAR szClassName[MAX_PATH] = { 0 };
  919. // 取得第一个窗口句柄;
  920. for (HWND hCurWnd = ::GetTopWindow(NULL); hCurWnd != NULL; hCurWnd = ::GetNextWindow(hCurWnd, GW_HWNDNEXT)) {
  921. // 重置为0;
  922. dwCurPorcessId = 0;
  923. // 通过窗口句柄反查进程pid;
  924. DWORD dwThreadId = ::GetWindowThreadProcessId(hCurWnd, &dwCurPorcessId);
  925. if (dwThreadId != 0) {
  926. // 判断当前进程id是否和目标进程id相同;
  927. if (dwCurPorcessId == dwTagetProcessId) {
  928. if (lpTagetWndName == NULL) {
  929. hTagetProcessWnd = hCurWnd;
  930. break;
  931. }
  932. else {
  933. // 获取窗口名称;
  934. ::GetWindowText(hCurWnd, szWndName, sizeof(szWndName) / sizeof(TCHAR));
  935. // 获取窗口类名;
  936. ::GetClassName(hCurWnd, szClassName, sizeof(szClassName) / sizeof(TCHAR));
  937. #ifdef _DEBUG
  938. TCHAR szLogMsg[MAX_PATH] = { 0 };
  939. _stprintf_s(szLogMsg, _T("类名:%s, 窗口名:%s,窗口地址:%p \n"), szClassName, szWndName, hCurWnd);
  940. OutputDebugString(szLogMsg);
  941. #endif
  942. if (_tcsstr(szWndName, lpTagetWndName) != NULL) {
  943. hTagetProcessWnd = hCurWnd;
  944. break;
  945. }
  946. }
  947. }
  948. }
  949. }
  950. // 当前窗口有可能不是进程父窗口;
  951. HWND hParentWnd = hTagetProcessWnd;
  952. while (hParentWnd) {
  953. hParentWnd = ::GetParent(hTagetProcessWnd);
  954. if (hParentWnd == NULL)
  955. break;
  956. hTagetProcessWnd = hParentWnd;
  957. }
  958. return hTagetProcessWnd;
  959. }
  960. BOOL CALLBACK EnumChildWindowCallBack(HWND hWnd, LPARAM lParam)
  961. {
  962. DWORD dwPid = 0;
  963. LPPROC_WND_INFO lpWndData = (LPPROC_WND_INFO)lParam;
  964. GetWindowThreadProcessId(hWnd, &dwPid); // 获得找到窗口所属的进程
  965. if(dwPid == lpWndData->dwProcId) // 判断是否是目标进程的窗口
  966. {
  967. WND_INFO wndInfo;
  968. wndInfo.hWnd=hWnd;
  969. SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)wndInfo.szWndTitle);
  970. GetClassName(hWnd, wndInfo.szClassName, MAX_PATH);
  971. wndInfo.dwCtrlId = ::GetDlgCtrlID(hWnd);
  972. lpWndData->AddWnd(wndInfo);
  973. // 输出窗口信息
  974. TRACE4("A-0x%08X, %ld, %s, %s\n", hWnd, wndInfo.dwCtrlId, wndInfo.szClassName, wndInfo.szWndTitle);
  975. // 此处如果再递归,会导致重复查找2次子窗口;
  976. //EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam); // 递归查找子窗口
  977. return TRUE;
  978. }
  979. return FALSE;
  980. }
  981. BOOL CALLBACK EnumWindowCallBack(HWND hWnd, LPARAM lParam)
  982. {
  983. DWORD dwPid = 0;
  984. LPPROC_WND_INFO lpWndData = (LPPROC_WND_INFO)lParam;
  985. GetWindowThreadProcessId(hWnd, &dwPid); // 获得找到窗口所属的进程
  986. if(dwPid == lpWndData->dwProcId) // 判断是否是目标进程的窗口
  987. {
  988. WND_INFO wndInfo;
  989. wndInfo.hWnd=hWnd;
  990. SendMessage(hWnd, WM_GETTEXT, MAX_PATH, (LPARAM)wndInfo.szWndTitle);
  991. GetClassName(hWnd, wndInfo.szClassName, MAX_PATH);
  992. wndInfo.dwCtrlId = ::GetDlgCtrlID(hWnd);
  993. lpWndData->AddWnd(wndInfo);
  994. // 输出窗口信息
  995. TRACE4("A-0x%08X, %ld, %s, %s\n", hWnd, wndInfo.dwCtrlId, wndInfo.szClassName, wndInfo.szWndTitle);
  996. EnumChildWindows(hWnd, EnumChildWindowCallBack, lParam); // 继续查找子窗口
  997. }
  998. return TRUE;
  999. }
  1000. BOOL EnumProcessAllWnd(LPPROC_WND_INFO lpProcWndInfo)
  1001. {
  1002. lpProcWndInfo->vtWndInfo.clear();
  1003. return EnumWindows(EnumWindowCallBack, (LPARAM)lpProcWndInfo);
  1004. }
  1005. BOOL StartProcess(LPCTSTR lpPath, BOOL bShowWnd, BOOL bSuspend)
  1006. {
  1007. STARTUPINFO si;
  1008. PROCESS_INFORMATION pi;
  1009. ZeroMemory(&si, sizeof(si));
  1010. si.cb = sizeof(si);
  1011. ZeroMemory(&pi, sizeof(pi));
  1012. CString strDir = lpPath;
  1013. strDir = strDir.Left(strDir.ReverseFind(_T('\\')));
  1014. si.dwFlags = STARTF_USESHOWWINDOW; // 指定wShowWindow成员有效
  1015. si.wShowWindow = bShowWnd ? SW_SHOW : SW_HIDE; // 设置创建进程时,窗口不显示,
  1016. // 为FALSE的话则不显示
  1017. BOOL bRet = ::CreateProcess (
  1018. lpPath, // 不在此指定可执行文件的文件名
  1019. NULL, // 命令行参数
  1020. NULL, // 默认进程安全性
  1021. NULL, // 默认线程安全性
  1022. FALSE, // 指定当前进程内的句柄不可以被子进程继承
  1023. bSuspend ? CREATE_SUSPENDED : NULL,
  1024. NULL, // 使用本进程的环境变量
  1025. strDir, // 使用本进程的驱动器和目录
  1026. &si,
  1027. &pi);
  1028. if(bRet)
  1029. {
  1030. // 进程挂起后,仍能成功注入dll;
  1031. // TCHAR szDllPath[MAX_PATH];
  1032. // ZeroMemory(szDllPath,MAX_PATH);
  1033. // _stprintf_s(szDllPath, _T("%shook.dll"), g_szModulePath);
  1034. // for (int i = 0; i < 10; i++)
  1035. // {
  1036. // CInjection inject(pi.dwProcessId,szDllPath);
  1037. // inject.InjectDynamicLibrary();
  1038. // inject.EjectDynamicLibrary();
  1039. // }
  1040. g_procWndInfo.dwProcId = pi.dwProcessId;
  1041. // 既然我们不使用两个句柄,最好是立刻将它们关闭
  1042. ::CloseHandle (pi.hThread);
  1043. ::CloseHandle (pi.hProcess);
  1044. }
  1045. return bRet;
  1046. }
  1047. /************************************************************************/
  1048. /* 函数:IsDigitString[3/28/2016 IT];
  1049. /* 描述:;
  1050. /* 参数:;
  1051. /* [IN] :;
  1052. /* 返回:返回2表示浮点型, 返回1表示整型, 返回-1表示不是数字;
  1053. /* 注意:;
  1054. /* 示例:;
  1055. /*
  1056. /* 修改:;
  1057. /* 日期:;
  1058. /* 内容:;
  1059. /************************************************************************/
  1060. int IsDigitString(IN CONST CString& str)
  1061. {
  1062. int npt = 0;
  1063. for (int i = 0; i < str.GetLength(); i++)
  1064. {
  1065. if (!_istdigit(str.GetAt(i)))
  1066. {
  1067. if ( str.GetAt(i) == _T('.') && i != 0)
  1068. {
  1069. if ( npt > 1)
  1070. {
  1071. return -1;
  1072. }
  1073. npt++;
  1074. }
  1075. else
  1076. {
  1077. return -1;
  1078. }
  1079. }
  1080. }
  1081. return npt ? 2 : 1;
  1082. }
  1083. int IsDigitString(LPCTSTR lpString)
  1084. {
  1085. if ( !lpString || lpString[0] == _T('\0') )
  1086. return -1;
  1087. int npt = 0;
  1088. int nLen = _tcslen(lpString);
  1089. for (int i = 0; i < nLen; i++)
  1090. {
  1091. if (!_istdigit(lpString[i]))
  1092. {
  1093. if ( lpString[i] == _T('.') && i != 0)
  1094. {
  1095. if ( npt > 1)
  1096. {
  1097. return -1;
  1098. }
  1099. npt++;
  1100. }
  1101. else
  1102. {
  1103. return -1;
  1104. }
  1105. }
  1106. }
  1107. return npt ? 2 : 1;
  1108. }
  1109. int IsDigitStringA(LPCSTR lpString)
  1110. {
  1111. if ( !lpString || lpString[0] == '\0' )
  1112. return -1;
  1113. int npt = 0;
  1114. int nLen = strlen(lpString);
  1115. for (int i = 0; i < nLen; i++)
  1116. {
  1117. if (!isdigit(lpString[i]))
  1118. {
  1119. if ( lpString[i] == '.' && i != 0)
  1120. {
  1121. if ( npt > 1)
  1122. {
  1123. return -1;
  1124. }
  1125. npt++;
  1126. }
  1127. else
  1128. {
  1129. return -1;
  1130. }
  1131. }
  1132. }
  1133. return npt ? 2 : 1;
  1134. }
  1135. bool InitSocket()
  1136. {
  1137. static bool bInit = false;
  1138. if ( !bInit )
  1139. {
  1140. WSADATA wsaData;
  1141. int nResult;
  1142. nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  1143. // 错误(一般都不可能出现)
  1144. if (NO_ERROR != nResult)
  1145. {
  1146. return false;
  1147. }
  1148. bInit = true;
  1149. }
  1150. return bInit;
  1151. }
  1152. TString GetHostName()
  1153. {
  1154. if ( InitSocket() )
  1155. {
  1156. char szHostName[MAX_PATH] = {0};
  1157. if ( 0 == gethostname(szHostName, MAX_PATH) )
  1158. {
  1159. #ifdef UNICODE
  1160. wchar_t szName[MAX_PATH] = {0};
  1161. if ( -1 != mbstowcs(szName, szHostName, strlen(szHostName)) )
  1162. {
  1163. return TString(szName);
  1164. }
  1165. #else
  1166. return TString(szHostName);
  1167. #endif
  1168. }
  1169. }
  1170. return TString();
  1171. }
  1172. TString GetLocalAddress()
  1173. {
  1174. int nCardNo = 1;
  1175. TString strIPAddress;
  1176. // PIP_ADAPTER_INFO结构体指针存储本机网卡信息
  1177. PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
  1178. // 得到结构体大小,用于GetAdaptersInfo参数
  1179. unsigned long stSize = sizeof(IP_ADAPTER_INFO);
  1180. // 调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
  1181. int nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
  1182. // 记录网卡数量
  1183. int netCardNum = 0;
  1184. // 记录每张网卡上的IP地址数量
  1185. int IPnumPerNetCard = 0;
  1186. if(ERROR_BUFFER_OVERFLOW == nRel)
  1187. {
  1188. // 如果函数返回的是ERROR_BUFFER_OVERFLOW
  1189. // 则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
  1190. // 这也是说明为什么stSize既是一个输入量也是一个输出量
  1191. // 释放原来的内存空间
  1192. delete pIpAdapterInfo;
  1193. // 重新申请内存空间用来存储所有网卡信息
  1194. pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
  1195. // 再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
  1196. nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
  1197. }
  1198. PIP_ADAPTER_INFO pIpAdp = pIpAdapterInfo;
  1199. if(ERROR_SUCCESS == nRel)
  1200. {
  1201. // 输出网卡信息
  1202. // 可能有多网卡,因此通过循环去判断
  1203. while (pIpAdapterInfo)
  1204. {
  1205. // 可能网卡有多IP,因此通过循环去判断
  1206. IP_ADDR_STRING*pIpAddrString = &(pIpAdapterInfo->IpAddressList);
  1207. if ( pIpAdapterInfo->Type == MIB_IF_TYPE_ETHERNET || pIpAdapterInfo->Type == IF_TYPE_IEEE80211 )
  1208. {
  1209. if(std::string("0.0.0.0") == pIpAddrString->IpAddress.String)
  1210. {
  1211. pIpAdapterInfo = pIpAdapterInfo->Next;
  1212. continue;
  1213. }
  1214. #ifdef UNICODE
  1215. wchar_t szName[MAX_PATH] = {0};
  1216. if ( -1 != mbstowcs(szName, pIpAddrString->IpAddress.String, strlen(pIpAddrString->IpAddress.String)) )
  1217. {
  1218. strIPAddress = szName;
  1219. }
  1220. #else
  1221. strIPAddress = pIpAddrString->IpAddress.String;
  1222. #endif
  1223. break;
  1224. }
  1225. pIpAdapterInfo = pIpAdapterInfo->Next;
  1226. }
  1227. }
  1228. else{
  1229. strIPAddress = _T("127.0.0.1");
  1230. }
  1231. // 释放内存空间
  1232. if (pIpAdp)
  1233. {
  1234. delete pIpAdp;
  1235. }
  1236. return strIPAddress;
  1237. }
  1238. };