Global.cpp 33 KB

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