Global.cpp 28 KB

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