Global.cpp 26 KB

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