Global.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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. }
  919. //////////////////////////////////////////////////////////////////////////
  920. static const int BEGIN_YEAR = 1901;
  921. static const int NUMBER_YEAR = 199;
  922. static const unsigned int LUNAR_YEARS[199] =
  923. {
  924. 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, //1901-1910
  925. 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, //1911-1920
  926. 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, //1921-1930
  927. 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, //1931-1940
  928. 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, //1941-1950
  929. 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x7AD53C, //1951-1960
  930. 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, //1961-1970
  931. 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, //1971-1980
  932. 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, //1981-1990
  933. 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, //1991-2000
  934. 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, //2001-2010
  935. 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, //2011-2020
  936. 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, //2021-2030
  937. 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, //2031-2040
  938. 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, //2041-2050
  939. 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, //2051-2060
  940. 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, //2061-2070
  941. 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, //2071-2080
  942. 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, //2081-2090
  943. 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 //2091-2099
  944. };
  945. //////////////////////////////////////////////////////////////////////////
  946. BOOL IsValidDate(IN LPCTSTR lpDate) // 判断公历;
  947. {
  948. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  949. return FALSE;
  950. CString strDate = lpDate;
  951. if (strDate.IsEmpty()) return FALSE;
  952. // 判断日期合法性;
  953. static int nYear = 0;
  954. static int nMonth = 0;
  955. static int nDay = 0;
  956. nYear = nMonth = nDay = 0;
  957. nYear = _ttoi(strDate.Left(4));
  958. nMonth = _ttoi(strDate.Mid(5, 2));
  959. nDay = _ttoi(strDate.Mid(8, 2));
  960. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  961. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  962. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  963. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  964. {
  965. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  966. }
  967. else
  968. {
  969. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  970. }
  971. return TRUE;
  972. }
  973. BOOL IsValidLunanrDate(IN LPCTSTR lpDate) // 判断农历;
  974. {
  975. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  976. return FALSE;
  977. CString strDate = lpDate;
  978. if (strDate.IsEmpty()) return FALSE;
  979. static int lunar_year, lunar_month, lunar_day;
  980. lunar_year = lunar_month = lunar_day = 0;
  981. lunar_year = _ttoi(strDate.Left(4));
  982. lunar_month = _ttoi(strDate.Mid(5, 2));
  983. lunar_day = _ttoi(strDate.Mid(8, 2));
  984. //越界检查,如果越界,返回无效日期;
  985. if (lunar_year <= BEGIN_YEAR || lunar_year > BEGIN_YEAR + NUMBER_YEAR - 1)
  986. return FALSE;
  987. if (lunar_month < 1 || lunar_month > 12)
  988. return FALSE;
  989. // 1.农历年是否有闰月;
  990. int nYearIndex = lunar_year - BEGIN_YEAR;
  991. int leap_month = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF;
  992. // 2.月份大小月天数;
  993. int month_day = 0;
  994. if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - lunar_month)) & 0x1)
  995. month_day = 30;
  996. else
  997. month_day = 29;
  998. if (lunar_day < 0 || lunar_day > month_day)
  999. return FALSE;
  1000. return TRUE;
  1001. }