Global.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. #if 1
  803. STARTUPINFO si;
  804. PROCESS_INFORMATION pi;
  805. ZeroMemory(&si, sizeof(si));
  806. si.cb = sizeof(si);
  807. ZeroMemory(&pi, sizeof(pi));
  808. si.dwFlags = STARTF_USESHOWWINDOW; // 指定wShowWindow成员有效
  809. si.wShowWindow = TRUE; // 此成员设为TRUE的话则显示新建进程的主窗口,
  810. // 为FALSE的话则不显示
  811. BOOL bRet = ::CreateProcess (
  812. g_szWeChatPath, // 不在此指定可执行文件的文件名
  813. NULL, // 命令行参数
  814. NULL, // 默认进程安全性
  815. NULL, // 默认线程安全性
  816. FALSE, // 指定当前进程内的句柄不可以被子进程继承
  817. //CREATE_SUSPENDED, // 挂起进程;CREATE_SUSPENDED
  818. //NORMAL_PRIORITY_CLASS,
  819. //CREATE_NEW_CONSOLE,
  820. NULL,
  821. NULL, // 使用本进程的环境变量
  822. NULL, // 使用本进程的驱动器和目录
  823. &si,
  824. &pi);
  825. if(bRet)
  826. {
  827. // 进程挂起后,仍能成功注入dll;
  828. // TCHAR szDllPath[MAX_PATH];
  829. // ZeroMemory(szDllPath,MAX_PATH);
  830. // _stprintf_s(szDllPath, _T("%shook.dll"), g_szModulePath);
  831. // for (int i = 0; i < 10; i++)
  832. // {
  833. // CInjection inject(pi.dwProcessId,szDllPath);
  834. // inject.InjectDynamicLibrary();
  835. // inject.EjectDynamicLibrary();
  836. // }
  837. //不sleep就会出现读取不到的297错误
  838. //Sleep(5000);
  839. //获取线程上下文
  840. // CONTEXT ct = { 0 };
  841. // ct.ContextFlags = CONTEXT_CONTROL;
  842. // GetThreadContext(pi.hThread, &ct);
  843. //
  844. // ::ResumeThread(pi.hThread);
  845. // 既然我们不使用两个句柄,最好是立刻将它们关闭
  846. ::CloseHandle (pi.hThread);
  847. ::CloseHandle (pi.hProcess);
  848. // 当进程挂起时,是无法修改关闭微信句柄;
  849. PatchWeChat();
  850. }
  851. #else
  852. SHELLEXECUTEINFO sei;
  853. memset(&sei, 0, sizeof(SHELLEXECUTEINFO));
  854. sei.cbSize = sizeof(SHELLEXECUTEINFO);
  855. sei.hwnd = NULL;
  856. sei.lpVerb = _T("open");
  857. //sei.lpVerb = _T("runas");
  858. //sei.fMask = SEE_MASK_NOCLOSEPROCESS;
  859. sei.lpFile = g_szWeChatPath;
  860. sei.lpParameters = NULL;
  861. sei.lpDirectory = NULL;
  862. sei.nShow = SW_NORMAL;
  863. sei.hInstApp = NULL;
  864. if ( !ShellExecuteEx(&sei) )
  865. {
  866. DWORD dw = GetLastError();
  867. return FALSE;
  868. }
  869. PatchWeChat();
  870. #endif
  871. return TRUE;
  872. }