Global.cpp 26 KB

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