Global.cpp 27 KB

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