Global.cpp 26 KB

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