Global.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. TCHAR g_szModulePath[MAX_PATH] = _T(""); // 软件目录;
  11. TCHAR g_szModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  12. TCHAR g_szIniFile[MAX_PATH] = _T("");
  13. // 配置文件信息;
  14. TCHAR g_szServAddress[MAX_PATH] = _T("");
  15. DWORD g_dwServPort = 0;
  16. TCHAR g_szAccount[MAX_PATH] = _T("");
  17. TCHAR g_szPassword[MAX_PATH] = _T("");
  18. TCHAR g_szWeChatPath[MAX_PATH] = _T("");
  19. TCHAR g_szCacheDir[MAX_PATH] = _T("");
  20. TCHAR g_szDynamicLibraryPath[MAX_PATH] = _T("");
  21. // 控制台输出;
  22. BOOL g_bStdOut = FALSE;
  23. /************************************************************************/
  24. /* 函数:[1/6/2019 Home];
  25. /* 描述:;
  26. /* 参数:;
  27. /* [IN] :;
  28. /* [OUT] :;
  29. /* [IN/OUT] :;
  30. /* 返回:void;
  31. /* 注意:;
  32. /* 示例:;
  33. /*
  34. /* 修改:;
  35. /* 日期:;
  36. /* 内容:;
  37. /************************************************************************/
  38. int GetIniInfo(LPCTSTR lpIniDir /* = NULL */, LPCTSTR lpIniName /* = NULL */)
  39. {
  40. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  41. TCHAR szDir[_MAX_DIR] = { 0 };
  42. TCHAR szFna[_MAX_DIR] = { 0 };
  43. TCHAR szExt[_MAX_DIR] = { 0 };
  44. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  45. _stprintf_s(g_szModuleFileName, _T("%s"), g_szModulePath);
  46. _tsplitpath_s(g_szModulePath, szDrive, szDir, szFna, szExt);
  47. _tcscpy_s(g_szModulePath, szDrive);
  48. _tcscat_s(g_szModulePath, szDir);
  49. // 动态库路径;
  50. _stprintf_s(g_szDynamicLibraryPath, _T("%shook.dll"), g_szModulePath);
  51. if (lpIniDir != NULL && lpIniName != NULL)
  52. _stprintf_s(g_szIniFile, _T("%s%s"), lpIniDir, lpIniName);
  53. else
  54. _stprintf_s(g_szIniFile, _T("%sconfig.ini"), g_szModulePath);
  55. HANDLE hFile = CreateFile(g_szIniFile, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  56. if (ERROR_FILE_NOT_FOUND == GetLastError())
  57. {
  58. return -1;
  59. }
  60. CloseHandle(hFile);
  61. hFile = NULL;
  62. // 获取服务器端信息;
  63. GetPrivateProfileString(_T("ServerInfo"), _T("IP"), _T(""), g_szServAddress, MAX_PATH, g_szIniFile);
  64. g_dwServPort = GetPrivateProfileInt(_T("ServerInfo"), _T("Port"), 0, g_szIniFile);
  65. GetPrivateProfileString(_T("CustomerInfo"), _T("Account"), _T(""), g_szAccount, MAX_PATH, g_szIniFile);
  66. GetPrivateProfileString(_T("CustomerInfo"), _T("Password"), _T(""), g_szPassword, MAX_PATH, g_szIniFile);
  67. GetPrivateProfileString(_T("CustomerInfo"), _T("WeChat"), _T(""), g_szWeChatPath, MAX_PATH, g_szIniFile);
  68. GetPrivateProfileString(_T("CustomerInfo"), _T("Cache"), _T(""), g_szCacheDir, MAX_PATH, g_szIniFile);
  69. g_bStdOut = GetPrivateProfileInt(_T("CustomerInfo"), _T("StdOut"), 0, g_szIniFile);
  70. if ( g_bStdOut )
  71. {
  72. AllocConsole(); // 开辟控制台;
  73. SetConsoleTitle(_T("调试输出")); // 设置控制台窗口标题;
  74. freopen("CONOUT$", "w+t", stdout); // 重定向输出;
  75. freopen("CONIN$", "r+t", stdin); // 重定向输入;
  76. HWND hWnd = NULL;
  77. again:
  78. hWnd = ::FindWindow(NULL, _T("调试输出"));
  79. if( hWnd )
  80. {
  81. if (!::SetWindowPos(hWnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE))
  82. {
  83. _tprintf_s(_T("前置设置失败\n"));
  84. }
  85. else
  86. {
  87. _tprintf_s(_T("前置设置成功\n"));
  88. }
  89. }
  90. else
  91. {
  92. goto again;
  93. }
  94. }
  95. return 0;
  96. }
  97. /************************************************************************/
  98. /* 函数:[1/6/2019 Home];
  99. /* 描述:;
  100. /* 参数:;
  101. /* [IN] :;
  102. /* [OUT] :;
  103. /* [IN/OUT] :;
  104. /* 返回:void;
  105. /* 注意:;
  106. /* 示例:;
  107. /*
  108. /* 修改:;
  109. /* 日期:;
  110. /* 内容:;
  111. /************************************************************************/
  112. DWORD FindProcess(LPCTSTR lpProName)
  113. {
  114. ASSERT(lpProName!=NULL);
  115. DWORD dwPID = 0;
  116. PROCESSENTRY32 pe32 = { 0 };
  117. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  118. if (hSnapshot == NULL)
  119. {
  120. return 0;
  121. }
  122. pe32.dwSize = sizeof(PROCESSENTRY32);
  123. if (Process32First(hSnapshot, &pe32))
  124. {
  125. do {
  126. if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
  127. {
  128. dwPID = pe32.th32ProcessID;
  129. break;
  130. }
  131. } while (Process32Next(hSnapshot, &pe32));
  132. }
  133. CloseHandle(hSnapshot);
  134. return dwPID;
  135. }
  136. vector<DWORD> FindAllProcess(LPCTSTR lpProName)
  137. {
  138. ASSERT(lpProName!=NULL);
  139. vector<DWORD> vtPID;
  140. PROCESSENTRY32 pe32 = { 0 };
  141. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  142. if (hSnapshot == NULL)
  143. return vector<DWORD>();
  144. pe32.dwSize = sizeof(PROCESSENTRY32);
  145. if (Process32First(hSnapshot, &pe32))
  146. {
  147. do {
  148. if (_tcsicmp(lpProName, pe32.szExeFile) == 0)
  149. {
  150. vtPID.push_back(pe32.th32ProcessID);
  151. }
  152. } while (Process32Next(hSnapshot, &pe32));
  153. }
  154. CloseHandle(hSnapshot);
  155. return vtPID;
  156. }
  157. HANDLE FindModule(LPCTSTR lpModuleName, DWORD dwPID)
  158. {
  159. ASSERT(lpModuleName!=NULL);
  160. DWORD dwMID = 0;
  161. MODULEENTRY32 me32 = { 0 };
  162. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, dwPID);
  163. if (hSnapshot == NULL)
  164. return NULL;
  165. me32.dwSize = sizeof(PROCESSENTRY32);
  166. if (Module32First(hSnapshot, &me32))
  167. {
  168. do {
  169. if (_tcsicmp(lpModuleName, me32.szModule) == 0)
  170. {
  171. break;
  172. }
  173. } while (Module32Next(hSnapshot, &me32));
  174. }
  175. CloseHandle(hSnapshot);
  176. return me32.hModule;
  177. }
  178. HANDLE FindModuleEx(LPCTSTR lpModuleName, DWORD dwPid)
  179. {
  180. HMODULE hMods[1024] = {0};
  181. DWORD cbNeeded = 0;
  182. TCHAR szModName[MAX_PATH];
  183. BOOL Wow64Process;
  184. HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwPid);
  185. IsWow64Process(hProcess, &Wow64Process); //判断是32位还是64位进程
  186. if ( EnumProcessModulesEx(hProcess, hMods, sizeof(hMods), &cbNeeded, Wow64Process?LIST_MODULES_32BIT:LIST_MODULES_64BIT) )
  187. {
  188. for (UINT i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
  189. {
  190. GetModuleFileNameEx(hProcess, hMods[i], szModName, _countof(szModName));
  191. if (_tcsicmp(lpModuleName, szModName) == 0)
  192. {
  193. CloseHandle(hProcess);
  194. return hMods[i];
  195. }
  196. }
  197. }
  198. CloseHandle(hProcess);
  199. return NULL;
  200. }
  201. // WINDOWS NT 以上的内核需要提权,才能对系统进行高级管理;
  202. /************************************************************************/
  203. /* 函数:[1/6/2019 Home];
  204. /* 描述:;
  205. /* 参数:;
  206. /* [IN] :;
  207. /* [OUT] :;
  208. /* [IN/OUT] :;
  209. /* 返回:void;
  210. /* 注意:;
  211. /* 示例:;
  212. /*
  213. /* 修改:;
  214. /* 日期:;
  215. /* 内容:;
  216. /************************************************************************/
  217. BOOL GetDebugPriv()
  218. {
  219. // 返回的访问令牌指针;
  220. HANDLE hToken;
  221. // 接收所返回的制定特权名称的信息;
  222. LUID sedebugnameValue;
  223. // 新特权信息的指针(结构体);
  224. TOKEN_PRIVILEGES tkp;
  225. //DWORD dwCurProcId = GetCurrentProcessId();
  226. // 要修改访问权限的进程句柄;
  227. HANDLE hCurProc = ::GetCurrentProcess();
  228. //hCurProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurProcId);
  229. if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  230. {
  231. return FALSE;
  232. }
  233. if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
  234. {
  235. CloseHandle(hToken);
  236. return FALSE;
  237. }
  238. tkp.PrivilegeCount = 1;
  239. tkp.Privileges[0].Luid = sedebugnameValue;
  240. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  241. if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
  242. {
  243. CloseHandle(hToken);
  244. return FALSE;
  245. }
  246. CloseHandle(hCurProc);
  247. CloseHandle(hToken);
  248. return TRUE;
  249. }
  250. /************************************************************************/
  251. /*
  252. 函数:GetFileVersion
  253. 描述:获取可执行文件的文件版号;
  254. 参数:
  255. hModule[IN] 可执行文件模块句柄;
  256. dwArray[OUT] 返回的文件版本号;
  257. 返回:
  258. 成功返回TRUE,失败返回FALSE;
  259. 注意:
  260. 当hModule为空时,表示要获取的可执行文件为本程序的文件版本号;
  261. */
  262. /************************************************************************/
  263. BOOL GetFileVersion( IN HMODULE hModule, OUT DWORD (&dwArray)[4])
  264. {
  265. TCHAR fname[MAX_PATH];
  266. VS_FIXEDFILEINFO *pVi;
  267. DWORD dwHandle;
  268. if ( GetModuleFileName(hModule, fname, MAX_PATH))
  269. {
  270. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  271. if (nSize > 0)
  272. {
  273. BYTE *pBuffer = new BYTE[nSize];
  274. memset(pBuffer, 0, nSize);
  275. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  276. {
  277. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  278. {
  279. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  280. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  281. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  282. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  283. delete[]pBuffer;
  284. return TRUE;
  285. }
  286. }
  287. if ( pBuffer )
  288. delete[]pBuffer;
  289. }
  290. }
  291. return FALSE;
  292. }
  293. /************************************************************************/
  294. /*
  295. 函数:GetFileVersion
  296. 描述:获取可执行文件的文件版号;
  297. 参数:
  298. lpFileName[IN] 可执行文件名全路径;
  299. dwArray[OUT] 返回的文件版本号;
  300. 返回:
  301. 成功返回TRUE,失败返回FALSE;
  302. 注意:
  303. */
  304. /************************************************************************/
  305. BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  306. {
  307. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  308. {
  309. OutputDebugString(_T("文件名错误或文件不存在\n"));
  310. return FALSE;
  311. }
  312. DWORD dwHandle = 0;
  313. VS_FIXEDFILEINFO *pVi = NULL;
  314. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  315. if ( nSize > 0 )
  316. {
  317. BYTE *pBuffer = new BYTE[nSize];
  318. memset(pBuffer, 0, nSize);
  319. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  320. {
  321. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  322. {
  323. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  324. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  325. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  326. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  327. if (pBuffer)
  328. delete[]pBuffer;
  329. return TRUE;
  330. }
  331. }
  332. if (pBuffer)
  333. delete[]pBuffer;
  334. }
  335. return FALSE;
  336. }
  337. /************************************************************************/
  338. /*
  339. 函数:GetProductVersion
  340. 描述:获取可执行文件的产品版号;
  341. 参数:
  342. hModule[IN] 可执行文件模块句柄;
  343. dwArray[OUT] 返回的产品版本号;
  344. 返回:
  345. 成功返回TRUE,失败返回FALSE;
  346. 注意:
  347. 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号;
  348. */
  349. /************************************************************************/
  350. BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
  351. {
  352. TCHAR fname[MAX_PATH];
  353. VS_FIXEDFILEINFO *pVi;
  354. DWORD dwHandle;
  355. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  356. {
  357. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  358. if (nSize > 0)
  359. {
  360. BYTE *pBuffer = new BYTE[nSize];
  361. memset(pBuffer, 0, nSize);
  362. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  363. {
  364. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  365. {
  366. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  367. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  368. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  369. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  370. if(pBuffer)
  371. delete[]pBuffer;
  372. return TRUE;
  373. }
  374. }
  375. if(pBuffer)
  376. delete[]pBuffer;
  377. }
  378. }
  379. return FALSE;
  380. }
  381. /************************************************************************/
  382. /*
  383. 函数:GetProductVersion
  384. 描述:获取可执行文件的产品版号;
  385. 参数:
  386. lpFileName[IN] 可执行文件名全路径;
  387. dwArray[OUT] 返回的产品版本号;
  388. 返回:
  389. 成功返回TRUE,失败返回FALSE;
  390. 注意:
  391. */
  392. /************************************************************************/
  393. BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  394. {
  395. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  396. {
  397. OutputDebugString(_T("文件名错误或文件不存在\n"));
  398. return FALSE;
  399. }
  400. DWORD dwHandle = 0;
  401. VS_FIXEDFILEINFO *pVi = NULL;
  402. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  403. if ( nSize > 0 )
  404. {
  405. BYTE *pBuffer = new BYTE[nSize];
  406. memset(pBuffer, 0, nSize);
  407. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  408. {
  409. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  410. {
  411. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  412. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  413. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  414. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  415. if (pBuffer)
  416. delete[]pBuffer;
  417. return TRUE;
  418. }
  419. }
  420. if (pBuffer)
  421. delete[]pBuffer;
  422. }
  423. return FALSE;
  424. }
  425. /************************************************************************/
  426. /* 函数:WriteTextLog[7/28/2016 IT];
  427. /* 描述:写文本日志;
  428. /* 参数:;
  429. /* [IN] :;
  430. /* 返回:void;
  431. /* 注意:;
  432. /* 示例:;
  433. /*
  434. /* 修改:;
  435. /* 日期:;
  436. /* 内容:;
  437. /************************************************************************/
  438. void WriteTextLog(const TCHAR *format, ...)
  439. {
  440. #if 0
  441. try
  442. {
  443. //static ThreadSection _critSection;
  444. //AutoThreadSection aSection(&_critSection);
  445. // 解析出日志路径;
  446. TCHAR szlogpath[MAX_PATH] = {0};
  447. static TCHAR szModulePath[MAX_PATH] = {0};
  448. static TCHAR szFna[_MAX_DIR] = { 0 };
  449. if ( szModulePath[0] == _T('\0') )
  450. {
  451. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  452. TCHAR szDir[_MAX_DIR] = { 0 };
  453. TCHAR szExt[_MAX_DIR] = { 0 };
  454. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  455. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  456. _tcscpy_s(szModulePath, szDrive);
  457. _tcscat_s(szModulePath, szDir);
  458. }
  459. _stprintf_s(szlogpath, _T("%s日志\\%s%s.txt"), szModulePath, szFna, CTime::GetCurrentTime().Format("[%Y-%m-%d]"));
  460. // 打开或创建文件;
  461. CStdioFile fp;
  462. if (PathFileExists(szlogpath))
  463. {
  464. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  465. {
  466. return;
  467. }
  468. fp.SeekToEnd();
  469. }
  470. else
  471. {
  472. fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite);
  473. }
  474. // 格式化前设置语言区域;
  475. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  476. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  477. // 格式化日志内容;
  478. va_list args = NULL;
  479. int len = 0;
  480. TCHAR *buffer = NULL;
  481. va_start( args, format );
  482. // _vscprintf doesn't count. terminating '\0'
  483. len = _vsctprintf( format, args ) + 1;
  484. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  485. _vstprintf_s( buffer, len, format, args ); // C4996
  486. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  487. // 将日志内容输入到文件中;
  488. fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  489. fp.WriteString(buffer);
  490. fp.WriteString(_T("\n"));
  491. // 关闭文件,释放资源并设置回原语言区域;
  492. free( buffer );
  493. _tsetlocale(LC_CTYPE, old_locale);
  494. free(old_locale);//还原区域设定;
  495. fp.Close();
  496. }
  497. catch (CException *e)
  498. {
  499. e->ReportError();
  500. e->Delete();
  501. }
  502. #else
  503. // 解析出日志路径;
  504. TCHAR szlogpath[MAX_PATH] = { 0 };
  505. static TCHAR szModulePath[MAX_PATH] = { 0 };
  506. static TCHAR szFna[MAX_PATH] = { 0 };
  507. if (szModulePath[0] == _T('\0'))
  508. {
  509. TCHAR szDrive[MAX_PATH] = { 0 };
  510. TCHAR szDir[MAX_PATH] = { 0 };
  511. TCHAR szExt[MAX_PATH] = { 0 };
  512. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  513. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  514. _tcscpy_s(szModulePath, szDrive);
  515. _tcscat_s(szModulePath, szDir);
  516. }
  517. _stprintf_s(szlogpath, _T("%s%s.txt"), szModulePath, szFna);
  518. // 打开或创建文件;
  519. FILE *fp = NULL;
  520. //if (_taccess(szlogpath, 0) != -1)
  521. #ifndef UNICODE
  522. if (_access(szlogpath, 0) != -1)
  523. #else
  524. if (_taccess(szlogpath, 0) != -1)
  525. #endif
  526. {// 存在;
  527. fp = _tfopen(szlogpath, _T("a+"));
  528. // 移动到末尾;
  529. fseek(fp, 0, SEEK_END);
  530. }
  531. else
  532. {// 不存在;
  533. fp = _tfopen(szlogpath, _T("w+"));
  534. }
  535. if (fp == NULL)
  536. return;
  537. // 格式化前设置语言区域;
  538. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  539. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  540. // 格式化日志内容;
  541. va_list args = NULL;
  542. int len = 0;
  543. TCHAR *buffer = NULL;
  544. va_start(args, format);
  545. // _vscprintf doesn't count. terminating '\0'
  546. len = _vsctprintf(format, args) + 1;
  547. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  548. _vstprintf_s(buffer, len, format, args);
  549. // 将日志内容输入到文件中;
  550. // 获取今年年份;
  551. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  552. struct tm gmtm = { 0 };
  553. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  554. _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);
  555. // 关闭文件,释放资源并设置回原语言区域;
  556. free(buffer);
  557. fclose(fp);
  558. _tsetlocale(LC_CTYPE, old_locale);
  559. free(old_locale);//还原区域设定;
  560. #endif
  561. }
  562. //---------------------------------------------------------------------
  563. // add by Jeff 2014.10.27
  564. // 函数:全局函数IsDirectoryLegitimate,多字节版本,非UNICODE
  565. // 描述:判断一个目录路径字符串,是否属于合法的、可创建的目录路径。
  566. // 参数:strDirectory 被验证的路径字符串;
  567. //
  568. // 返回:合法路径返回TRUE;
  569. //---------------------------------------------------------------------
  570. BOOL IsDirectoryLegitimate(const CString &strDirectory)
  571. {
  572. if (strDirectory.Find('/') != -1 ||
  573. strDirectory.Find('\\') != -1 ||
  574. strDirectory.Find(':') != -1 ||
  575. strDirectory.Find('*') != -1 ||
  576. strDirectory.Find('?') != -1 ||
  577. strDirectory.Find('\"') != -1 ||
  578. strDirectory.Find('>') != -1 ||
  579. strDirectory.Find('<') != -1 ||
  580. strDirectory.Find('|') != -1
  581. )
  582. return FALSE;
  583. return TRUE;
  584. }
  585. //--------------------------------------------------------------------------------
  586. // Jeff add 2014.06.23;
  587. // 函数:ErrorExit
  588. // 描述:
  589. // 参数:
  590. // lpszFunction:函数名;
  591. // dwError:错误码;
  592. //
  593. //--------------------------------------------------------------------------------
  594. void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError)
  595. {
  596. #if 1
  597. LPVOID lpMsgBuf;
  598. BOOL fOk = FormatMessage(
  599. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  600. FORMAT_MESSAGE_FROM_SYSTEM |
  601. FORMAT_MESSAGE_IGNORE_INSERTS,
  602. NULL,
  603. dwError,
  604. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  605. (LPTSTR)&lpMsgBuf,
  606. 0, NULL);
  607. if (!fOk)
  608. {
  609. // Is it a network-related error?
  610. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  611. if (hDll != NULL)
  612. {
  613. FormatMessage(
  614. FORMAT_MESSAGE_FROM_HMODULE |
  615. FORMAT_MESSAGE_FROM_SYSTEM |
  616. FORMAT_MESSAGE_IGNORE_INSERTS,
  617. hDll,
  618. dwError,
  619. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  620. (LPTSTR)&lpMsgBuf,
  621. 0,
  622. NULL);
  623. FreeLibrary(hDll);
  624. }
  625. }
  626. if (lpMsgBuf != NULL)
  627. {
  628. CString strDisplay;
  629. strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
  630. //WriteLog(strDisplay);
  631. LocalFree(lpMsgBuf);
  632. }
  633. else
  634. {
  635. //WriteLog(strDescription);
  636. }
  637. #else
  638. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  639. // Get the error code's textual description
  640. BOOL fOk = FormatMessage(
  641. FORMAT_MESSAGE_FROM_SYSTEM |
  642. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  643. FORMAT_MESSAGE_IGNORE_INSERTS,
  644. NULL,
  645. dwError,
  646. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  647. (PTSTR)&hlocal,
  648. 0,
  649. NULL);
  650. if (!fOk)
  651. {
  652. // Is it a network-related error?
  653. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  654. if (hDll != NULL)
  655. {
  656. FormatMessage(
  657. FORMAT_MESSAGE_FROM_HMODULE |
  658. FORMAT_MESSAGE_FROM_SYSTEM |
  659. FORMAT_MESSAGE_IGNORE_INSERTS,
  660. hDll,
  661. dwError,
  662. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  663. (PTSTR)&hlocal,
  664. 0,
  665. NULL);
  666. FreeLibrary(hDll);
  667. }
  668. }
  669. if (hlocal != NULL)
  670. {
  671. CString strDisplay;
  672. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  673. //WriteLog(strDisplay);
  674. LocalFree(hlocal);
  675. }
  676. else
  677. {
  678. //WriteLog("Error number not found.");
  679. }
  680. #endif
  681. }
  682. // 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).
  683. // After the time-out interval elapses, the system is restarted.
  684. //The application must enable the SE_SHUTDOWN_NAME privilege before calling InitiateSystemShutdown
  685. BOOL MySystemShutdown(LPTSTR lpMsg)
  686. {
  687. HANDLE hToken; // handle to process token
  688. TOKEN_PRIVILEGES tkp; // pointer to token structure
  689. BOOL fResult; // system shutdown flag
  690. // Get the current process token handle so we can get shutdown
  691. // privilege.
  692. if (!OpenProcessToken(GetCurrentProcess(),
  693. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  694. return FALSE;
  695. // Get the LUID for shutdown privilege.
  696. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  697. &tkp.Privileges[0].Luid);
  698. tkp.PrivilegeCount = 1; // one privilege to set
  699. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  700. // Get shutdown privilege for this process.
  701. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  702. (PTOKEN_PRIVILEGES)NULL, 0);
  703. // Cannot test the return value of AdjustTokenPrivileges.
  704. if (GetLastError() != ERROR_SUCCESS)
  705. return FALSE;
  706. // Display the shutdown dialog box and start the countdown.
  707. fResult = InitiateSystemShutdown(
  708. NULL, // shut down local computer
  709. lpMsg, // message for user
  710. 30, // time-out period, in seconds
  711. FALSE, // ask user to close apps
  712. TRUE); // reboot after shutdown
  713. if (!fResult)
  714. return FALSE;
  715. // Disable shutdown privilege.
  716. tkp.Privileges[0].Attributes = 0;
  717. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  718. (PTOKEN_PRIVILEGES)NULL, 0);
  719. return TRUE;
  720. }
  721. // If the AbortSystemShutdown function is executed in the time-out period specified by InitiateSystemShutdown,
  722. // the system does not shut down. For example, if PreventSystemShutdown is called after MySystemShutdown,
  723. // the system closes the dialog box and does not restart the system.
  724. BOOL PreventSystemShutdown()
  725. {
  726. HANDLE hToken; // handle to process token
  727. TOKEN_PRIVILEGES tkp; // pointer to token structure
  728. // Get the current process token handle so we can get shutdown
  729. // privilege.
  730. if (!OpenProcessToken(GetCurrentProcess(),
  731. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  732. return FALSE;
  733. // Get the LUID for shutdown privilege.
  734. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  735. &tkp.Privileges[0].Luid);
  736. tkp.PrivilegeCount = 1; // one privilege to set
  737. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  738. // Get shutdown privilege for this process.
  739. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  740. (PTOKEN_PRIVILEGES)NULL, 0);
  741. if (GetLastError() != ERROR_SUCCESS)
  742. return FALSE;
  743. // Prevent the system from shutting down.
  744. if (!AbortSystemShutdown(NULL))
  745. return FALSE;
  746. // Disable shutdown privilege.
  747. tkp.Privileges[0].Attributes = 0;
  748. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  749. (PTOKEN_PRIVILEGES)NULL, 0);
  750. return TRUE;
  751. }
  752. // Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer
  753. // The application must first enable the SE_SHUTDOWN_NAME privilege.
  754. // The final parameter in the call to ExitWindowsEx indicates that the system was shut down for a planning update of the operating system.
  755. BOOL MySystemShutdown()
  756. {
  757. HANDLE hToken;
  758. TOKEN_PRIVILEGES tkp;
  759. // Get a token for this process.
  760. if (!OpenProcessToken(GetCurrentProcess(),
  761. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  762. return(FALSE);
  763. // Get the LUID for the shutdown privilege.
  764. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  765. &tkp.Privileges[0].Luid);
  766. tkp.PrivilegeCount = 1; // one privilege to set
  767. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  768. // Get the shutdown privilege for this process.
  769. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  770. (PTOKEN_PRIVILEGES)NULL, 0);
  771. if (GetLastError() != ERROR_SUCCESS)
  772. return FALSE;
  773. // Shut down the system and force all applications to close.
  774. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  775. SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
  776. SHTDN_REASON_MINOR_UPGRADE |
  777. SHTDN_REASON_FLAG_PLANNED))
  778. return FALSE;
  779. return TRUE;
  780. }
  781. BOOL getWeChatPath()
  782. {
  783. // 通过注册表获取微信安装目录;
  784. HKEY hKey = NULL;
  785. if(ERROR_SUCCESS != RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Tencent\\WeChat"), &hKey))
  786. {
  787. return FALSE;
  788. }
  789. DWORD Type = REG_SZ;
  790. DWORD cbData = MAX_PATH*sizeof(WCHAR);
  791. if(ERROR_SUCCESS != RegQueryValueEx(hKey, _T("InstallPath"), 0, &Type, (LPBYTE)g_szWeChatPath, &cbData))
  792. {
  793. RegCloseKey(hKey);
  794. return FALSE;
  795. }
  796. PathAppend(g_szWeChatPath, _T("WeChat.exe"));
  797. return TRUE;
  798. }
  799. BOOL OpenWeChat()
  800. {
  801. #if 1
  802. STARTUPINFO si;
  803. PROCESS_INFORMATION pi;
  804. ZeroMemory(&si, sizeof(si));
  805. si.cb = sizeof(si);
  806. ZeroMemory(&pi, sizeof(pi));
  807. si.dwFlags = STARTF_USESHOWWINDOW; // 指定wShowWindow成员有效
  808. si.wShowWindow = TRUE; // 此成员设为TRUE的话则显示新建进程的主窗口,
  809. // 为FALSE的话则不显示
  810. BOOL bRet = ::CreateProcess (
  811. g_szWeChatPath, // 不在此指定可执行文件的文件名
  812. NULL, // 命令行参数
  813. NULL, // 默认进程安全性
  814. NULL, // 默认线程安全性
  815. FALSE, // 指定当前进程内的句柄不可以被子进程继承
  816. //CREATE_SUSPENDED, // 挂起进程;CREATE_SUSPENDED
  817. //NORMAL_PRIORITY_CLASS,
  818. //CREATE_NEW_CONSOLE,
  819. NULL,
  820. NULL, // 使用本进程的环境变量
  821. NULL, // 使用本进程的驱动器和目录
  822. &si,
  823. &pi);
  824. if(bRet)
  825. {
  826. // 进程挂起后,仍能成功注入dll;
  827. // TCHAR szDllPath[MAX_PATH];
  828. // ZeroMemory(szDllPath,MAX_PATH);
  829. // _stprintf_s(szDllPath, _T("%shook.dll"), g_szModulePath);
  830. // for (int i = 0; i < 10; i++)
  831. // {
  832. // CInjection inject(pi.dwProcessId,szDllPath);
  833. // inject.InjectDynamicLibrary();
  834. // inject.EjectDynamicLibrary();
  835. // }
  836. //不sleep就会出现读取不到的297错误
  837. //Sleep(5000);
  838. //获取线程上下文
  839. // CONTEXT ct = { 0 };
  840. // ct.ContextFlags = CONTEXT_CONTROL;
  841. // GetThreadContext(pi.hThread, &ct);
  842. //
  843. // ::ResumeThread(pi.hThread);
  844. // 既然我们不使用两个句柄,最好是立刻将它们关闭
  845. ::CloseHandle (pi.hThread);
  846. ::CloseHandle (pi.hProcess);
  847. // 当进程挂起时,是无法修改关闭微信句柄;
  848. PatchWeChat();
  849. }
  850. #else
  851. SHELLEXECUTEINFO sei;
  852. memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
  853. sei.cbSize = sizeof(SHELLEXECUTEINFO);
  854. sei.hwnd = NULL;
  855. sei.lpVerb = _T("open");
  856. //sei.lpVerb = _T("runas");
  857. //sei.fMask = SEE_MASK_NOCLOSEPROCESS;
  858. sei.lpFile = g_szWeChatPath;
  859. sei.lpParameters = NULL;
  860. sei.lpDirectory = NULL;
  861. sei.nShow = SW_NORMAL;
  862. sei.hInstApp = NULL;
  863. if ( !ShellExecuteEx(&sei) )
  864. {
  865. DWORD dw = GetLastError();
  866. return FALSE;
  867. }
  868. PatchWeChat();
  869. #endif
  870. return TRUE;
  871. }