Global.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. TCHAR szlogpath[MAX_PATH] = { 0 };
  847. _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
  848. // 打开或创建文件;
  849. FILE* fp = NULL;
  850. if (_taccess(szlogpath, 0) != -1)
  851. {// 存在;
  852. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  853. // 移动到末尾;
  854. fseek(fp, 0, SEEK_END);
  855. }
  856. else
  857. {// 不存在;
  858. _tfopen_s(&fp, szlogpath, _T("w+"));
  859. }
  860. if (fp == NULL)
  861. return;
  862. // 格式化前设置语言区域;
  863. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  864. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  865. // 格式化日志内容;
  866. va_list args = NULL;
  867. int len = 0;
  868. TCHAR* buffer = NULL;
  869. va_start(args, format);
  870. // _vscprintf doesn't count. terminating '\0'
  871. len = _vsctprintf(format, args) + 1;
  872. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  873. _vstprintf_s(buffer, len, format, args);
  874. // 将日志内容输入到文件中;
  875. // 获取今年年份;
  876. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  877. struct tm gmtm = { 0 };
  878. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  879. _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);
  880. // 关闭文件,释放资源并设置回原语言区域;
  881. free(buffer);
  882. fclose(fp);
  883. _tsetlocale(LC_CTYPE, old_locale);
  884. free(old_locale);//还原区域设定;
  885. }
  886. void WritePythonLog(LPCTSTR file, const TCHAR* msg)
  887. {
  888. AutoThreadSection aSection(&g_critSection);
  889. // 将日志内容输入到文件中;
  890. // 获取今年年份;
  891. //__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  892. //struct tm gmtm = { 0 };
  893. //localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  894. int nErr = -1;
  895. // 打开或创建文件;
  896. FILE* fp = NULL;
  897. #ifndef UNICODE
  898. if (_access(file, 0) != -1)
  899. #else
  900. if (_taccess(file, 0) != -1)
  901. #endif
  902. { // 存在;
  903. if (0 == _tfopen_s(&fp, file, _T("a+")))
  904. {
  905. // 移动到末尾;
  906. fseek(fp, 0, SEEK_END);
  907. }
  908. else
  909. {
  910. //WriteTextLog(_T("打开文件失败:%s,%d"), file, GetLastError());
  911. return;
  912. }
  913. }
  914. else
  915. { // 不存在;
  916. MKDIR(file);
  917. if (0 != _tfopen_s(&fp, file, _T("w+")) )
  918. {
  919. //WriteTextLog(_T("创建文件失败:%s,%d"), file, GetLastError());
  920. return;
  921. }
  922. }
  923. if (fp == NULL)
  924. {
  925. //WriteTextLog(_T("文件句柄空:%s,%d"), file, GetLastError());
  926. return;
  927. }
  928. // 格式化前设置语言区域;
  929. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  930. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  931. //_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);
  932. _ftprintf(fp, _T("%s"), msg);
  933. // 关闭文件,释放资源并设置回原语言区域;
  934. fclose(fp);
  935. _tsetlocale(LC_CTYPE, old_locale);
  936. free(old_locale); //还原区域设定;
  937. }
  938. std::string Replace(std::string strSrc, std::string strNew, std::string strOld)
  939. {
  940. while(true) {
  941. std::string::size_type pos(0);
  942. if(( pos = strSrc.find(strOld)) != std::string::npos )
  943. strSrc.replace(pos,strOld.length(),strNew);
  944. else
  945. break;
  946. }
  947. return strSrc;
  948. }
  949. BOOL Python27Dir()
  950. {
  951. HKEY hKey;
  952. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
  953. if (ret != ERROR_SUCCESS)
  954. return FALSE;
  955. //读取KEY
  956. DWORD dwType = REG_SZ; //数据类型
  957. DWORD cbData = MAX_PATH;
  958. ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
  959. if (ret != ERROR_SUCCESS)
  960. {
  961. RegCloseKey(hKey);
  962. return FALSE;
  963. }
  964. RegCloseKey(hKey);
  965. // 给尾加杠;
  966. if ( g_szPython27Dir[_tcslen(g_szPython27Dir) - 1] != '\\')
  967. _tcscat_s(g_szPython27Dir, MAX_PATH, "\\");
  968. return TRUE;
  969. }
  970. void Split(std::string str1, std::string str2, std::vector<std::string> &vt)
  971. {
  972. if ( str1.size() == 0 || str2.size() == 0 )
  973. return;
  974. int npos = str1.find(str2);
  975. while( std::string::npos != npos )
  976. {
  977. vt.push_back(str1.substr(0, npos));
  978. str1 = str1.substr(npos + str2.size());
  979. npos = str1.find(str2);
  980. }
  981. if ( str1.size() )
  982. vt.push_back(str1);
  983. }
  984. };