Global.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "Global.h"
  4. #include <io.h>
  5. #include <direct.h>
  6. #include <tlhelp32.h>
  7. #include <shlwapi.h>
  8. #include <strsafe.h>
  9. #include <WinVer.h>
  10. #pragma comment(lib,"version.lib")
  11. #include "CritSection.h"
  12. // 获取本机ip地址;
  13. #include <Iphlpapi.h>
  14. #pragma comment(lib,"Iphlpapi.lib") //需要添加Iphlpapi.lib库
  15. // 获取网卡地址;
  16. #include <SetupAPI.h>
  17. #include <InitGuid.h>
  18. #include <WinIoCtl.h>
  19. #pragma comment(lib, "SetupAPI.lib")
  20. namespace GLOBAL
  21. {
  22. PyNotice g_PyNotify;
  23. STSATConfig g_stSATConfig;
  24. //////////////////////////////////////////////////////////////////////////
  25. // 全局变量;
  26. TCHAR g_szIniFile[MAX_PATH] = {0};
  27. TCHAR g_szHostName[MAX_PATH] = {0}; // 主机计算机名;
  28. TCHAR g_szConnectString[MAX_PATH]; // DB连接串;
  29. TCHAR g_szCurModuleFileName[MAX_PATH] = { 0 }; // 软件名称;
  30. TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
  31. TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
  32. TCHAR g_szFna[_MAX_FNAME] = { 0 };
  33. TCHAR g_szExt[_MAX_EXT] = { 0 };
  34. // 服务器信息;
  35. TCHAR g_szDBSource[MAX_PATH]; // 数据库源(服务所在IP或计算机名);
  36. TCHAR g_szDBSourcePort[MAX_PATH]; // 数据库源端口;
  37. DWORD g_dwDBServerPort; // 数据库源端口;
  38. TCHAR g_szDBAccount[MAX_PATH]; // 数据库登录用户;
  39. TCHAR g_szDBPassWord[MAX_PATH]; // 数据库登录密码;
  40. TCHAR g_szDBName[MAX_PATH]; // 数据库名称;
  41. TCHAR g_szPython27Dir[MAX_PATH] = {0};
  42. std::string g_strIPAddress;
  43. //////////////////////////////////////////////////////////////////////////
  44. std::string g_strMacs;
  45. std::vector<MacAddress> g_vtMac;
  46. // usb;
  47. static GUID UsbClassGuid = { 0xA5DCBF10L, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED} };
  48. // mac;
  49. static GUID MacClassGuid = { 0xAD498944, 0x762F, 0x11D0, {0x8D, 0xCB, 0x00, 0xC0, 0x4F, 0xC3, 0x35, 0x8C} };
  50. // hdd;
  51. static GUID HDDClassGuid = { 0x53F56307, 0xB6BF, 0x11D0, {0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B} };
  52. //////////////////////////////////////////////////////////////////////////
  53. ThreadSection g_critSection;
  54. ThreadSection g_csTextLog;
  55. //////////////////////////////////////////////////////////////////////////
  56. static const int BEGIN_YEAR = 1901;
  57. static const int NUMBER_YEAR = 199;
  58. static const unsigned int LUNAR_YEARS[199] =
  59. {
  60. 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, //1901-1910
  61. 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, //1911-1920
  62. 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, //1921-1930
  63. 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, //1931-1940
  64. 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, //1941-1950
  65. 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x7AD53C, //1951-1960
  66. 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, //1961-1970
  67. 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, //1971-1980
  68. 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, //1981-1990
  69. 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, //1991-2000
  70. 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, //2001-2010
  71. 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, //2011-2020
  72. 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, //2021-2030
  73. 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, //2031-2040
  74. 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, //2041-2050
  75. 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, //2051-2060
  76. 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, //2061-2070
  77. 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, //2071-2080
  78. 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, //2081-2090
  79. 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 //2091-2099
  80. };
  81. //////////////////////////////////////////////////////////////////////////
  82. BOOL IsValidDate(IN LPCTSTR lpDate) // 判断公历;
  83. {
  84. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  85. return FALSE;
  86. CString strDate = lpDate;
  87. if (strDate.IsEmpty()) return FALSE;
  88. // 判断日期合法性;
  89. static int nYear = 0;
  90. static int nMonth = 0;
  91. static int nDay = 0;
  92. nYear = nMonth = nDay = 0;
  93. nYear = _ttoi(strDate.Left(4));
  94. nMonth = _ttoi(strDate.Mid(5, 2));
  95. nDay = _ttoi(strDate.Mid(8, 2));
  96. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  97. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  98. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  99. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  100. {
  101. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  102. }
  103. else
  104. {
  105. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  106. }
  107. return TRUE;
  108. }
  109. BOOL IsValidLunanrDate(IN LPCTSTR lpDate) // 判断农历;
  110. {
  111. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  112. return FALSE;
  113. CString strDate = lpDate;
  114. if (strDate.IsEmpty()) return FALSE;
  115. static int lunar_year, lunar_month, lunar_day;
  116. lunar_year = lunar_month = lunar_day = 0;
  117. lunar_year = _ttoi(strDate.Left(4));
  118. lunar_month = _ttoi(strDate.Mid(5, 2));
  119. lunar_day = _ttoi(strDate.Mid(8, 2));
  120. //越界检查,如果越界,返回无效日期;
  121. if (lunar_year <= BEGIN_YEAR || lunar_year > BEGIN_YEAR + NUMBER_YEAR - 1)
  122. return FALSE;
  123. if (lunar_month < 1 || lunar_month > 12)
  124. return FALSE;
  125. // 1.农历年是否有闰月;
  126. int nYearIndex = lunar_year - BEGIN_YEAR;
  127. int leap_month = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF;
  128. // 2.月份大小月天数;
  129. int month_day = 0;
  130. if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - lunar_month)) & 0x1)
  131. month_day = 30;
  132. else
  133. month_day = 29;
  134. if (lunar_day < 0 || lunar_day > month_day)
  135. return FALSE;
  136. return TRUE;
  137. }
  138. int GetIniInfo(IN const TCHAR *szPath , const TCHAR *szIniName )
  139. {
  140. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  141. TCHAR szDir[_MAX_DIR] = { 0 };
  142. TCHAR szFna[_MAX_FNAME] = { 0 };
  143. TCHAR szExt[_MAX_EXT] = { 0 };
  144. DWORD dwRet = ::GetModuleFileName(NULL, g_szCurModuleFileName, sizeof(g_szCurModuleFileName) / sizeof(TCHAR));
  145. _tsplitpath_s(g_szCurModuleFileName, szDrive, szDir, g_szFna, g_szExt);
  146. StringCchCat(g_szCurModuleDir, MAX_PATH, szDrive);
  147. StringCchCat(g_szCurModuleDir, MAX_PATH, szDir);
  148. // -----------------------------------------------------//
  149. TCHAR szIniPath[MAX_PATH] = {0};
  150. if (szPath != NULL && szIniName != NULL)
  151. StringCchPrintf(szIniPath, MAX_PATH, _T("%s%s"), szPath, szIniName);
  152. else
  153. StringCchPrintf(szIniPath, MAX_PATH, _T("%sSATService.ini"), g_szCurModuleDir);
  154. HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  155. if (ERROR_FILE_NOT_FOUND == GetLastError())
  156. {
  157. return -1;
  158. }
  159. // 获取服务器端信息;
  160. GetPrivateProfileString(_T("SATService"), _T("ScriptDir"), _T(""), g_stSATConfig.szScriptDir, MAX_PATH, szIniPath);
  161. if ( g_stSATConfig.szScriptDir[_tcslen(g_stSATConfig.szScriptDir)-1] != '\\')
  162. {
  163. _tcscat_s(g_stSATConfig.szScriptDir, MAX_PATH, "\\");
  164. WritePrivateProfileString(_T("SATService"), _T("ScriptDir"), g_stSATConfig.szScriptDir, szIniPath);
  165. }
  166. g_stSATConfig.dwAdbTimeout = GetPrivateProfileInt(_T("SATService"), _T("AdbTimeout"), 120000, szIniPath);
  167. g_stSATConfig.dwTCPSvrPort = GetPrivateProfileInt(_T("SATService"), _T("TCPSvrPort"), 5588, szIniPath);
  168. g_stSATConfig.dwScriptTimeout = GetPrivateProfileInt(_T("SATService"), _T("ScriptTimeout"), 300000, szIniPath);
  169. GetPrivateProfileString(_T("SATService"), _T("ExecuteServer"), _T(""), g_stSATConfig.szExecuteServer, MAX_PATH, szIniPath);
  170. GetPrivateProfileString(_T("SATService"), _T("ExecuteSyscode"), _T(""), g_stSATConfig.szExecuteSysCode, MAX_PATH, szIniPath);
  171. GetPrivateProfileString(_T("SATService"), _T("ApkServer"), _T(""), g_stSATConfig.szApkServer, MAX_PATH, szIniPath);
  172. GetPrivateProfileString(_T("SATService"), _T("ApkSyscode"), _T(""), g_stSATConfig.szApkSysCode, MAX_PATH, szIniPath);
  173. GetPrivateProfileString(_T("SATService"), _T("CaseServer"), _T(""), g_stSATConfig.szCaseServer, MAX_PATH, szIniPath);
  174. GetPrivateProfileString(_T("SATService"), _T("CaseSyscode"), _T(""), g_stSATConfig.szCaseSysCode, MAX_PATH, szIniPath);
  175. GetPrivateProfileString(_T("SATService"), _T("TaskServer"), _T(""), g_stSATConfig.szTaskServer, MAX_PATH, szIniPath);
  176. GetPrivateProfileString(_T("SATService"), _T("TaskSyscode"), _T(""), g_stSATConfig.szTaskSysCode, MAX_PATH, szIniPath);
  177. GetPrivateProfileString(_T("SATService"), _T("ResultServer"), _T(""), g_stSATConfig.szResultServer, MAX_PATH, szIniPath);
  178. GetPrivateProfileString(_T("SATService"), _T("ResultExcelDir"), _T(""), g_stSATConfig.szResultExcelDir, MAX_PATH, szIniPath);
  179. GetPrivateProfileString(_T("LinuxDevice"), _T("device"), _T(""), g_stSATConfig.stLinuxDevice.szDevice, MAX_PATH, szIniPath);
  180. GetPrivateProfileString(_T("LinuxDevice"), _T("manu"), _T(""), g_stSATConfig.stLinuxDevice.szManu, MAX_PATH, szIniPath);
  181. GetPrivateProfileString(_T("LinuxDevice"), _T("hardwareversion"), _T(""), g_stSATConfig.stLinuxDevice.szHardwareVersion, MAX_PATH, szIniPath);
  182. GetPrivateProfileString(_T("LinuxDevice"), _T("model"), _T(""), g_stSATConfig.stLinuxDevice.szModel, MAX_PATH, szIniPath);
  183. // 读取其他目录的配置文件;sat_result_dir
  184. _stprintf_s(szIniPath, _T("%s%s"), g_szPython27Dir, "Lib\\site-packages\\ssat_sdk\\config\\resource_run.cfg");
  185. GetPrivateProfileString(_T("COMM"), _T("serial_communicator_port"), _T(""), g_stSATConfig.szTVPort, MAX_PATH, szIniPath);
  186. GetPrivateProfileString(_T("Abnormal"), _T("reboot"), NULL, g_stSATConfig.szTVReboot, MAX_PATH, szIniPath);
  187. GetPrivateProfileString(_T("Abnormal"), _T("shutdown"), NULL, g_stSATConfig.szTVShutdown, MAX_PATH, szIniPath);
  188. g_stSATConfig.bWatchTVPort = GetPrivateProfileInt(_T("Abnormal"), _T("Watch"), 0, szIniPath);
  189. GetPrivateProfileString(_T("COMM"), _T("sat_result_dir"), _T(""), g_stSATConfig.szCaseResultDir, MAX_PATH, szIniPath);
  190. // 将反斜转正斜;
  191. std::string dir = Replace(g_stSATConfig.szCaseResultDir, "\\", "/");
  192. _stprintf_s(g_stSATConfig.szCaseResultDir, dir.c_str());
  193. if (g_stSATConfig.szCaseResultDir[_tcslen(g_stSATConfig.szCaseResultDir) - 1] != '\\')
  194. _tcscat_s(g_stSATConfig.szCaseResultDir, MAX_PATH, "\\");
  195. // 将新的SATSevice的tcp端口号复制到resource_run中;
  196. TCHAR szValue[10] = { 0 };
  197. _itoa_s(g_stSATConfig.dwTCPSvrPort, szValue, 10);
  198. // 作用:python脚本不需要去读取SATService的配置文件;
  199. WritePrivateProfileString(_T("COMM"), _T("runner_tcp_port"), szValue, szIniPath);
  200. return 0;
  201. }
  202. // 判断指定字符串是否在数组里(区分大小写);
  203. BOOL IsStringExist(IN CString& 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 (str == tagAry.ElementAt(i))
  211. {
  212. bExist = TRUE;
  213. break;
  214. }
  215. }
  216. return bExist;
  217. }
  218. // 判断指定字符串是否在数组里(区分大小写);
  219. BOOL IsStringExist(IN TString& str, IN vector<TString> &tagVt)
  220. {
  221. int nSize = tagVt.size();
  222. if (nSize == 0) return FALSE;
  223. BOOL bExist = FALSE;
  224. for (vector<TString>::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  225. {
  226. if (str.compare(it->c_str()) == 0)
  227. {
  228. bExist = TRUE;
  229. break;
  230. }
  231. }
  232. return bExist;
  233. }
  234. // 判断指定字符串是否在数组里(不区分大小写);
  235. BOOL IsStringExistNoCase(IN CString& str, IN CStringArray &tagAry)
  236. {
  237. int nSize = tagAry.GetSize();
  238. if (nSize == 0) return FALSE;
  239. BOOL bExist = FALSE;
  240. for (int i = 0; i < nSize; i++)
  241. {
  242. if (str.CompareNoCase(tagAry.ElementAt(i)) == 0)
  243. {
  244. bExist = TRUE;
  245. break;
  246. }
  247. }
  248. return bExist;
  249. }
  250. BOOL IsStringExistNoCase(IN CONST TString& str, IN CStringArray &tagAry)
  251. {
  252. int nSize = tagAry.GetSize();
  253. if (nSize == 0) return FALSE;
  254. BOOL bExist = FALSE;
  255. for (int i = 0; i < nSize; i++)
  256. {
  257. if ((tagAry.ElementAt(i).CompareNoCase(str.c_str())) == 0)
  258. {
  259. bExist = TRUE;
  260. break;
  261. }
  262. }
  263. return bExist;
  264. }
  265. // 判断指定字符串是否在数组里(不区分大小写);
  266. BOOL IsStringExistNoCase(IN CONST TString& str, IN vector<TString> &tagVt)
  267. {
  268. int nSize = tagVt.size();
  269. if (nSize == 0) return FALSE;
  270. BOOL bExist = FALSE;
  271. TString stmp1 = uppercase(str);
  272. TString stmp2;
  273. for (vector<TString>::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  274. {
  275. stmp2 = uppercase(*it);
  276. if (stmp1.compare(stmp2) == 0)
  277. {
  278. bExist = TRUE;
  279. break;
  280. }
  281. }
  282. return bExist;
  283. }
  284. TString uppercase(IN const TString &Str)
  285. {
  286. const TCHAR aazz = _T('z') - _T('Z');
  287. TString sResult;
  288. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  289. {
  290. if (_T('a') <= *it && *it <= _T('z'))
  291. sResult.append(1, *it - aazz);
  292. else
  293. sResult.append(1, *it);
  294. }
  295. return sResult;
  296. }
  297. int match(IN CONST TString &sFileExt, IN CONST TString &sFile)
  298. {
  299. TString fext = uppercase(sFileExt);
  300. TString file = uppercase(sFile);
  301. int pos = file.find_last_of(_T('.'));
  302. if (TString::npos != pos)
  303. file = file.substr(pos);
  304. return (TString::npos != fext.find(file));
  305. }
  306. TString getfilename(IN CONST TString &file)
  307. {
  308. TString name;
  309. TString strfile = file;
  310. int nIndex = strfile.find_last_of(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  311. if (nIndex == TString::npos)
  312. {
  313. nIndex = strfile.find_last_of(_T('.'));
  314. if ( nIndex == TString::npos )
  315. return _T("");
  316. return strfile.substr(0, nIndex);
  317. }
  318. name = strfile.substr(nIndex+1);
  319. nIndex = name.find_last_of(_T('.'));
  320. if (nIndex == TString::npos)
  321. return _T("");
  322. return name.substr(0, nIndex);
  323. }
  324. // 获取文件名;
  325. CString getfilename(IN CONST CString &file)
  326. {
  327. CString name;
  328. CString strfile = file;
  329. int nIndex = strfile.ReverseFind(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  330. if (nIndex == -1)
  331. {
  332. nIndex = strfile.ReverseFind(_T('.'));
  333. if (nIndex == -1)
  334. return _T("");
  335. return strfile.Left(nIndex);
  336. }
  337. name = strfile.Mid(nIndex+1);
  338. nIndex = name.Find(_T('.'));
  339. if (nIndex == -1)
  340. return _T("");
  341. return name.Left(nIndex);
  342. }
  343. // 通过路径名查找进程,返回进程ID;
  344. BOOL FindAppProcessID(IN CString path, OUT DWORD &dwProcessID)
  345. {
  346. int nIndex = path.ReverseFind('\\');
  347. path = path.Right(path.GetLength() - nIndex - 1);
  348. dwProcessID = FindProcess(path);
  349. return (dwProcessID != 0 ? TRUE : FALSE);
  350. BOOL bFound = FALSE;
  351. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  352. if ( handle == NULL )
  353. return FALSE;
  354. PROCESSENTRY32 Info;
  355. Info.dwSize = sizeof(PROCESSENTRY32);
  356. path.MakeLower();
  357. if (::Process32First(handle, &Info))
  358. {
  359. do{
  360. CString ss = Info.szExeFile;
  361. ss.MakeLower();
  362. if (ss == path)
  363. {
  364. dwProcessID = Info.th32ProcessID;
  365. bFound = TRUE;
  366. break;
  367. }
  368. } while (::Process32Next(handle, &Info));
  369. }
  370. CloseHandle(handle);
  371. return (bFound ? TRUE : FALSE);
  372. }
  373. // 根据路径名查找进程,返回进程ID;
  374. DWORD FindProcess(IN CString strProName)
  375. {
  376. DWORD dwProcessID = 0;
  377. PROCESSENTRY32 pe32 = { 0 };
  378. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  379. if (hProcessSnap == NULL)
  380. {
  381. return 0;
  382. }
  383. pe32.dwSize = sizeof(PROCESSENTRY32);
  384. if (Process32First(hProcessSnap, &pe32))
  385. {
  386. do
  387. {
  388. if (_tcscmp(strProName.GetString(), pe32.szExeFile) == 0)
  389. {
  390. dwProcessID = pe32.th32ProcessID;
  391. break;
  392. }
  393. } while (Process32Next(hProcessSnap, &pe32));
  394. }
  395. CloseHandle(hProcessSnap);
  396. return dwProcessID;
  397. }
  398. DWORD FindProcessCount(IN LPCTSTR lpExeName)
  399. {
  400. DWORD dwCount = 0;
  401. if ( lpExeName && lpExeName[0] != _T('\0'))
  402. {
  403. PROCESSENTRY32 pe32 = { 0 };
  404. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  405. if (hProcessSnap == NULL) return 0;
  406. pe32.dwSize = sizeof(PROCESSENTRY32);
  407. if ( !Process32First(hProcessSnap, &pe32) )
  408. return 0;
  409. do
  410. {
  411. if (_tcscmp(lpExeName, pe32.szExeFile) == 0)
  412. dwCount++;
  413. } while (Process32Next(hProcessSnap, &pe32));
  414. CloseHandle(hProcessSnap);
  415. }
  416. return dwCount;
  417. }
  418. // 根据路径名查找进程,查找成功并结束;
  419. int FindAndCloseProcess(IN CString strProName)
  420. {
  421. int nIndex = strProName.ReverseFind('\\');
  422. if (nIndex != -1) // 如果传的是全路径;
  423. strProName = strProName.Right(strProName.GetLength() - nIndex - 1);
  424. DWORD dwProcessID = 0;
  425. PROCESSENTRY32 pe32 = { 0 };
  426. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  427. if (hProcessSnap == NULL)
  428. {
  429. return 0;
  430. }
  431. pe32.dwSize = sizeof(PROCESSENTRY32);
  432. if (Process32First(hProcessSnap, &pe32))
  433. {
  434. do
  435. {
  436. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  437. {
  438. dwProcessID = pe32.th32ProcessID;
  439. break;
  440. }
  441. } while (Process32Next(hProcessSnap, &pe32));
  442. }
  443. CloseHandle(hProcessSnap);
  444. if (dwProcessID == 0)
  445. return 0;
  446. HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
  447. if (hProcess == NULL)
  448. return 0;
  449. DWORD dwError;
  450. if (!TerminateProcess(hProcess, 0))
  451. {
  452. dwError = GetLastError();
  453. CloseHandle(hProcess);
  454. return -1;
  455. }
  456. // 等待进程结束响应;
  457. if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE))
  458. {
  459. CloseHandle(hProcess);
  460. AfxMessageBox(_T("结束进程失败"));
  461. return -1;
  462. }
  463. CloseHandle(hProcess);
  464. return 1;
  465. }
  466. /************************************************************************/
  467. /*
  468. 函数:GetDiskSpaceInfo
  469. 描述:获取指定目录或盘符的容量信息;
  470. 参数:
  471. IN: lpRootPathName 根目录名称;
  472. INOUT: dTotalNum 总容量;
  473. INOUT: dFreeNum 可用容量;
  474. 返回:
  475. 注意:
  476. GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G);
  477. GetDiskFreeSpaceEx支持64位(16384P=1024*16384T)
  478. 能获取本机硬盘信息,但路径必须包含":"冒号, 同时还可以获取网络路径的共享硬盘大小;
  479. 调用约定:
  480. */
  481. /************************************************************************/
  482. BOOL GetDiskSpaceInfo(IN LPCTSTR lpRootPathName, IN OUT double &dTotalNum, IN OUT double &dFreeNum)
  483. {
  484. #if 1
  485. // 校验参数有效性;
  486. if (lpRootPathName == NULL || _tcscmp(lpRootPathName, _T("")) == 0)
  487. {
  488. //_tprintf_s(_T("根目录为空!"));
  489. return FALSE;
  490. }
  491. // 如果长度==1;
  492. int nlen = _tcslen(lpRootPathName);
  493. if ((1 == nlen) || (2 == nlen && lpRootPathName[nlen - 1] != _T(':')))
  494. {
  495. //_tprintf_s(_T("根目录无效!\n"));
  496. return FALSE;
  497. }
  498. if (!PathFileExists(lpRootPathName))
  499. {
  500. //_tprintf_s(_T("根目录不存在!\n"));
  501. return FALSE;
  502. }
  503. #else
  504. // 使用IsValipath判断网络路径时较慢;
  505. if (!IsValidpath(lpRootPathName) || !PathFileExists(lpRootPathName)) return FALSE;
  506. #endif
  507. BOOL fResult = FALSE;
  508. DWORD dwSectPerClust; // 每簇的扇区数;
  509. DWORD dwBytesPerSect; // 每个扇区的字节数;
  510. DWORD dwFreeClusters; // 可用簇数;
  511. DWORD dwTotalClusters; // 总簇数;
  512. typedef BOOL(WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
  513. P_GDFSE pGetDiskFreeSpaceEx = NULL;
  514. /*unsigned*/ __int64 i64FreeBytesToCaller;
  515. /*unsigned*/ __int64 i64TotalBytes;
  516. /*unsigned*/ __int64 i64FreeBytes;
  517. // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace;
  518. #ifdef UNICODE
  519. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExW");
  520. #else
  521. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
  522. #endif
  523. if (pGetDiskFreeSpaceEx)
  524. {
  525. fResult = pGetDiskFreeSpaceEx(lpRootPathName,
  526. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  527. (PULARGE_INTEGER)&i64TotalBytes,
  528. (PULARGE_INTEGER)&i64FreeBytes);
  529. if (fResult)
  530. {
  531. // 将单位由byte转为G byte;
  532. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  533. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  534. }
  535. }
  536. else
  537. {
  538. fResult = GetDiskFreeSpace(lpRootPathName,
  539. &dwSectPerClust,
  540. &dwBytesPerSect,
  541. &dwFreeClusters,
  542. &dwTotalClusters);
  543. if (fResult)
  544. {
  545. // 成功获得容量信息;
  546. i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
  547. i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
  548. // 将单位由byte转为G byte;
  549. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  550. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  551. }
  552. }
  553. return fResult;
  554. }
  555. /************************************************************************/
  556. /*
  557. 函数:GetFileVersion
  558. 描述:获取可执行文件的文件版号;
  559. 参数:
  560. hModule[IN] 可执行文件模块句柄;
  561. dwArray[OUT] 返回的文件版本号;
  562. 返回:
  563. 成功返回TRUE,失败返回FALSE;
  564. 注意:
  565. 当hModule为空时,表示要获取的可执行文件为本程序的文件版本号;
  566. */
  567. /************************************************************************/
  568. BOOL GetFileVersion( IN HMODULE hModule, OUT DWORD (&dwArray)[4])
  569. {
  570. TCHAR fname[MAX_PATH];
  571. VS_FIXEDFILEINFO *pVi;
  572. DWORD dwHandle;
  573. if ( GetModuleFileName(hModule, fname, MAX_PATH))
  574. {
  575. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  576. if (nSize > 0)
  577. {
  578. BYTE *pBuffer = new BYTE[nSize];
  579. memset(pBuffer, 0, nSize);
  580. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  581. {
  582. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  583. {
  584. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  585. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  586. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  587. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  588. delete[]pBuffer;
  589. return TRUE;
  590. }
  591. }
  592. if ( pBuffer )
  593. delete[]pBuffer;
  594. }
  595. }
  596. return FALSE;
  597. }
  598. /************************************************************************/
  599. /*
  600. 函数:GetFileVersion
  601. 描述:获取可执行文件的文件版号;
  602. 参数:
  603. lpFileName[IN] 可执行文件名全路径;
  604. dwArray[OUT] 返回的文件版本号;
  605. 返回:
  606. 成功返回TRUE,失败返回FALSE;
  607. 注意:
  608. */
  609. /************************************************************************/
  610. BOOL GetFileVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  611. {
  612. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  613. {
  614. OutputDebugString(_T("文件名错误或文件不存在\n"));
  615. return FALSE;
  616. }
  617. DWORD dwHandle = 0;
  618. VS_FIXEDFILEINFO *pVi = NULL;
  619. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  620. if ( nSize > 0 )
  621. {
  622. BYTE *pBuffer = new BYTE[nSize];
  623. memset(pBuffer, 0, nSize);
  624. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  625. {
  626. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  627. {
  628. dwArray[0] = HIWORD(pVi->dwFileVersionMS);
  629. dwArray[1] = LOWORD(pVi->dwFileVersionMS);
  630. dwArray[2] = HIWORD(pVi->dwFileVersionLS);
  631. dwArray[3] = LOWORD(pVi->dwFileVersionLS);
  632. if (pBuffer)
  633. delete[]pBuffer;
  634. return TRUE;
  635. }
  636. }
  637. if (pBuffer)
  638. delete[]pBuffer;
  639. }
  640. return FALSE;
  641. }
  642. /************************************************************************/
  643. /*
  644. 函数:GetProductVersion
  645. 描述:获取可执行文件的产品版号;
  646. 参数:
  647. hModule[IN] 可执行文件模块句柄;
  648. dwArray[OUT] 返回的产品版本号;
  649. 返回:
  650. 成功返回TRUE,失败返回FALSE;
  651. 注意:
  652. 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号;
  653. */
  654. /************************************************************************/
  655. BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
  656. {
  657. TCHAR fname[MAX_PATH];
  658. VS_FIXEDFILEINFO *pVi;
  659. DWORD dwHandle;
  660. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  661. {
  662. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  663. if (nSize > 0)
  664. {
  665. BYTE *pBuffer = new BYTE[nSize];
  666. memset(pBuffer, 0, nSize);
  667. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  668. {
  669. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  670. {
  671. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  672. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  673. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  674. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  675. if(pBuffer)
  676. delete[]pBuffer;
  677. return TRUE;
  678. }
  679. }
  680. if(pBuffer)
  681. delete[]pBuffer;
  682. }
  683. }
  684. return FALSE;
  685. }
  686. /************************************************************************/
  687. /*
  688. 函数:GetProductVersion
  689. 描述:获取可执行文件的产品版号;
  690. 参数:
  691. lpFileName[IN] 可执行文件名全路径;
  692. dwArray[OUT] 返回的产品版本号;
  693. 返回:
  694. 成功返回TRUE,失败返回FALSE;
  695. 注意:
  696. */
  697. /************************************************************************/
  698. BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  699. {
  700. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  701. {
  702. OutputDebugString(_T("文件名错误或文件不存在\n"));
  703. return FALSE;
  704. }
  705. DWORD dwHandle = 0;
  706. VS_FIXEDFILEINFO *pVi = NULL;
  707. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  708. if ( nSize > 0 )
  709. {
  710. BYTE *pBuffer = new BYTE[nSize];
  711. memset(pBuffer, 0, nSize);
  712. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  713. {
  714. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  715. {
  716. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  717. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  718. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  719. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  720. if (pBuffer)
  721. delete[]pBuffer;
  722. return TRUE;
  723. }
  724. }
  725. if (pBuffer)
  726. delete[]pBuffer;
  727. }
  728. return FALSE;
  729. }
  730. /************************************************************************/
  731. /*
  732. 函数:
  733. 描述:
  734. 参数:
  735. 返回:
  736. 注意:
  737. */
  738. /************************************************************************/
  739. BOOL GetDirLastWriteTime(IN LPCTSTR lpDirName, OUT LPTSTR lpszString, IN CONST DWORD& dwSize)
  740. {
  741. if ( lpDirName == NULL || !PathFileExists(lpDirName))
  742. {
  743. OutputDebugString(_T("文件夹不存在\n"));
  744. return FALSE;
  745. }
  746. // 打开文件夹;
  747. HANDLE hDir = CreateFile(
  748. lpDirName,
  749. GENERIC_READ,
  750. FILE_SHARE_READ|FILE_SHARE_DELETE,
  751. NULL, OPEN_EXISTING,
  752. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  753. if ( hDir == INVALID_HANDLE_VALUE )
  754. {
  755. OutputDebugString(_T("打开文件夹失败\n"));
  756. return FALSE;
  757. }
  758. if ( !GetLastWriteTime(hDir, lpszString, dwSize) )
  759. {
  760. OutputDebugString(_T("获取文件修改时间失败\n"));
  761. return FALSE;
  762. }
  763. // 关闭文件句柄;
  764. CloseHandle(hDir);
  765. return TRUE;
  766. }
  767. BOOL GetLastWriteTime(IN HANDLE hFile, OUT LPTSTR lpszString, IN CONST DWORD& dwSize)
  768. {
  769. // 创建时间、访问时间、修改时间;
  770. FILETIME ftCreate, ftAccess, ftWrite;
  771. // UTC时间、系统时间;
  772. SYSTEMTIME stUTC, stLocal;
  773. // 返回的结果;
  774. DWORD dwRet = 0;
  775. // Retrieve the file times for the file.
  776. if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
  777. return FALSE;
  778. // Convert the last-write time to local time.
  779. FileTimeToSystemTime(&ftWrite, &stUTC);
  780. SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
  781. // Build a string showing the date and time.
  782. dwRet = StringCchPrintf(lpszString, dwSize,
  783. TEXT("%02d/%02d/%d %02d:%02d"),
  784. stLocal.wMonth, stLocal.wDay, stLocal.wYear,
  785. stLocal.wHour, stLocal.wMinute);
  786. return (S_OK == dwRet ? TRUE : FALSE);
  787. }
  788. BOOL SetFileLastWirteTime(IN LPCTSTR lpDirName, IN SYSTEMTIME &st)
  789. {
  790. if ( lpDirName == NULL || !PathFileExists(lpDirName))
  791. {
  792. OutputDebugString(_T("文件夹不存在\n"));
  793. return FALSE;
  794. }
  795. // 打开文件夹;
  796. HANDLE hDir = CreateFile(
  797. lpDirName,
  798. GENERIC_WRITE,
  799. FILE_SHARE_READ|FILE_SHARE_DELETE,
  800. NULL, OPEN_EXISTING,
  801. FILE_FLAG_BACKUP_SEMANTICS, NULL);
  802. if ( hDir == INVALID_HANDLE_VALUE )
  803. {
  804. OutputDebugString(_T("打开文件夹失败\n"));
  805. return FALSE;
  806. }
  807. FILETIME ft;
  808. BOOL f;
  809. SystemTimeToFileTime(&st, &ft); // converts to file time format
  810. // sets last-write time for file
  811. f = SetFileTime(hDir, (LPFILETIME) NULL, (LPFILETIME) NULL, &ft);
  812. CloseHandle(hDir);
  813. return f;
  814. }
  815. void DeleteDirectory(CString strDir)
  816. {
  817. if ( strDir.IsEmpty() || !PathFileExists(strDir) )
  818. {
  819. OutputDebugString(_T("DeleteDirectory:路径无效!"));
  820. return;
  821. }
  822. try
  823. {
  824. // 首先删除文件及子文件夹;
  825. CFileFind ff;
  826. BOOL bFound = ff.FindFile(strDir + _T("\\*"), 0);
  827. while (bFound)
  828. {
  829. bFound = ff.FindNextFile();
  830. if (ff.GetFileName() == _T(".") || ff.GetFileName() == _T(".."))
  831. continue;
  832. // 去掉文件(夹)只读等属性;
  833. if ( SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL) )
  834. {
  835. if (ff.IsDirectory())
  836. { // 递归删除子文件夹;
  837. DeleteDirectory(ff.GetFilePath());
  838. RemoveDirectory(ff.GetFilePath());
  839. }
  840. else
  841. {
  842. DeleteFile(ff.GetFilePath()); // Jeff:删除某文件夹下的所有子文件;
  843. OutputDebugString(ff.GetFilePath());
  844. }
  845. }
  846. else
  847. {
  848. DWORD dwError = GetLastError();
  849. CString strError = _T("");
  850. strError.Format(_T("设置文件[%s]属性失败:%d"), ff.GetFilePath(), dwError);
  851. }
  852. }
  853. ff.Close();
  854. // 然后删除该文件夹;
  855. RemoveDirectory(strDir);
  856. }
  857. catch (...)
  858. {
  859. OutputDebugString(_T("出错在函数deldir中"));
  860. }
  861. }
  862. void GetHostName()
  863. {
  864. WSADATA WSAData = { 0 };
  865. if(0 != WSAStartup(MAKEWORD(2, 2), &WSAData))
  866. {
  867. AfxMessageBox(_T("网络环境初始化失败!"));
  868. return;
  869. }
  870. CHAR szHostName[MAX_PATH] = {0};
  871. gethostname(szHostName,MAX_PATH);
  872. #ifdef UNICODE
  873. INT wSize = MultiByteToWideChar(CP_ACP, 0, szHostName, -1, NULL, 0);
  874. if (wSize == 0) return ;
  875. WCHAR *pHostName = new WCHAR[wSize];
  876. int convresult = MultiByteToWideChar(CP_ACP, 0, szHostName, -1, pHostName, wSize);
  877. if (convresult != wSize)
  878. {
  879. if (pHostName) delete []pHostName;
  880. return;
  881. }
  882. #else
  883. TCHAR *pHostName = szHostName;
  884. #endif
  885. StringCchPrintf(g_szHostName, MAX_PATH, _T("%s"), pHostName);
  886. #ifdef UNICODE
  887. delete []pHostName;
  888. #endif
  889. }
  890. bool MKDIR(LPCTSTR dir)
  891. {
  892. //////////////////////////////////////////////////////////////////////////
  893. // 创建目录;
  894. int nleft = 0;
  895. int nIndex = -1;
  896. string strdir = dir;
  897. strdir = strdir.substr(0, strdir.find_last_of(_T("\\")));
  898. if (strdir.at(strdir.size() - 1) != _T('\\'))
  899. strdir.append(_T("\\"));
  900. // 共享路径和硬盘盘符;
  901. if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
  902. nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
  903. else if (strdir.at(2) == _T('\\'))
  904. nleft = 3;
  905. do
  906. {
  907. nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
  908. if (nIndex != string::npos)
  909. {
  910. if (_mkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
  911. {
  912. //WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
  913. return false;
  914. }
  915. nleft += nIndex + 1;
  916. }
  917. } while (nIndex != -1);
  918. return true;
  919. };
  920. /************************************************************************/
  921. /* 函数:WriteTextLog[7/28/2009 Jeff];
  922. /* 描述:写文本日志;
  923. /* 参数:;
  924. /* [IN] :;
  925. /* 返回:void;
  926. /* 注意:;
  927. /* 示例:;
  928. /*
  929. /* 修改:;
  930. /* 日期:;
  931. /* 内容:;
  932. /************************************************************************/
  933. void WriteTextLog(const TCHAR* format, ...)
  934. {
  935. AutoThreadSection aSection(&g_csTextLog);
  936. // 解析出日志路径;
  937. TCHAR szlogpath[MAX_PATH] = { 0 };
  938. _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
  939. // 打开或创建文件;
  940. FILE* fp = NULL;
  941. if (_taccess(szlogpath, 0) != -1)
  942. {// 存在;
  943. if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
  944. // 移动到末尾;
  945. fseek(fp, 0, SEEK_END);
  946. }
  947. else
  948. {// 不存在;
  949. _tfopen_s(&fp, szlogpath, _T("w+"));
  950. }
  951. if (fp == NULL)
  952. return;
  953. // 格式化前设置语言区域;
  954. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  955. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  956. // 格式化日志内容;
  957. va_list args = NULL;
  958. int len = 0;
  959. TCHAR* buffer = NULL;
  960. va_start(args, format);
  961. // _vscprintf doesn't count. terminating '\0'
  962. len = _vsctprintf(format, args) + 1;
  963. buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
  964. _vstprintf_s(buffer, len, format, args);
  965. // 将日志内容输入到文件中;
  966. // 获取今年年份;
  967. __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
  968. struct tm gmtm = { 0 };
  969. localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  970. _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
  971. // 关闭文件,释放资源并设置回原语言区域;
  972. free(buffer);
  973. fclose(fp);
  974. _tsetlocale(LC_CTYPE, old_locale);
  975. free(old_locale);//还原区域设定;
  976. }
  977. void WritePythonLog(LPCTSTR file, const TCHAR* msg)
  978. {
  979. AutoThreadSection aSection(&g_critSection);
  980. // 将日志内容输入到文件中;
  981. // 获取今年年份;
  982. //__time64_t gmt = time(NULL); // 获取当前日历时间(1900-01-01开始的Unix时间戳);
  983. //struct tm gmtm = { 0 };
  984. //localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
  985. int nErr = -1;
  986. // 打开或创建文件;
  987. FILE* fp = NULL;
  988. #ifndef UNICODE
  989. if (_access(file, 0) != -1)
  990. #else
  991. if (_taccess(file, 0) != -1)
  992. #endif
  993. { // 存在;
  994. if (0 == _tfopen_s(&fp, file, _T("a+")))
  995. {
  996. // 移动到末尾;
  997. fseek(fp, 0, SEEK_END);
  998. }
  999. else
  1000. {
  1001. //WriteTextLog(_T("打开文件失败:%s,%d"), file, GetLastError());
  1002. return;
  1003. }
  1004. }
  1005. else
  1006. { // 不存在;
  1007. MKDIR(file);
  1008. if (0 != _tfopen_s(&fp, file, _T("w+")) )
  1009. {
  1010. //WriteTextLog(_T("创建文件失败:%s,%d"), file, GetLastError());
  1011. return;
  1012. }
  1013. }
  1014. if (fp == NULL)
  1015. {
  1016. //WriteTextLog(_T("文件句柄空:%s,%d"), file, GetLastError());
  1017. return;
  1018. }
  1019. // 格式化前设置语言区域;
  1020. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  1021. _tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
  1022. //_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);
  1023. _ftprintf(fp, _T("%s"), msg);
  1024. // 关闭文件,释放资源并设置回原语言区域;
  1025. fclose(fp);
  1026. _tsetlocale(LC_CTYPE, old_locale);
  1027. free(old_locale); //还原区域设定;
  1028. }
  1029. std::string GetLocalAddress(int nType/*= MIB_IF_TYPE_ETHERNET*/)
  1030. {
  1031. int nCardNo = 1;
  1032. // PIP_ADAPTER_INFO结构体指针存储本机网卡信息
  1033. PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
  1034. // 得到结构体大小,用于GetAdaptersInfo参数
  1035. unsigned long stSize = sizeof(IP_ADAPTER_INFO);
  1036. // 调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量
  1037. int nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
  1038. // 记录网卡数量
  1039. int netCardNum = 0;
  1040. // 记录每张网卡上的IP地址数量
  1041. int IPnumPerNetCard = 0;
  1042. if(ERROR_BUFFER_OVERFLOW == nRel)
  1043. {
  1044. // 如果函数返回的是ERROR_BUFFER_OVERFLOW
  1045. // 则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小
  1046. // 这也是说明为什么stSize既是一个输入量也是一个输出量
  1047. // 释放原来的内存空间
  1048. delete pIpAdapterInfo;
  1049. // 重新申请内存空间用来存储所有网卡信息
  1050. pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
  1051. // 再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
  1052. nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
  1053. }
  1054. if(ERROR_SUCCESS == nRel)
  1055. {
  1056. // 输出网卡信息
  1057. // 可能有多网卡,因此通过循环去判断
  1058. while (pIpAdapterInfo)
  1059. {
  1060. // 可能网卡有多IP,因此通过循环去判断
  1061. IP_ADDR_STRING*pIpAddrString = &(pIpAdapterInfo->IpAddressList);
  1062. if ( pIpAdapterInfo->Type == nType )
  1063. {
  1064. g_strIPAddress = pIpAddrString->IpAddress.String;
  1065. break;
  1066. }
  1067. #if 0
  1068. switch(pIpAdapterInfo->Type)
  1069. {
  1070. case MIB_IF_TYPE_OTHER:
  1071. case MIB_IF_TYPE_ETHERNET:
  1072. case MIB_IF_TYPE_TOKENRING:
  1073. case MIB_IF_TYPE_FDDI:
  1074. case MIB_IF_TYPE_PPP:
  1075. case MIB_IF_TYPE_LOOPBACK:
  1076. case MIB_IF_TYPE_SLIP:
  1077. {
  1078. strAddress = pIpAddrString->IpAddress.String;
  1079. // 需要注意的是有时可能获取的IP地址是0.0.0.0,这时需要过滤掉
  1080. if(std::string("0.0.0.0") == strAddress)
  1081. break;
  1082. std::cout<<_T("第")<<nCardNo<<_T("张网卡的IP地址是")<<strAddress<<std::endl;
  1083. nCardNo++;
  1084. break;
  1085. }
  1086. default:
  1087. // 未知类型网卡就跳出
  1088. break;
  1089. }
  1090. #endif
  1091. pIpAdapterInfo = pIpAdapterInfo->Next;
  1092. }
  1093. }
  1094. // 释放内存空间
  1095. if (pIpAdapterInfo)
  1096. {
  1097. delete pIpAdapterInfo;
  1098. }
  1099. return g_strIPAddress;
  1100. }
  1101. INT GetMacAddress()
  1102. {
  1103. HDEVINFO hDevInfo;
  1104. DWORD MemberIndex, RequiredSize;
  1105. SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
  1106. PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData;
  1107. INT nTotal = 0;
  1108. INT nNICKind = 0;
  1109. // 获取设备信息集;
  1110. hDevInfo = SetupDiGetClassDevs(&MacClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
  1111. if (hDevInfo == INVALID_HANDLE_VALUE)
  1112. {
  1113. return -1;
  1114. }
  1115. g_vtMac.clear();
  1116. // 枚举设备信息集中所有设备;
  1117. DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  1118. for (MemberIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &MacClassGuid, MemberIndex, &DeviceInterfaceData); MemberIndex++)
  1119. {
  1120. // 获取接收缓冲区大小,函数返回值为FALSE,GetLastError()=ERROR_INSUFFICIENT_BUFFER;
  1121. SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, NULL, 0, &RequiredSize, NULL);
  1122. // 申请接收缓冲区;
  1123. DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  1124. if ( DeviceInterfaceDetailData == NULL)
  1125. continue;
  1126. DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  1127. // 获取设备细节信息;
  1128. if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &DeviceInterfaceData, DeviceInterfaceDetailData, RequiredSize, NULL, NULL))
  1129. {
  1130. HANDLE hDeviceFile;
  1131. BOOL isOK = FALSE;
  1132. if (_tcsnicmp(DeviceInterfaceDetailData->DevicePath + 4, TEXT("pci"), 3) != 0)
  1133. {
  1134. free(DeviceInterfaceDetailData);
  1135. DeviceInterfaceDetailData = NULL;
  1136. continue;
  1137. }
  1138. MacAddress tagMacAddress;
  1139. tagMacAddress.nNICKind = NIC_PCI;
  1140. _stprintf_s(tagMacAddress.szNICKind, _T("%s"), _T("NIC_PCI"));
  1141. // 获取设备句柄;
  1142. hDeviceFile = CreateFile(DeviceInterfaceDetailData->DevicePath,
  1143. 0,
  1144. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1145. NULL,
  1146. OPEN_EXISTING,
  1147. 0,
  1148. NULL);
  1149. if (hDeviceFile != INVALID_HANDLE_VALUE)
  1150. {
  1151. ULONG dwID;
  1152. BYTE ucData[8];
  1153. DWORD dwByteRet;
  1154. // 获取原生MAC地址;
  1155. dwID = OID_802_3_PERMANENT_ADDRESS;
  1156. isOK = DeviceIoControl(hDeviceFile, IOCTL_NDIS_QUERY_GLOBAL_STATS, &dwID, sizeof(dwID), ucData, sizeof(ucData), &dwByteRet, NULL);
  1157. if (isOK)
  1158. {
  1159. ++nTotal;
  1160. _stprintf_s(tagMacAddress.szDevicePath, _T("%s"), DeviceInterfaceDetailData->DevicePath);
  1161. memset(tagMacAddress.szMacAddress, 0, sizeof(TCHAR) * MAX_PATH);
  1162. // 将字节数组转换成16进制字符串;
  1163. for (DWORD i = 0; i < dwByteRet; i++)
  1164. {
  1165. _stprintf_s(&tagMacAddress.szMacAddress[i * 3], MAX_PATH - (i * 3), (i != dwByteRet - 1) ? _T("%02X-") : _T("%02X"), ucData[i]);
  1166. }
  1167. g_vtMac.push_back(tagMacAddress);
  1168. }
  1169. CloseHandle(hDeviceFile);
  1170. }
  1171. }
  1172. free(DeviceInterfaceDetailData);
  1173. DeviceInterfaceDetailData = NULL;
  1174. }
  1175. SetupDiDestroyDeviceInfoList(hDevInfo);
  1176. #if 1
  1177. g_strMacs.clear();
  1178. std::vector<MacAddress>::iterator it = g_vtMac.begin();
  1179. for (; it != g_vtMac.end(); it++)
  1180. {
  1181. g_strMacs.append(it->szMacAddress);
  1182. break;
  1183. }
  1184. #endif
  1185. return nTotal;
  1186. }
  1187. std::string Replace(std::string strSrc, std::string strNew, std::string strOld)
  1188. {
  1189. while(true) {
  1190. std::string::size_type pos(0);
  1191. if(( pos = strSrc.find(strOld)) != std::string::npos )
  1192. strSrc.replace(pos,strOld.length(),strNew);
  1193. else
  1194. break;
  1195. }
  1196. return strSrc;
  1197. }
  1198. BOOL Python27Dir()
  1199. {
  1200. HKEY hKey;
  1201. int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
  1202. if (ret != ERROR_SUCCESS)
  1203. return FALSE;
  1204. //读取KEY
  1205. DWORD dwType = REG_SZ; //数据类型
  1206. DWORD cbData = MAX_PATH;
  1207. ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
  1208. if (ret != ERROR_SUCCESS)
  1209. {
  1210. RegCloseKey(hKey);
  1211. return FALSE;
  1212. }
  1213. RegCloseKey(hKey);
  1214. // 给尾加杠;
  1215. if ( g_szPython27Dir[_tcslen(g_szPython27Dir) - 1] != '\\')
  1216. _tcscat_s(g_szPython27Dir, MAX_PATH, "\\");
  1217. return TRUE;
  1218. }
  1219. void Split(std::string str1, std::string str2, std::vector<std::string> &vt)
  1220. {
  1221. if ( str1.size() == 0 || str2.size() == 0 )
  1222. return;
  1223. int npos = str1.find(str2);
  1224. while( std::string::npos != npos )
  1225. {
  1226. vt.push_back(str1.substr(0, npos));
  1227. str1 = str1.substr(npos + str2.size());
  1228. npos = str1.find(str2);
  1229. }
  1230. if ( str1.size() )
  1231. vt.push_back(str1);
  1232. }
  1233. };