Global.cpp 29 KB

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