Global.cpp 28 KB

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