Global.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "Global.h"
  4. #include <io.h>
  5. #include <direct.h>
  6. #include <tlhelp32.h>
  7. #include <shlwapi.h>
  8. #include <strsafe.h>
  9. #include <WinVer.h>
  10. #pragma comment(lib,"version.lib")
  11. #include "CritSection.h"
  12. // 获取本机ip地址;
  13. #include <Iphlpapi.h>
  14. #pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
  15. // 获取网卡地址;
  16. #include <SetupAPI.h>
  17. #include <InitGuid.h>
  18. #include <WinIoCtl.h>
  19. #pragma comment(lib, "SetupAPI.lib")
  20. //#include <psapi.h>
  21. namespace GLOBAL
  22. {
  23. //////////////////////////////////////////////////////////////////////////
  24. // 全局变量;
  25. TCHAR g_szIniFile[MAX_PATH] = {0};
  26. TCHAR g_szCurModuleFileName[MAX_PATH] = { 0 }; // 软件名称;
  27. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  28. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  29. TCHAR g_szFna[_MAX_FNAME] = { 0 };
  30. TCHAR g_szExt[_MAX_EXT] = { 0 };
  31. TCHAR g_szPython27Dir[MAX_PATH] = {0};
  32. //////////////////////////////////////////////////////////////////////////
  33. ThreadSection g_critSection;
  34. ThreadSection g_csTextLog;
  35. //////////////////////////////////////////////////////////////////////////
  36. static const int BEGIN_YEAR = 1901;
  37. static const int NUMBER_YEAR = 199;
  38. static const unsigned int LUNAR_YEARS[199] =
  39. {
  40. 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, //1901-1910
  41. 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, //1911-1920
  42. 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, //1921-1930
  43. 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, //1931-1940
  44. 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, //1941-1950
  45. 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x7AD53C, //1951-1960
  46. 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, //1961-1970
  47. 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, //1971-1980
  48. 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, //1981-1990
  49. 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, //1991-2000
  50. 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, //2001-2010
  51. 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, //2011-2020
  52. 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, //2021-2030
  53. 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, //2031-2040
  54. 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, //2041-2050
  55. 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, //2051-2060
  56. 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, //2061-2070
  57. 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, //2071-2080
  58. 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, //2081-2090
  59. 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 //2091-2099
  60. };
  61. //////////////////////////////////////////////////////////////////////////
  62. BOOL IsValidDate(IN LPCTSTR lpDate) // 判断公历;
  63. {
  64. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  65. return FALSE;
  66. CString strDate = lpDate;
  67. if (strDate.IsEmpty()) return FALSE;
  68. // 判断日期合法性;
  69. static int nYear = 0;
  70. static int nMonth = 0;
  71. static int nDay = 0;
  72. nYear = nMonth = nDay = 0;
  73. nYear = _ttoi(strDate.Left(4));
  74. nMonth = _ttoi(strDate.Mid(5, 2));
  75. nDay = _ttoi(strDate.Mid(8, 2));
  76. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  77. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  78. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  79. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  80. {
  81. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  82. }
  83. else
  84. {
  85. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  86. }
  87. return TRUE;
  88. }
  89. BOOL IsValidLunanrDate(IN LPCTSTR lpDate) // 判断农历;
  90. {
  91. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  92. return FALSE;
  93. CString strDate = lpDate;
  94. if (strDate.IsEmpty()) return FALSE;
  95. static int lunar_year, lunar_month, lunar_day;
  96. lunar_year = lunar_month = lunar_day = 0;
  97. lunar_year = _ttoi(strDate.Left(4));
  98. lunar_month = _ttoi(strDate.Mid(5, 2));
  99. lunar_day = _ttoi(strDate.Mid(8, 2));
  100. //越界检查,如果越界,返回无效日期;
  101. if (lunar_year <= BEGIN_YEAR || lunar_year > BEGIN_YEAR + NUMBER_YEAR - 1)
  102. return FALSE;
  103. if (lunar_month < 1 || lunar_month > 12)
  104. return FALSE;
  105. // 1.农历年是否有闰月;
  106. int nYearIndex = lunar_year - BEGIN_YEAR;
  107. int leap_month = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF;
  108. // 2.月份大小月天数;
  109. int month_day = 0;
  110. if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - lunar_month)) & 0x1)
  111. month_day = 30;
  112. else
  113. month_day = 29;
  114. if (lunar_day < 0 || lunar_day > month_day)
  115. return FALSE;
  116. return TRUE;
  117. }
  118. int GetIniInfo(IN const TCHAR *szPath , const TCHAR *szIniName )
  119. {
  120. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  121. TCHAR szDir[_MAX_DIR] = { 0 };
  122. TCHAR szExt[_MAX_EXT] = { 0 };
  123. HMODULE hModule = NULL;
  124. // 获取当前DLL模块路径,参数2使用DLL中任意一个函数变量名即可;
  125. GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)&IsValidLunanrDate, &hModule);
  126. if ( hModule )
  127. {
  128. GetModuleFileName(hModule, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
  129. }
  130. else
  131. {
  132. GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
  133. }
  134. _tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
  135. StringCchCat(g_szCurModuleDir, MAX_PATH, szDrive);
  136. StringCchCat(g_szCurModuleDir, MAX_PATH, szDir);
  137. return 0;
  138. }
  139. // 判断指定字符串是否在数组里(区分大小写);
  140. BOOL IsStringExist(IN CString& str, IN CStringArray &tagAry)
  141. {
  142. int nSize = tagAry.GetSize();
  143. if (nSize == 0) return FALSE;
  144. BOOL bExist = FALSE;
  145. for (int i = 0; i < nSize; i++)
  146. {
  147. if (str == tagAry.ElementAt(i))
  148. {
  149. bExist = TRUE;
  150. break;
  151. }
  152. }
  153. return bExist;
  154. }
  155. // 判断指定字符串是否在数组里(区分大小写);
  156. BOOL IsStringExist(IN TString& str, IN vector<TString> &tagVt)
  157. {
  158. int nSize = tagVt.size();
  159. if (nSize == 0) return FALSE;
  160. BOOL bExist = FALSE;
  161. for (vector<TString>::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  162. {
  163. if (str.compare(it->c_str()) == 0)
  164. {
  165. bExist = TRUE;
  166. break;
  167. }
  168. }
  169. return bExist;
  170. }
  171. // 判断指定字符串是否在数组里(不区分大小写);
  172. BOOL IsStringExistNoCase(IN CString& str, IN CStringArray &tagAry)
  173. {
  174. int nSize = tagAry.GetSize();
  175. if (nSize == 0) return FALSE;
  176. BOOL bExist = FALSE;
  177. for (int i = 0; i < nSize; i++)
  178. {
  179. if (str.CompareNoCase(tagAry.ElementAt(i)) == 0)
  180. {
  181. bExist = TRUE;
  182. break;
  183. }
  184. }
  185. return bExist;
  186. }
  187. BOOL IsStringExistNoCase(IN CONST TString& str, IN CStringArray &tagAry)
  188. {
  189. int nSize = tagAry.GetSize();
  190. if (nSize == 0) return FALSE;
  191. BOOL bExist = FALSE;
  192. for (int i = 0; i < nSize; i++)
  193. {
  194. if ((tagAry.ElementAt(i).CompareNoCase(str.c_str())) == 0)
  195. {
  196. bExist = TRUE;
  197. break;
  198. }
  199. }
  200. return bExist;
  201. }
  202. // 判断指定字符串是否在数组里(不区分大小写);
  203. BOOL IsStringExistNoCase(IN CONST TString& str, IN vector<TString> &tagVt)
  204. {
  205. int nSize = tagVt.size();
  206. if (nSize == 0) return FALSE;
  207. BOOL bExist = FALSE;
  208. TString stmp1 = uppercase(str);
  209. TString stmp2;
  210. for (vector<TString>::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  211. {
  212. stmp2 = uppercase(*it);
  213. if (stmp1.compare(stmp2) == 0)
  214. {
  215. bExist = TRUE;
  216. break;
  217. }
  218. }
  219. return bExist;
  220. }
  221. TString uppercase(IN const TString &Str)
  222. {
  223. const TCHAR aazz = _T('z') - _T('Z');
  224. TString sResult;
  225. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  226. {
  227. if (_T('a') <= *it && *it <= _T('z'))
  228. sResult.append(1, *it - aazz);
  229. else
  230. sResult.append(1, *it);
  231. }
  232. return sResult;
  233. }
  234. int match(IN CONST TString &sFileExt, IN CONST TString &sFile)
  235. {
  236. TString fext = uppercase(sFileExt);
  237. TString file = uppercase(sFile);
  238. int pos = file.find_last_of(_T('.'));
  239. if (TString::npos != pos)
  240. file = file.substr(pos);
  241. return (TString::npos != fext.find(file));
  242. }
  243. TString getfilename(IN CONST TString &file)
  244. {
  245. TString name;
  246. TString strfile = file;
  247. int nIndex = strfile.find_last_of(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  248. if (nIndex == TString::npos)
  249. {
  250. nIndex = strfile.find_last_of(_T('.'));
  251. if ( nIndex == TString::npos )
  252. return _T("");
  253. return strfile.substr(0, nIndex);
  254. }
  255. name = strfile.substr(nIndex+1);
  256. nIndex = name.find_last_of(_T('.'));
  257. if (nIndex == TString::npos)
  258. return _T("");
  259. return name.substr(0, nIndex);
  260. }
  261. // 获取文件名;
  262. CString getfilename(IN CONST CString &file)
  263. {
  264. CString name;
  265. CString strfile = file;
  266. int nIndex = strfile.ReverseFind(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  267. if (nIndex == -1)
  268. {
  269. nIndex = strfile.ReverseFind(_T('.'));
  270. if (nIndex == -1)
  271. return _T("");
  272. return strfile.Left(nIndex);
  273. }
  274. name = strfile.Mid(nIndex+1);
  275. nIndex = name.Find(_T('.'));
  276. if (nIndex == -1)
  277. return _T("");
  278. return name.Left(nIndex);
  279. }
  280. // 通过路径名查找进程,返回进程ID;
  281. BOOL FindAppProcessID(IN CString path, OUT DWORD &dwProcessID)
  282. {
  283. int nIndex = path.ReverseFind('\\');
  284. path = path.Right(path.GetLength() - nIndex - 1);
  285. dwProcessID = FindProcess(path);
  286. return (dwProcessID != 0 ? TRUE : FALSE);
  287. BOOL bFound = FALSE;
  288. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  289. if ( handle == NULL )
  290. return FALSE;
  291. PROCESSENTRY32 Info;
  292. Info.dwSize = sizeof(PROCESSENTRY32);
  293. path.MakeLower();
  294. if (::Process32First(handle, &Info))
  295. {
  296. do{
  297. CString ss = Info.szExeFile;
  298. ss.MakeLower();
  299. if (ss == path)
  300. {
  301. dwProcessID = Info.th32ProcessID;
  302. bFound = TRUE;
  303. break;
  304. }
  305. } while (::Process32Next(handle, &Info));
  306. }
  307. CloseHandle(handle);
  308. return (bFound ? TRUE : FALSE);
  309. }
  310. // 根据路径名查找进程,返回进程ID;
  311. DWORD FindProcess(IN CString strProName)
  312. {
  313. DWORD dwProcessID = 0;
  314. PROCESSENTRY32 pe32 = { 0 };
  315. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  316. if (hProcessSnap == NULL)
  317. {
  318. return 0;
  319. }
  320. pe32.dwSize = sizeof(PROCESSENTRY32);
  321. if (Process32First(hProcessSnap, &pe32))
  322. {
  323. do
  324. {
  325. if (_tcscmp(strProName.GetString(), pe32.szExeFile) == 0)
  326. {
  327. dwProcessID = pe32.th32ProcessID;
  328. break;
  329. }
  330. } while (Process32Next(hProcessSnap, &pe32));
  331. }
  332. CloseHandle(hProcessSnap);
  333. return dwProcessID;
  334. }
  335. DWORD FindProcessCount(IN LPCTSTR lpExeName)
  336. {
  337. DWORD dwCount = 0;
  338. if ( lpExeName && lpExeName[0] != _T('\0'))
  339. {
  340. PROCESSENTRY32 pe32 = { 0 };
  341. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  342. if (hProcessSnap == NULL) return 0;
  343. pe32.dwSize = sizeof(PROCESSENTRY32);
  344. if ( !Process32First(hProcessSnap, &pe32) )
  345. return 0;
  346. do
  347. {
  348. if (_tcscmp(lpExeName, pe32.szExeFile) == 0)
  349. dwCount++;
  350. } while (Process32Next(hProcessSnap, &pe32));
  351. CloseHandle(hProcessSnap);
  352. }
  353. return dwCount;
  354. }
  355. // 根据路径名查找进程,查找成功并结束;
  356. int FindAndCloseProcess(IN CString strProName)
  357. {
  358. int nIndex = strProName.ReverseFind('\\');
  359. if (nIndex != -1) // 如果传的是全路径;
  360. strProName = strProName.Right(strProName.GetLength() - nIndex - 1);
  361. DWORD dwProcessID = 0;
  362. PROCESSENTRY32 pe32 = { 0 };
  363. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  364. if (hProcessSnap == NULL)
  365. {
  366. return 0;
  367. }
  368. pe32.dwSize = sizeof(PROCESSENTRY32);
  369. if (Process32First(hProcessSnap, &pe32))
  370. {
  371. do
  372. {
  373. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  374. {
  375. dwProcessID = pe32.th32ProcessID;
  376. break;
  377. }
  378. } while (Process32Next(hProcessSnap, &pe32));
  379. }
  380. CloseHandle(hProcessSnap);
  381. if (dwProcessID == 0)
  382. return 0;
  383. HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
  384. if (hProcess == NULL)
  385. return 0;
  386. DWORD dwError;
  387. if (!TerminateProcess(hProcess, 0))
  388. {
  389. dwError = GetLastError();
  390. CloseHandle(hProcess);
  391. return -1;
  392. }
  393. // 等待进程结束响应;
  394. if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE))
  395. {
  396. CloseHandle(hProcess);
  397. AfxMessageBox(_T("结束进程失败"));
  398. return -1;
  399. }
  400. CloseHandle(hProcess);
  401. return 1;
  402. }
  403. /************************************************************************/
  404. /*
  405. 函数:GetDiskSpaceInfo
  406. 描述:获取指定目录或盘符的容量信息;
  407. 参数:
  408. IN: lpRootPathName 根目录名称;
  409. INOUT: dTotalNum 总容量;
  410. INOUT: dFreeNum 可用容量;
  411. 返回:
  412. 注意:
  413. GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G);
  414. GetDiskFreeSpaceEx支持64位(16384P=1024*16384T)
  415. 能获取本机硬盘信息,但路径必须包含":"冒号, 同时还可以获取网络路径的共享硬盘大小;
  416. 调用约定:
  417. */
  418. /************************************************************************/
  419. BOOL GetDiskSpaceInfo(IN LPCTSTR lpRootPathName, IN OUT double &dTotalNum, IN OUT double &dFreeNum)
  420. {
  421. #if 1
  422. // 校验参数有效性;
  423. if (lpRootPathName == NULL || _tcscmp(lpRootPathName, _T("")) == 0)
  424. {
  425. //_tprintf_s(_T("根目录为空!"));
  426. return FALSE;
  427. }
  428. // 如果长度==1;
  429. int nlen = _tcslen(lpRootPathName);
  430. if ((1 == nlen) || (2 == nlen && lpRootPathName[nlen - 1] != _T(':')))
  431. {
  432. //_tprintf_s(_T("根目录无效!\n"));
  433. return FALSE;
  434. }
  435. if (!PathFileExists(lpRootPathName))
  436. {
  437. //_tprintf_s(_T("根目录不存在!\n"));
  438. return FALSE;
  439. }
  440. #else
  441. // 使用IsValipath判断网络路径时较慢;
  442. if (!IsValidpath(lpRootPathName) || !PathFileExists(lpRootPathName)) return FALSE;
  443. #endif
  444. BOOL fResult = FALSE;
  445. DWORD dwSectPerClust; // 每簇的扇区数;
  446. DWORD dwBytesPerSect; // 每个扇区的字节数;
  447. DWORD dwFreeClusters; // 可用簇数;
  448. DWORD dwTotalClusters; // 总簇数;
  449. typedef BOOL(WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
  450. P_GDFSE pGetDiskFreeSpaceEx = NULL;
  451. /*unsigned*/ __int64 i64FreeBytesToCaller;
  452. /*unsigned*/ __int64 i64TotalBytes;
  453. /*unsigned*/ __int64 i64FreeBytes;
  454. // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace;
  455. #ifdef UNICODE
  456. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExW");
  457. #else
  458. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
  459. #endif
  460. if (pGetDiskFreeSpaceEx)
  461. {
  462. fResult = pGetDiskFreeSpaceEx(lpRootPathName,
  463. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  464. (PULARGE_INTEGER)&i64TotalBytes,
  465. (PULARGE_INTEGER)&i64FreeBytes);
  466. if (fResult)
  467. {
  468. // 将单位由byte转为G byte;
  469. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  470. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  471. }
  472. }
  473. else
  474. {
  475. fResult = GetDiskFreeSpace(lpRootPathName,
  476. &dwSectPerClust,
  477. &dwBytesPerSect,
  478. &dwFreeClusters,
  479. &dwTotalClusters);
  480. if (fResult)
  481. {
  482. // 成功获得容量信息;
  483. i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
  484. i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
  485. // 将单位由byte转为G byte;
  486. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  487. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  488. }
  489. }
  490. return fResult;
  491. }
  492. /************************************************************************/
  493. /*
  494. 函数:GetFileVersion
  495. 描述:获取可执行文件的文件版号;
  496. 参数:
  497. hModule[IN] 可执行文件模块句柄;
  498. dwArray[OUT] 返回的文件版本号;
  499. 返回:
  500. 成功返回TRUE,失败返回FALSE;
  501. 注意:
  502. 当hModule为空时,表示要获取的可执行文件为本程序的文件版本号;
  503. */
  504. /************************************************************************/
  505. BOOL GetFileVersion( IN HMODULE hModule, OUT DWORD (&dwArray)[4])
  506. {
  507. TCHAR fname[MAX_PATH];
  508. VS_FIXEDFILEINFO *pVi;
  509. DWORD dwHandle;
  510. if ( GetModuleFileName(hModule, fname, MAX_PATH))
  511. {
  512. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  513. if (nSize > 0)
  514. {
  515. BYTE *pBuffer = new BYTE[nSize];
  516. memset(pBuffer, 0, nSize);
  517. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  518. {
  519. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  520. {
  521. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  522. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  523. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  524. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  525. delete[]pBuffer;
  526. return TRUE;
  527. }
  528. }
  529. if ( pBuffer )
  530. delete[]pBuffer;
  531. }
  532. }
  533. return FALSE;
  534. }
  535. /************************************************************************/
  536. /*
  537. 函数:GetFileVersion
  538. 描述:获取可执行文件的文件版号;
  539. 参数:
  540. lpFileName[IN] 可执行文件名全路径;
  541. dwArray[OUT] 返回的文件版本号;
  542. 返回:
  543. 成功返回TRUE,失败返回FALSE;
  544. 注意:
  545. */
  546. /************************************************************************/
  547. BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  548. {
  549. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  550. {
  551. OutputDebugString(_T("文件名错误或文件不存在\n"));
  552. return FALSE;
  553. }
  554. DWORD dwHandle = 0;
  555. VS_FIXEDFILEINFO *pVi = NULL;
  556. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  557. if ( nSize > 0 )
  558. {
  559. BYTE *pBuffer = new BYTE[nSize];
  560. memset(pBuffer, 0, nSize);
  561. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  562. {
  563. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  564. {
  565. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  566. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  567. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  568. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  569. if (pBuffer)
  570. delete[]pBuffer;
  571. return TRUE;
  572. }
  573. }
  574. if (pBuffer)
  575. delete[]pBuffer;
  576. }
  577. return FALSE;
  578. }
  579. /************************************************************************/
  580. /*
  581. 函数:GetProductVersion
  582. 描述:获取可执行文件的产品版号;
  583. 参数:
  584. hModule[IN] 可执行文件模块句柄;
  585. dwArray[OUT] 返回的产品版本号;
  586. 返回:
  587. 成功返回TRUE,失败返回FALSE;
  588. 注意:
  589. 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号;
  590. */
  591. /************************************************************************/
  592. BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
  593. {
  594. TCHAR fname[MAX_PATH];
  595. VS_FIXEDFILEINFO *pVi;
  596. DWORD dwHandle;
  597. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  598. {
  599. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  600. if (nSize > 0)
  601. {
  602. BYTE *pBuffer = new BYTE[nSize];
  603. memset(pBuffer, 0, nSize);
  604. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  605. {
  606. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  607. {
  608. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  609. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  610. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  611. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  612. if(pBuffer)
  613. delete[]pBuffer;
  614. return TRUE;
  615. }
  616. }
  617. if(pBuffer)
  618. delete[]pBuffer;
  619. }
  620. }
  621. return FALSE;
  622. }
  623. /************************************************************************/
  624. /*
  625. 函数:GetProductVersion
  626. 描述:获取可执行文件的产品版号;
  627. 参数:
  628. lpFileName[IN] 可执行文件名全路径;
  629. dwArray[OUT] 返回的产品版本号;
  630. 返回:
  631. 成功返回TRUE,失败返回FALSE;
  632. 注意:
  633. */
  634. /************************************************************************/
  635. BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  636. {
  637. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  638. {
  639. OutputDebugString(_T("文件名错误或文件不存在\n"));
  640. return FALSE;
  641. }
  642. DWORD dwHandle = 0;
  643. VS_FIXEDFILEINFO *pVi = NULL;
  644. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  645. if ( nSize > 0 )
  646. {
  647. BYTE *pBuffer = new BYTE[nSize];
  648. memset(pBuffer, 0, nSize);
  649. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  650. {
  651. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  652. {
  653. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  654. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  655. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  656. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  657. if (pBuffer)
  658. delete[]pBuffer;
  659. return TRUE;
  660. }
  661. }
  662. if (pBuffer)
  663. delete[]pBuffer;
  664. }
  665. return FALSE;
  666. }
  667. /************************************************************************/
  668. /*
  669. 函数:
  670. 描述:
  671. 参数:
  672. 返回:
  673. 注意:
  674. */
  675. /************************************************************************/
  676. BOOL GetDirLastWriteTime(IN LPCTSTR lpDirName, OUT LPTSTR lpszString, IN CONST DWORD& dwSize)
  677. {
  678. if ( lpDirName == NULL || !PathFileExists(lpDirName))
  679. {
  680. OutputDebugString(_T("文件夹不存在\n"));
  681. return FALSE;
  682. }
  683. // 打开文件夹;
  684. HANDLE hDir = CreateFile(
  685. lpDirName,
  686. GENERIC_READ,
  687. FILE_SHARE_READ|FILE_SHARE_DELETE,
  688. NULL, OPEN_EXISTING,
  689. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  690. if ( hDir == INVALID_HANDLE_VALUE )
  691. {
  692. OutputDebugString(_T("打开文件夹失败\n"));
  693. return FALSE;
  694. }
  695. if ( !GetLastWriteTime(hDir, lpszString, dwSize) )
  696. {
  697. OutputDebugString(_T("获取文件修改时间失败\n"));
  698. return FALSE;
  699. }
  700. // 关闭文件句柄;
  701. CloseHandle(hDir);
  702. return TRUE;
  703. }
  704. BOOL GetLastWriteTime(IN HANDLE hFile, OUT LPTSTR lpszString, IN CONST DWORD& dwSize)
  705. {
  706. // 创建时间、访问时间、修改时间;
  707. FILETIME ftCreate, ftAccess, ftWrite;
  708. // UTC时间、系统时间;
  709. SYSTEMTIME stUTC, stLocal;
  710. // 返回的结果;
  711. DWORD dwRet = 0;
  712. // Retrieve the file times for the file.
  713. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  714. return FALSE;
  715. // Convert the last-write time to local time.
  716. FileTimeToSystemTime(&ftWrite, &stUTC);
  717. SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
  718. // Build a string showing the date and time.
  719. dwRet = StringCchPrintf(lpszString, dwSize,
  720. TEXT("%02d/%02d/%d %02d:%02d"),
  721. stLocal.wMonth, stLocal.wDay, stLocal.wYear,
  722. stLocal.wHour, stLocal.wMinute);
  723. return (S_OK == dwRet ? TRUE : FALSE);
  724. }
  725. BOOL SetFileLastWirteTime(IN LPCTSTR lpDirName, IN SYSTEMTIME &st)
  726. {
  727. if ( lpDirName == NULL || !PathFileExists(lpDirName))
  728. {
  729. OutputDebugString(_T("文件夹不存在\n"));
  730. return FALSE;
  731. }
  732. // 打开文件夹;
  733. HANDLE hDir = CreateFile(
  734. lpDirName,
  735. GENERIC_WRITE,
  736. FILE_SHARE_READ|FILE_SHARE_DELETE,
  737. NULL, OPEN_EXISTING,
  738. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  739. if ( hDir == INVALID_HANDLE_VALUE )
  740. {
  741. OutputDebugString(_T("打开文件夹失败\n"));
  742. return FALSE;
  743. }
  744. FILETIME ft;
  745. BOOL f;
  746. SystemTimeToFileTime(&st, &ft); // converts to file time format
  747. // sets last-write time for file
  748. f = SetFileTime(hDir, (LPFILETIME) NULL, (LPFILETIME) NULL, &ft);
  749. CloseHandle(hDir);
  750. return f;
  751. }
  752. void DeleteDirectory(CString strDir)
  753. {
  754. if ( strDir.IsEmpty() || !PathFileExists(strDir) )
  755. {
  756. OutputDebugString(_T("DeleteDirectory:路径无效!"));
  757. return;
  758. }
  759. try
  760. {
  761. // 首先删除文件及子文件夹;
  762. CFileFind ff;
  763. BOOL bFound = ff.FindFile(strDir + _T("\\*"), 0);
  764. while (bFound)
  765. {
  766. bFound = ff.FindNextFile();
  767. if (ff.GetFileName() == _T(".") || ff.GetFileName() == _T(".."))
  768. continue;
  769. // 去掉文件(夹)只读等属性;
  770. if ( SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL) )
  771. {
  772. if (ff.IsDirectory())
  773. { // 递归删除子文件夹;
  774. DeleteDirectory(ff.GetFilePath());
  775. RemoveDirectory(ff.GetFilePath());
  776. }
  777. else
  778. {
  779. DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
  780. OutputDebugString(ff.GetFilePath());
  781. }
  782. }
  783. else
  784. {
  785. DWORD dwError = GetLastError();
  786. CString strError = _T("");
  787. strError.Format(_T("设置文件[%s]属性失败:%d"), ff.GetFilePath(), dwError);
  788. }
  789. }
  790. ff.Close();
  791. // 然后删除该文件夹;
  792. RemoveDirectory(strDir);
  793. }
  794. catch (...)
  795. {
  796. OutputDebugString(_T("出错在函数deldir中"));
  797. }
  798. }
  799. bool MKDIR(LPCTSTR dir)
  800. {
  801. //////////////////////////////////////////////////////////////////////////
  802. // 创建目录;
  803. int nleft = 0;
  804. int nIndex = -1;
  805. string strdir = dir;
  806. strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
  807. if (strdir.at(strdir.size() - 1) != _T('\\'))
  808. strdir.append(_T("\\"));
  809. // 共享路径和硬盘盘符;
  810. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  811. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  812. else if (strdir.at(2) == _T('\\'))
  813. nleft = 3;
  814. do
  815. {
  816. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  817. if (nIndex != string::npos)
  818. {
  819. if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  820. {
  821. //WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  822. return false;
  823. }
  824. nleft += nIndex + 1;
  825. }
  826. } while (nIndex != -1);
  827. return true;
  828. };
  829. /************************************************************************/
  830. /* 函数:WriteTextLog[7/28/2009 Jeff];
  831. /* 描述:写文本日志;
  832. /* 参数:;
  833. /* [IN] :;
  834. /* 返回:void;
  835. /* 注意:;
  836. /* 示例:;
  837. /*
  838. /* 修改:;
  839. /* 日期:;
  840. /* 内容:;
  841. /************************************************************************/
  842. void WriteTextLog(const TCHAR* format, ...)
  843. {
  844. AutoThreadSection aSection(&g_csTextLog);
  845. // 获取今年年份;
  846. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  847. struct tm gmtm = { 0 };
  848. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  849. // 解析出日志路径;
  850. TCHAR szlogpath[MAX_PATH] = { 0 };
  851. //_stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
  852. _stprintf_s(szlogpath, _T("%s%s\\SATHTTP-%04d-%02d.txt"), g_szCurModuleDir, "log", gmtm.tm_year + 1900, gmtm.tm_mon + 1);
  853. // 打开或创建文件;
  854. FILE* fp = NULL;
  855. if (_taccess(szlogpath, 0) != -1)
  856. {// 存在;
  857. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  858. // 移动到末尾;
  859. fseek(fp, 0, SEEK_END);
  860. }
  861. else
  862. {// 不存在;
  863. _tfopen_s(&fp, szlogpath, _T("w+"));
  864. }
  865. if (fp == NULL)
  866. return;
  867. // 格式化前设置语言区域;
  868. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  869. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  870. // 格式化日志内容;
  871. va_list args = NULL;
  872. int len = 0;
  873. TCHAR* buffer = NULL;
  874. va_start(args, format);
  875. // _vscprintf doesn't count. terminating '\0'
  876. len = _vsctprintf(format, args) + 1;
  877. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  878. _vstprintf_s(buffer, len, format, args);
  879. // 将日志内容输入到文件中;
  880. _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1900, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
  881. // 关闭文件,释放资源并设置回原语言区域;
  882. free(buffer);
  883. fclose(fp);
  884. _tsetlocale(LC_CTYPE, old_locale);
  885. free(old_locale);//还原区域设定;
  886. }
  887. void WritePythonLog(LPCTSTR file, const TCHAR* msg)
  888. {
  889. AutoThreadSection aSection(&g_critSection);
  890. // 将日志内容输入到文件中;
  891. // 获取今年年份;
  892. //__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  893. //struct tm gmtm = { 0 };
  894. //localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  895. int nErr = -1;
  896. // 打开或创建文件;
  897. FILE* fp = NULL;
  898. #ifndef UNICODE
  899. if (_access(file, 0) != -1)
  900. #else
  901. if (_taccess(file, 0) != -1)
  902. #endif
  903. { // 存在;
  904. if (0 == _tfopen_s(&fp, file, _T("a+")))
  905. {
  906. // 移动到末尾;
  907. fseek(fp, 0, SEEK_END);
  908. }
  909. else
  910. {
  911. //WriteTextLog(_T("打开文件失败:%s,%d"), file, GetLastError());
  912. return;
  913. }
  914. }
  915. else
  916. { // 不存在;
  917. MKDIR(file);
  918. if (0 != _tfopen_s(&fp, file, _T("w+")) )
  919. {
  920. //WriteTextLog(_T("创建文件失败:%s,%d"), file, GetLastError());
  921. return;
  922. }
  923. }
  924. if (fp == NULL)
  925. {
  926. //WriteTextLog(_T("文件句柄空:%s,%d"), file, GetLastError());
  927. return;
  928. }
  929. // 格式化前设置语言区域;
  930. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  931. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  932. //_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, msg);
  933. _ftprintf(fp, _T("%s"), msg);
  934. // 关闭文件,释放资源并设置回原语言区域;
  935. fclose(fp);
  936. _tsetlocale(LC_CTYPE, old_locale);
  937. free(old_locale); //还原区域设定;
  938. }
  939. std::string Replace(std::string strSrc, std::string strNew, std::string strOld)
  940. {
  941. while(true) {
  942. std::string::size_type pos(0);
  943. if(( pos = strSrc.find(strOld)) != std::string::npos )
  944. strSrc.replace(pos,strOld.length(),strNew);
  945. else
  946. break;
  947. }
  948. return strSrc;
  949. }
  950. BOOL Python27Dir()
  951. {
  952. HKEY hKey;
  953. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
  954. if (ret != ERROR_SUCCESS)
  955. return FALSE;
  956. //读取KEY
  957. DWORD dwType = REG_SZ; //数据类型
  958. DWORD cbData = MAX_PATH;
  959. ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
  960. if (ret != ERROR_SUCCESS)
  961. {
  962. RegCloseKey(hKey);
  963. return FALSE;
  964. }
  965. RegCloseKey(hKey);
  966. // 给尾加杠;
  967. if ( g_szPython27Dir[_tcslen(g_szPython27Dir) - 1] != '\\')
  968. _tcscat_s(g_szPython27Dir, MAX_PATH, "\\");
  969. return TRUE;
  970. }
  971. void Split(std::string str1, std::string str2, std::vector<std::string> &vt)
  972. {
  973. if ( str1.size() == 0 || str2.size() == 0 )
  974. return;
  975. int npos = str1.find(str2);
  976. while( std::string::npos != npos )
  977. {
  978. vt.push_back(str1.substr(0, npos));
  979. str1 = str1.substr(npos + str2.size());
  980. npos = str1.find(str2);
  981. }
  982. if ( str1.size() )
  983. vt.push_back(str1);
  984. }
  985. };