Global.cpp 29 KB

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