Global.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. #include "StdAfx.h"
  2. #include <tlhelp32.h>
  3. #include "Global.h"
  4. #include <windows.h> // 关机函数;
  5. // Jeff.获取文件版本号函数头文件;
  6. #include <WinVer.h>
  7. #pragma comment(lib,"version.lib")
  8. using namespace std;
  9. // 注册OCX bool RegOcx(CString ocxfile)
  10. #include "afxcoll.h"
  11. #include "afxtempl.h"
  12. #include "atlconv.h"
  13. // 在函数GetOSDisplayString的前面需要定义如下宏:
  14. // 同时还要包含strsafe.h
  15. #include <strsafe.h>
  16. #pragma comment(lib, "User32.lib")
  17. #include <wincrypt.h>
  18. #include <tlhelp32.h>
  19. namespace Global
  20. {
  21. //////////////////////////////////////////////////////////////////////////
  22. // 全局变量;
  23. TCHAR g_szHostName[MAX_PATH]; // 主机计算机名;
  24. TCHAR g_szModulePath[MAX_PATH]; // 软件目录;
  25. TCHAR g_szModuleFileName[MAX_PATH]; // 软件名称;
  26. TCHAR g_szConnectString[MAX_PATH]; // DB连接串;
  27. // 服务器信息;
  28. TCHAR g_szDBSource[MAX_PATH]; // 数据库源(服务所在IP或计算机名);
  29. TCHAR g_szDBSourcePort[MAX_PATH]; // 数据库源端口;
  30. DWORD g_dwDBServerPort; // 数据库源端口;
  31. TCHAR g_szDBAccount[MAX_PATH]; // 数据库登录用户;
  32. TCHAR g_szDBPassWord[MAX_PATH]; // 数据库登录密码;
  33. TCHAR g_szDBName[MAX_PATH]; // 数据库名称;
  34. TCHAR g_szLogDBName[MAX_PATH]; // Log数据库名称;
  35. DWORD g_dwCSPort = 5678; // 客户端与服务端的通信端口;
  36. Encryptlockkernel::YtInfoVer1 g_YtInfoVer1;
  37. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  38. #define KEYLENGTH 0x00800000
  39. #define ENCRYPT_ALGORITHM CALG_RC4
  40. #define ENCRYPT_BLOCK_SIZE 8
  41. //////////////////////////////////////////////////////////////////////////
  42. static const int BEGIN_YEAR = 1901;
  43. static const int NUMBER_YEAR = 199;
  44. static const unsigned int LUNAR_YEARS[199] =
  45. {
  46. 0x04AE53, 0x0A5748, 0x5526BD, 0x0D2650, 0x0D9544, 0x46AAB9, 0x056A4D, 0x09AD42, 0x24AEB6, 0x04AE4A, //1901-1910
  47. 0x6A4DBE, 0x0A4D52, 0x0D2546, 0x5D52BA, 0x0B544E, 0x0D6A43, 0x296D37, 0x095B4B, 0x749BC1, 0x049754, //1911-1920
  48. 0x0A4B48, 0x5B25BC, 0x06A550, 0x06D445, 0x4ADAB8, 0x02B64D, 0x095742, 0x2497B7, 0x04974A, 0x664B3E, //1921-1930
  49. 0x0D4A51, 0x0EA546, 0x56D4BA, 0x05AD4E, 0x02B644, 0x393738, 0x092E4B, 0x7C96BF, 0x0C9553, 0x0D4A48, //1931-1940
  50. 0x6DA53B, 0x0B554F, 0x056A45, 0x4AADB9, 0x025D4D, 0x092D42, 0x2C95B6, 0x0A954A, 0x7B4ABD, 0x06CA51, //1941-1950
  51. 0x0B5546, 0x555ABB, 0x04DA4E, 0x0A5B43, 0x352BB8, 0x052B4C, 0x8A953F, 0x0E9552, 0x06AA48, 0x7AD53C, //1951-1960
  52. 0x0AB54F, 0x04B645, 0x4A5739, 0x0A574D, 0x052642, 0x3E9335, 0x0D9549, 0x75AABE, 0x056A51, 0x096D46, //1961-1970
  53. 0x54AEBB, 0x04AD4F, 0x0A4D43, 0x4D26B7, 0x0D254B, 0x8D52BF, 0x0B5452, 0x0B6A47, 0x696D3C, 0x095B50, //1971-1980
  54. 0x049B45, 0x4A4BB9, 0x0A4B4D, 0xAB25C2, 0x06A554, 0x06D449, 0x6ADA3D, 0x0AB651, 0x093746, 0x5497BB, //1981-1990
  55. 0x04974F, 0x064B44, 0x36A537, 0x0EA54A, 0x86B2BF, 0x05AC53, 0x0AB647, 0x5936BC, 0x092E50, 0x0C9645, //1991-2000
  56. 0x4D4AB8, 0x0D4A4C, 0x0DA541, 0x25AAB6, 0x056A49, 0x7AADBD, 0x025D52, 0x092D47, 0x5C95BA, 0x0A954E, //2001-2010
  57. 0x0B4A43, 0x4B5537, 0x0AD54A, 0x955ABF, 0x04BA53, 0x0A5B48, 0x652BBC, 0x052B50, 0x0A9345, 0x474AB9, //2011-2020
  58. 0x06AA4C, 0x0AD541, 0x24DAB6, 0x04B64A, 0x69573D, 0x0A4E51, 0x0D2646, 0x5E933A, 0x0D534D, 0x05AA43, //2021-2030
  59. 0x36B537, 0x096D4B, 0xB4AEBF, 0x04AD53, 0x0A4D48, 0x6D25BC, 0x0D254F, 0x0D5244, 0x5DAA38, 0x0B5A4C, //2031-2040
  60. 0x056D41, 0x24ADB6, 0x049B4A, 0x7A4BBE, 0x0A4B51, 0x0AA546, 0x5B52BA, 0x06D24E, 0x0ADA42, 0x355B37, //2041-2050
  61. 0x09374B, 0x8497C1, 0x049753, 0x064B48, 0x66A53C, 0x0EA54F, 0x06B244, 0x4AB638, 0x0AAE4C, 0x092E42, //2051-2060
  62. 0x3C9735, 0x0C9649, 0x7D4ABD, 0x0D4A51, 0x0DA545, 0x55AABA, 0x056A4E, 0x0A6D43, 0x452EB7, 0x052D4B, //2061-2070
  63. 0x8A95BF, 0x0A9553, 0x0B4A47, 0x6B553B, 0x0AD54F, 0x055A45, 0x4A5D38, 0x0A5B4C, 0x052B42, 0x3A93B6, //2071-2080
  64. 0x069349, 0x7729BD, 0x06AA51, 0x0AD546, 0x54DABA, 0x04B64E, 0x0A5743, 0x452738, 0x0D264A, 0x8E933E, //2081-2090
  65. 0x0D5252, 0x0DAA47, 0x66B53B, 0x056D4F, 0x04AE45, 0x4A4EB9, 0x0A4D4C, 0x0D1541, 0x2D92B5 //2091-2099
  66. };
  67. //////////////////////////////////////////////////////////////////////////
  68. static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  69. {
  70. switch (uMsg)
  71. {
  72. case BFFM_INITIALIZED:
  73. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, NULL);
  74. break;
  75. case BFFM_SELCHANGED:
  76. {
  77. TCHAR szFileName[MAX_PATH];
  78. LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
  79. SHGetPathFromIDList(pidlCurrent, szFileName);
  80. SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
  81. }
  82. break;
  83. // 这个不能让默认目录为桌面;
  84. // case BFFM_INITIALIZED:
  85. // {
  86. // // WParam is TRUE since you are passing a path.
  87. // // It would be FALSE if you were passing a pidl.
  88. // TCHAR szDir[MAX_PATH] = { 0 };
  89. // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
  90. // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
  91. // }
  92. // break;
  93. }
  94. return 0;
  95. }
  96. BOOL GetFilePath(TCHAR *FilePath, HWND hParentWnd, TCHAR* InstructionString, UINT ulFlags)
  97. {
  98. BROWSEINFO bInfo;
  99. LPITEMIDLIST pidl, pidlDesktop;
  100. ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
  101. SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
  102. bInfo.pidlRoot = pidlDesktop;
  103. bInfo.hwndOwner = hParentWnd;
  104. bInfo.pszDisplayName = FilePath;
  105. bInfo.lpszTitle = InstructionString;
  106. bInfo.ulFlags = ulFlags;
  107. bInfo.lpfn = BrowseCallbackProc;
  108. bInfo.lParam = 0;
  109. if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL) return FALSE;
  110. if (::SHGetPathFromIDList(pidl, FilePath) == FALSE) return FALSE;
  111. return TRUE;
  112. }
  113. void GetHostName()
  114. {
  115. CHAR szHostName[MAX_PATH] = {0};
  116. gethostname(szHostName,MAX_PATH);
  117. #ifdef UNICODE
  118. TCHAR *pHostName = ascii2unicode(szHostName);
  119. #else
  120. TCHAR *pHostName = szHostName;
  121. #endif
  122. _stprintf_s(g_szHostName, _T("%s"), pHostName);
  123. #ifdef UNICODE
  124. delete []pHostName;
  125. #endif
  126. }
  127. BOOL IsValidDate(IN LPCTSTR lpDate) // 判断公历;
  128. {
  129. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  130. return FALSE;
  131. CString strDate = lpDate;
  132. if (strDate.IsEmpty()) return FALSE;
  133. // 判断日期合法性;
  134. static int nYear = 0;
  135. static int nMonth = 0;
  136. static int nDay = 0;
  137. nYear = nMonth = nDay = 0;
  138. nYear = _ttoi(strDate.Left(4));
  139. nMonth = _ttoi(strDate.Mid(5, 2));
  140. nDay = _ttoi(strDate.Mid(8, 2));
  141. CONST static INT nNormalMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  142. CONST static INT nLeapMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  143. if ((nDay <= 0) || (nDay > 31) || (nMonth <= 0) || (nMonth > 12)) return FALSE;
  144. if ((nYear % 400 == 0) || (nYear % 4 == 0 && nYear % 100 != 0))
  145. {
  146. if (nDay > nLeapMonth[nMonth - 1]) return FALSE;
  147. }
  148. else
  149. {
  150. if (nDay > nNormalMonth[nMonth - 1]) return FALSE;
  151. }
  152. return TRUE;
  153. }
  154. BOOL IsValidLunanrDate(IN LPCTSTR lpDate) // 判断农历;
  155. {
  156. if (lpDate == NULL || _tcscmp(lpDate, _T("")) == 0)
  157. return FALSE;
  158. CString strDate = lpDate;
  159. if (strDate.IsEmpty()) return FALSE;
  160. static int lunar_year, lunar_month, lunar_day;
  161. lunar_year = lunar_month = lunar_day = 0;
  162. lunar_year = _ttoi(strDate.Left(4));
  163. lunar_month = _ttoi(strDate.Mid(5, 2));
  164. lunar_day = _ttoi(strDate.Mid(8, 2));
  165. //越界检查,如果越界,返回无效日期;
  166. if (lunar_year <= BEGIN_YEAR || lunar_year > BEGIN_YEAR + NUMBER_YEAR - 1)
  167. return FALSE;
  168. if (lunar_month < 1 || lunar_month > 12)
  169. return FALSE;
  170. // 1.农历年是否有闰月;
  171. int nYearIndex = lunar_year - BEGIN_YEAR;
  172. int leap_month = (LUNAR_YEARS[nYearIndex] >> 20) & 0xF;
  173. // 2.月份大小月天数;
  174. int month_day = 0;
  175. if ((LUNAR_YEARS[nYearIndex] >> (7 + 13 - lunar_month)) & 0x1)
  176. month_day = 30;
  177. else
  178. month_day = 29;
  179. if (lunar_day < 0 || lunar_day > month_day)
  180. return FALSE;
  181. return TRUE;
  182. }
  183. TString lowercase(IN const TString &Str)
  184. {
  185. const TCHAR aazz = _T('z') - _T('Z');
  186. TString sResult;
  187. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  188. {
  189. if (_T('A') <= *it && *it <= _T('Z'))
  190. sResult.append(1, *it + aazz);
  191. else
  192. sResult.append(1, *it);
  193. }
  194. return sResult;
  195. }
  196. TString uppercase(IN const TString &Str)
  197. {
  198. const TCHAR aazz = _T('z') - _T('Z');
  199. TString sResult;
  200. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  201. {
  202. if (_T('a') <= *it && *it <= _T('z'))
  203. sResult.append(1, *it - aazz);
  204. else
  205. sResult.append(1, *it);
  206. }
  207. return sResult;
  208. }
  209. int match(IN CONST TString &sFileExt, IN CONST TString &sFile)
  210. {
  211. TString fext = uppercase(sFileExt);
  212. TString file = uppercase(sFile);
  213. int pos = file.find_last_of(_T('.'));
  214. if (TString::npos != pos)
  215. file = file.substr(pos);
  216. return (TString::npos != fext.find(file));
  217. }
  218. TString getfilename(IN CONST TString &file)
  219. {
  220. TString name;
  221. TString strfile = file;
  222. int nIndex = strfile.find_last_of(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  223. if (nIndex == TString::npos)
  224. {
  225. nIndex = strfile.find_last_of(_T('.'));
  226. if ( nIndex == TString::npos )
  227. return _T("");
  228. return strfile.substr(0, nIndex);
  229. }
  230. name = strfile.substr(nIndex+1);
  231. nIndex = name.find_last_of(_T('.'));
  232. if (nIndex == TString::npos)
  233. return _T("");
  234. return name.substr(0, nIndex);
  235. }
  236. // 获取文件名;
  237. CString getfilename(IN CONST CString &file)
  238. {
  239. CString name;
  240. CString strfile = file;
  241. int nIndex = strfile.ReverseFind(_T('\\')); // 如果file不包含 '\\' ,得不到文件名;
  242. if (nIndex == -1)
  243. {
  244. nIndex = strfile.ReverseFind(_T('.'));
  245. if (nIndex == -1)
  246. return _T("");
  247. return strfile.Left(nIndex);
  248. }
  249. name = strfile.Mid(nIndex+1);
  250. nIndex = name.Find(_T('.'));
  251. if (nIndex == -1)
  252. return _T("");
  253. return name.Left(nIndex);
  254. }
  255. int GetIniInfo(IN const TCHAR *szPath , const TCHAR *szIniName )
  256. {
  257. TCHAR szDrive[_MAX_DRIVE];
  258. TCHAR szDir[_MAX_DIR];
  259. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  260. _stprintf_s(g_szModulePath, MAX_PATH, _T("%s"), g_szModulePath);
  261. _tsplitpath(g_szModulePath, szDrive, szDir, NULL, NULL);
  262. _tcscpy_s(g_szModulePath, szDrive);
  263. _tcscat_s(g_szModulePath, szDir);
  264. // -----------------------------------------------------//
  265. TCHAR szIniPath[MAX_PATH] = {0};
  266. if (szPath != NULL && szIniName != NULL)
  267. _stprintf_s(szIniPath, _T("%s%s"), szPath, szIniName);
  268. else
  269. _stprintf_s(szIniPath, _T("%sServiceInfo.ini"), g_szModulePath);
  270. HANDLE hFile = CreateFile(szIniPath, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  271. if (ERROR_FILE_NOT_FOUND == GetLastError())
  272. {
  273. return -1;
  274. }
  275. // 获取服务器端信息;
  276. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szIniPath);
  277. g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szIniPath);
  278. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szIniPath);
  279. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szIniPath);
  280. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szIniPath);
  281. g_dwCSPort = GetPrivateProfileInt(_T("NetWorkInfo"), _T("TCPChatPort"), 5678, szIniPath);
  282. if (g_dwDBServerPort != 0)
  283. _stprintf_s(g_szConnectString, MAX_PATH, _T("driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s"),
  284. g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
  285. else
  286. _stprintf_s(g_szConnectString, MAX_PATH, _T("driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s"),
  287. g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
  288. return 0;
  289. }
  290. void GetDirveInfo()
  291. {
  292. UINT DiskType;
  293. size_t szAllDriveStrings = GetLogicalDriveStrings(0, NULL);
  294. TCHAR *pDriveStrings = new TCHAR[szAllDriveStrings + sizeof(_T(" "))];
  295. GetLogicalDriveStrings(szAllDriveStrings, pDriveStrings);
  296. size_t szDriveString = _tcslen(pDriveStrings);
  297. while (szDriveString > 0)
  298. {
  299. AfxMessageBox(pDriveStrings);
  300. DiskType = GetDriveType(pDriveStrings);
  301. switch (DiskType)
  302. {
  303. case DRIVE_NO_ROOT_DIR:
  304. return;
  305. case DRIVE_REMOVABLE:
  306. ::AfxMessageBox(_T("移动存储设备 "));
  307. break;
  308. case DRIVE_FIXED:
  309. ::AfxMessageBox(_T("固定硬盘驱动器 "));
  310. break;
  311. case DRIVE_REMOTE:
  312. ::AfxMessageBox(_T("这是网络驱动器 "));
  313. break;
  314. case DRIVE_CDROM:
  315. ::AfxMessageBox(_T("这是光盘驱动器 "));
  316. break;
  317. }
  318. pDriveStrings += szDriveString + 1;
  319. szDriveString = _tcslen(pDriveStrings);
  320. }
  321. }
  322. BOOL OpenFloder(IN CStringArray &vtFiles, IN LPCTSTR lpFilter )
  323. {
  324. // 获取桌面路径;
  325. TCHAR szDesktop[MAX_PATH];
  326. SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
  327. CString strFindFile = _T("");
  328. LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
  329. //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
  330. LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
  331. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
  332. CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
  333. dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
  334. dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
  335. // 最多可以打开nMaxFile个文件
  336. // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
  337. dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
  338. dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
  339. ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
  340. // 显示文件对话框,获得文件名集合;
  341. if (dlg.DoModal() == IDCANCEL)
  342. {
  343. delete[]dlg.m_ofn.lpstrFile;
  344. dlg.m_ofn.lpstrFile = NULL;
  345. return FALSE;
  346. }
  347. POSITION pos_file = dlg.GetStartPosition();
  348. while (pos_file != NULL)
  349. vtFiles.Add((LPCTSTR)dlg.GetNextPathName(pos_file));
  350. // 释放内存;
  351. delete[]dlg.m_ofn.lpstrFile;
  352. dlg.m_ofn.lpstrFile = NULL;
  353. return TRUE;
  354. }
  355. BOOL OpenFloder(IN STR_VEC &vtFiles, IN LPCTSTR lpFilter)
  356. {
  357. // 获取桌面路径;
  358. TCHAR szDesktop[MAX_PATH];
  359. SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
  360. CString strFindFile = _T("");
  361. LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
  362. //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
  363. LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
  364. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
  365. CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
  366. dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
  367. dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
  368. // 最多可以打开nMaxFile个文件
  369. // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
  370. dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
  371. dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
  372. ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
  373. // 显示文件对话框,获得文件名集合;
  374. if (dlg.DoModal() == IDCANCEL)
  375. {
  376. delete[]dlg.m_ofn.lpstrFile;
  377. dlg.m_ofn.lpstrFile = NULL;
  378. return FALSE;
  379. }
  380. POSITION pos_file = dlg.GetStartPosition();
  381. while (pos_file != NULL)
  382. vtFiles.push_back((LPCTSTR)dlg.GetNextPathName(pos_file));
  383. // 释放内存;
  384. delete[]dlg.m_ofn.lpstrFile;
  385. dlg.m_ofn.lpstrFile = NULL;
  386. return TRUE;
  387. }
  388. // 判断指定字符串是否在数组里(区分大小写);
  389. BOOL IsStringExist(IN CString& str, IN CStringArray &tagAry)
  390. {
  391. int nSize = tagAry.GetSize();
  392. if (nSize == 0) return FALSE;
  393. BOOL bExist = FALSE;
  394. for (int i = 0; i < nSize; i++)
  395. {
  396. if (str == tagAry.ElementAt(i))
  397. {
  398. bExist = TRUE;
  399. break;
  400. }
  401. }
  402. return bExist;
  403. }
  404. // 判断指定字符串是否在数组里(区分大小写);
  405. BOOL IsStringExist(IN TString& str, IN STR_VEC &tagVt)
  406. {
  407. int nSize = tagVt.size();
  408. if (nSize == 0) return FALSE;
  409. BOOL bExist = FALSE;
  410. for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  411. {
  412. if (str.compare(it->c_str()) == 0)
  413. {
  414. bExist = TRUE;
  415. break;
  416. }
  417. }
  418. return bExist;
  419. }
  420. // 判断指定字符串是否在数组里(不区分大小写);
  421. BOOL IsStringExistNoCase(IN CString& str, IN CStringArray &tagAry)
  422. {
  423. int nSize = tagAry.GetSize();
  424. if (nSize == 0) return FALSE;
  425. BOOL bExist = FALSE;
  426. for (int i = 0; i < nSize; i++)
  427. {
  428. if (str.CompareNoCase(tagAry.ElementAt(i)) == 0)
  429. {
  430. bExist = TRUE;
  431. break;
  432. }
  433. }
  434. return bExist;
  435. }
  436. // 判断指定字符串是否在数组里(不区分大小写);
  437. BOOL IsStringExistNoCase(IN CONST TString& str, IN STR_VEC &tagVt)
  438. {
  439. int nSize = tagVt.size();
  440. if (nSize == 0) return FALSE;
  441. BOOL bExist = FALSE;
  442. TString stmp1 = uppercase(str);
  443. TString stmp2;
  444. for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  445. {
  446. stmp2 = uppercase(*it);
  447. if (stmp1.compare(stmp2) == 0)
  448. {
  449. bExist = TRUE;
  450. break;
  451. }
  452. }
  453. return bExist;
  454. }
  455. /************************************************************************/
  456. /*
  457. 函数:IsValidpath
  458. 描述:判断一个路径是否是本地可以盘符或可用的网络路径;
  459. 参数:
  460. IN: strPath 要判断的路径;
  461. 返回:盘符或网络路径有效返回TRUE;
  462. */
  463. /************************************************************************/
  464. BOOL IsValidpath(IN CONST TString &strPath)
  465. {
  466. // 文件表现形式有三种:
  467. // 1.本地形式: C:\myfolder\you
  468. // 2.共享形式: \\192.168.1.112\myfolder\\you
  469. if (strPath.size() < 2) return FALSE;
  470. // 含":"为本地路径;
  471. if (strPath[1] == _T(':'))
  472. {
  473. DWORD dwDiskType = 0;
  474. dwDiskType = GetDriveType(strPath.substr(0, 3).c_str());
  475. switch (dwDiskType)
  476. {
  477. case DRIVE_UNKNOWN: // 未知驱动盘符;
  478. case DRIVE_NO_ROOT_DIR: // 没有根目录;
  479. //case DRIVE_REMOVABLE: // 移动存储设备盘符;
  480. // case DRIVE_FIXED: // 硬盘;
  481. case DRIVE_CDROM: // CD盘符;
  482. case DRIVE_RAMDISK: // ;
  483. return FALSE;
  484. break;
  485. }
  486. }
  487. else if (strPath.substr(0, 2).find(_T("\\\\")) != TString::npos)
  488. {
  489. // 共享路径;
  490. TString strtmp = strPath.substr(2);
  491. int n = strPath.substr(2).find(_T("\\"));
  492. if (n == TString::npos)
  493. return FALSE;
  494. strtmp = strPath.substr(0, n + 2);
  495. // 需要判断网络路径可用否;
  496. DWORD dwRet = 0;
  497. NETRESOURCE nr;
  498. memset(&nr, 0, sizeof(nr));
  499. nr.dwScope = RESOURCE_CONNECTED;
  500. nr.dwType = RESOURCETYPE_ANY;
  501. nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
  502. nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
  503. #ifndef UNICODE
  504. nr.lpRemoteName = (LPSTR)strtmp.c_str();
  505. #else
  506. nr.lpRemoteName = (LPWSTR)(strtmp.c_str());
  507. #endif
  508. dwRet = WNetAddConnection2(&nr, NULL, NULL, CONNECT_UPDATE_PROFILE);
  509. if (dwRet == ERROR_BAD_NETPATH)// 无效的网络路径,表示没有这个网络地址;
  510. return FALSE;
  511. else if (dwRet == ERROR_LOGON_FAILURE)// 登陆这个网络路径失败,因为当前用户名和密码错误; 没有权限;
  512. return FALSE;
  513. }
  514. return TRUE;
  515. }
  516. void Splitpath(IN CONST TCHAR *path, IN TCHAR * drive, IN size_t driveNumberOfElements, IN TCHAR * dir, IN size_t dirNumberOfElements, IN CONST int &nTimes)
  517. {
  518. int i = 0;
  519. int npoint = 0; // '.'
  520. int nsprit = 0; // '\\'
  521. int ncolon = 0; // ':'
  522. int ntimes = -1;
  523. #ifdef UNICODE // 使用_tcslen来代替wcslen和strlen有问题,在UNICODE时还没有问题,一但转成多字节后_tcslen就不准确;
  524. int nlen = wcslen(path);
  525. #else
  526. int nlen = strlen(path);
  527. #endif
  528. while (nlen > i++)
  529. {
  530. if (path[i] == _T(':'))
  531. ncolon = i;
  532. else if (path[i] == _T('\\'))
  533. {
  534. nsprit = i;
  535. if (nTimes == ++ntimes) break;
  536. }
  537. }
  538. //memcpy_s(drive, driveNumberOfElements*sizeof(TCHAR), path, (ncolon + 1)*sizeof(TCHAR));
  539. //memcpy_s(dir, dirNumberOfElements*sizeof(TCHAR), &path[ncolon + 1], (nsprit - ncolon)*sizeof(TCHAR));
  540. #ifdef UNICODE
  541. wmemcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
  542. wmemcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
  543. #else
  544. memcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
  545. memcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
  546. #endif
  547. }
  548. /************************************************************************/
  549. /*
  550. 函数:全局函数JCreateDirectory,多字节版本,支持UNICODE
  551. 描述:创建目录及子目录文件夹;
  552. 参数:
  553. lpDestDirectory:目录,必须是"E:\lyfzdb\数据\"格式,目录最后必须有"\",否则最后一个子目录无法创建!
  554. 建议使用系统_tsplitpath来分隔目录,这样会保存最后一个"\"斜杠;
  555. 返回:成功创建返回TRUE;
  556. */
  557. /************************************************************************/
  558. BOOL CreateDirectoryEx(IN LPCTSTR lpNewDirectory)
  559. {
  560. // 判断路径有效性
  561. if (!lpNewDirectory || !IsValidpath(lpNewDirectory)) return FALSE;
  562. BOOL bExists = FALSE;
  563. TCHAR szNewFileName[_MAX_PATH] = { 0 };
  564. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  565. TCHAR szDir[_MAX_DIR] = { 0 };
  566. TCHAR szFna[_MAX_FNAME] = { 0 };
  567. TCHAR szExt[_MAX_EXT] = { 0 };
  568. TCHAR szNewDirectory[_MAX_PATH] = { 0 };
  569. if (lpNewDirectory[_tcslen(lpNewDirectory) - 1] != _T('\\'))
  570. StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s\\"), lpNewDirectory);
  571. else
  572. StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s"), lpNewDirectory);
  573. int nIndex = 0;
  574. do
  575. {
  576. bExists = PathFileExists(szNewDirectory);
  577. if (!bExists)
  578. {
  579. memset(szDrive, 0, _MAX_DRIVE*sizeof(TCHAR));
  580. memset(szDir, 0, _MAX_DIR*sizeof(TCHAR));
  581. memset(szFna, 0, _MAX_FNAME*sizeof(TCHAR));
  582. memset(szExt, 0, _MAX_EXT*sizeof(TCHAR));
  583. Splitpath(szNewDirectory, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, ++nIndex);
  584. StringCchPrintf(szNewFileName, _MAX_PATH, _T("%s%s"), szDrive, szDir);
  585. if (!CreateDirectory(szNewFileName, NULL))
  586. {
  587. DWORD dwError = GetLastError();
  588. switch (dwError)
  589. {
  590. case ERROR_PATH_NOT_FOUND: // 路径未找到,上一层目录未创建导致该错误;
  591. case ERROR_ACCESS_DENIED: // 没有权限访问;
  592. case ERROR_BAD_NET_NAME: // 错误的网络名称;
  593. //case ERROR_BAD_PATHNAME: //
  594. return FALSE;
  595. break;
  596. case ERROR_ALREADY_EXISTS: // 文件存在,不需要创建;
  597. break;
  598. default:
  599. break;
  600. }
  601. }
  602. }
  603. } while (!bExists);
  604. return TRUE;
  605. }
  606. /************************************************************************/
  607. /*
  608. 函数:GetDiskSpaceInfo
  609. 描述:获取指定目录或盘符的容量信息;
  610. 参数:
  611. IN: lpRootPathName 根目录名称;
  612. INOUT: dTotalNum 总容量;
  613. INOUT: dFreeNum 可用容量;
  614. 返回:
  615. 注意:
  616. GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G);
  617. GetDiskFreeSpaceEx支持64位(16384P=1024*16384T)
  618. 能获取本机硬盘信息,但路径必须包含":"冒号, 同时还可以获取网络路径的共享硬盘大小;
  619. 调用约定:
  620. */
  621. /************************************************************************/
  622. BOOL GetDiskSpaceInfo(IN LPCTSTR lpRootPathName, IN OUT double &dTotalNum, IN OUT double &dFreeNum)
  623. {
  624. #if 1
  625. // 校验参数有效性;
  626. if (lpRootPathName == NULL || _tcscmp(lpRootPathName, _T("")) == 0)
  627. {
  628. //_tprintf_s(_T("根目录为空!"));
  629. return FALSE;
  630. }
  631. // 如果长度==1;
  632. int nlen = _tcslen(lpRootPathName);
  633. if ((1 == nlen) || (2 == nlen && lpRootPathName[nlen - 1] != _T(':')))
  634. {
  635. //_tprintf_s(_T("根目录无效!\n"));
  636. return FALSE;
  637. }
  638. if (!PathFileExists(lpRootPathName))
  639. {
  640. //_tprintf_s(_T("根目录不存在!\n"));
  641. return FALSE;
  642. }
  643. #else
  644. // 使用IsValipath判断网络路径时较慢;
  645. if (!IsValidpath(lpRootPathName) || !PathFileExists(lpRootPathName)) return FALSE;
  646. #endif
  647. BOOL fResult = FALSE;
  648. DWORD dwSectPerClust; // 每簇的扇区数;
  649. DWORD dwBytesPerSect; // 每个扇区的字节数;
  650. DWORD dwFreeClusters; // 可用簇数;
  651. DWORD dwTotalClusters; // 总簇数;
  652. typedef BOOL(WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
  653. P_GDFSE pGetDiskFreeSpaceEx = NULL;
  654. /*unsigned*/ __int64 i64FreeBytesToCaller;
  655. /*unsigned*/ __int64 i64TotalBytes;
  656. /*unsigned*/ __int64 i64FreeBytes;
  657. // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace;
  658. #ifdef UNICODE
  659. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExW");
  660. #else
  661. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
  662. #endif
  663. if (pGetDiskFreeSpaceEx)
  664. {
  665. fResult = pGetDiskFreeSpaceEx(lpRootPathName,
  666. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  667. (PULARGE_INTEGER)&i64TotalBytes,
  668. (PULARGE_INTEGER)&i64FreeBytes);
  669. if (fResult)
  670. {
  671. // 将单位由byte转为G byte;
  672. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  673. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  674. }
  675. }
  676. else
  677. {
  678. fResult = GetDiskFreeSpace(lpRootPathName,
  679. &dwSectPerClust,
  680. &dwBytesPerSect,
  681. &dwFreeClusters,
  682. &dwTotalClusters);
  683. if (fResult)
  684. {
  685. // 成功获得容量信息;
  686. i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
  687. i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
  688. // 将单位由byte转为G byte;
  689. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  690. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  691. }
  692. }
  693. return fResult;
  694. }
  695. // 通过路径名查找进程,返回进程ID;
  696. BOOL FindAppProcessID(IN CString path, OUT DWORD &dwProcessID)
  697. {
  698. int nIndex = path.ReverseFind('\\');
  699. path = path.Right(path.GetLength() - nIndex - 1);
  700. dwProcessID = FindProcess(path);
  701. return (dwProcessID != 0 ? TRUE : FALSE);
  702. BOOL bFound = FALSE;
  703. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  704. PROCESSENTRY32 Info;
  705. Info.dwSize = sizeof(PROCESSENTRY32);
  706. path.MakeLower();
  707. if (::Process32First(handle, &Info))
  708. {
  709. do{
  710. CString ss = Info.szExeFile;
  711. ss.MakeLower();
  712. if (ss == path)
  713. {
  714. ::CloseHandle(handle);
  715. dwProcessID = Info.th32ProcessID;
  716. bFound = TRUE;
  717. break;
  718. }
  719. } while (::Process32Next(handle, &Info));
  720. if (handle) ::CloseHandle(handle);
  721. }
  722. return (bFound ? TRUE : FALSE);
  723. }
  724. // 根据路径名查找进程,返回进程ID;
  725. DWORD FindProcess(IN CString strProName)
  726. {
  727. DWORD dwProcessID = 0;
  728. PROCESSENTRY32 pe32 = { 0 };
  729. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  730. if (hProcessSnap == NULL)
  731. {
  732. return 0;
  733. }
  734. pe32.dwSize = sizeof(PROCESSENTRY32);
  735. if (Process32First(hProcessSnap, &pe32))
  736. {
  737. do
  738. {
  739. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  740. {
  741. dwProcessID = pe32.th32ProcessID;
  742. break;
  743. }
  744. } while (Process32Next(hProcessSnap, &pe32));
  745. }
  746. CloseHandle(hProcessSnap);
  747. return dwProcessID;
  748. }
  749. // 根据路径名查找进程,查找成功并结束;
  750. int FindAndCloseProcess(IN CString strProName)
  751. {
  752. int nIndex = strProName.ReverseFind('\\');
  753. if (nIndex != -1) // 如果传的是全路径;
  754. strProName = strProName.Right(strProName.GetLength() - nIndex - 1);
  755. DWORD dwProcessID = 0;
  756. PROCESSENTRY32 pe32 = { 0 };
  757. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  758. if (hProcessSnap == NULL)
  759. {
  760. return 0;
  761. }
  762. pe32.dwSize = sizeof(PROCESSENTRY32);
  763. if (Process32First(hProcessSnap, &pe32))
  764. {
  765. do
  766. {
  767. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  768. {
  769. dwProcessID = pe32.th32ProcessID;
  770. break;
  771. }
  772. } while (Process32Next(hProcessSnap, &pe32));
  773. }
  774. CloseHandle(hProcessSnap);
  775. if (dwProcessID == 0)
  776. return 0;
  777. HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
  778. if (hProcess == NULL)
  779. return 0;
  780. DWORD dwError;
  781. if (!TerminateProcess(hProcess, 0))
  782. {
  783. dwError = GetLastError();
  784. CloseHandle(hProcess);
  785. return -1;
  786. }
  787. // 等待进程结束响应;
  788. if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE))
  789. {
  790. CloseHandle(hProcess);
  791. AfxMessageBox(_T("结束进程失败"));
  792. return -1;
  793. }
  794. CloseHandle(hProcess);
  795. return 1;
  796. }
  797. // Jeff.Hacker. WINDOWS NT 以上的内核需要提权,才能对系统进行高级管理;
  798. BOOL GetDebugPriv()
  799. {
  800. // 返回的访问令牌指针;
  801. HANDLE hToken;
  802. // 接收所返回的制定特权名称的信息;
  803. LUID sedebugnameValue;
  804. // 新特权信息的指针(结构体);
  805. TOKEN_PRIVILEGES tkp;
  806. DWORD dwCurProcId = GetCurrentProcessId();
  807. // 要修改访问权限的进程句柄;
  808. HANDLE hCurProc;
  809. hCurProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurProcId);
  810. if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  811. {
  812. ShowSystemErrorInfo(_T("升级包OpenProcessToken失败."),GetLastError());
  813. return FALSE;
  814. }
  815. if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
  816. {
  817. ShowSystemErrorInfo(_T("升级包LookupPrivilegeValue失败."),GetLastError());
  818. CloseHandle(hToken);
  819. return FALSE;
  820. }
  821. tkp.PrivilegeCount = 1;
  822. tkp.Privileges[0].Luid = sedebugnameValue;
  823. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  824. if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
  825. {
  826. ShowSystemErrorInfo(_T("升级包AdjustTokenPrivileges失败."),GetLastError());
  827. CloseHandle(hToken);
  828. return FALSE;
  829. }
  830. CloseHandle(hCurProc);
  831. CloseHandle(hToken);
  832. return TRUE;
  833. }
  834. bool GetFileVersion(HMODULE hModule, WORD *pBuffer)
  835. {
  836. TCHAR fname[MAX_PATH];
  837. VS_FIXEDFILEINFO *pVi;
  838. DWORD dwHandle;
  839. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  840. {
  841. int size = GetFileVersionInfoSize(fname, &dwHandle);
  842. if (size > 0)
  843. {
  844. BYTE *buffer = new BYTE[size];
  845. memset(buffer, 0, size);
  846. if (GetFileVersionInfo(fname, dwHandle, size, buffer))
  847. {
  848. if (VerQueryValue(buffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&size))
  849. {
  850. pBuffer[0] = HIWORD(pVi->dwFileVersionMS);
  851. pBuffer[1] = LOWORD(pVi->dwFileVersionMS);
  852. pBuffer[2] = HIWORD(pVi->dwFileVersionLS);
  853. pBuffer[3] = LOWORD(pVi->dwFileVersionLS);
  854. delete[]buffer;
  855. return true;
  856. }
  857. }
  858. delete[]buffer;
  859. }
  860. }
  861. return false;
  862. }
  863. bool GetProductVersion(HMODULE hModule, WORD *pBuffer)
  864. {
  865. TCHAR fname[MAX_PATH];
  866. VS_FIXEDFILEINFO *pVi;
  867. DWORD dwHandle;
  868. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  869. {
  870. int size = GetFileVersionInfoSize(fname, &dwHandle);
  871. if (size > 0)
  872. {
  873. BYTE *buffer = new BYTE[size];
  874. memset(buffer, 0, size);
  875. if (GetFileVersionInfo(fname, dwHandle, size, buffer))
  876. {
  877. if (VerQueryValue(buffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&size))
  878. {
  879. pBuffer[0] = HIWORD(pVi->dwProductVersionMS);
  880. pBuffer[1] = LOWORD(pVi->dwProductVersionMS);
  881. pBuffer[2] = HIWORD(pVi->dwProductVersionLS);
  882. pBuffer[3] = LOWORD(pVi->dwProductVersionLS);
  883. delete[]buffer;
  884. return true;
  885. }
  886. }
  887. delete[]buffer;
  888. }
  889. }
  890. return false;
  891. }
  892. /************************************************************************/
  893. /*
  894. 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{AC5D0DDE-BD4C-11D1-B137-0000F8753F5D}\INPROCSERVER32\[]
  895. 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{3A2B370C-BA0A-11D1-B137-0000F8753F5D}\INPROCSERVER32\[]
  896. */
  897. /************************************************************************/
  898. //----------------------------------------------------------
  899. // 函数:RegisterOcx
  900. // 描述:注册指定的组件文件;
  901. // 参数:ocxfile指定要注册的组件文件名;
  902. // 返回:TRUE表示注册成功;
  903. //----------------------------------------------------------
  904. BOOL RegisterOcx(LPCTSTR ocxfile)
  905. {
  906. HKEY hKey;
  907. bool bRet;
  908. USES_CONVERSION;
  909. ITypeLib* pTypeLib;
  910. CString CLSIDStr;
  911. CLSID clsid;
  912. LPOLESTR wRes = NULL;
  913. if (LoadTypeLib(T2OLE(const_cast<LPTSTR>(ocxfile)), &pTypeLib) == S_OK)
  914. {
  915. TLIBATTR tlibAttr, *ptlibAttr;
  916. ptlibAttr = &tlibAttr;
  917. pTypeLib->GetLibAttr(&ptlibAttr);
  918. clsid = ptlibAttr->guid;
  919. pTypeLib->Release();
  920. }
  921. ::StringFromCLSID(clsid, &wRes);
  922. CLSIDStr = OLE2CT(wRes);
  923. if ((RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("CLSID\\") + CLSIDStr, 0, KEY_READ, &hKey) == ERROR_SUCCESS) || (RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("Wow6432Node\\TypeLib\\") + CLSIDStr, 0, KEY_READ, &hKey) == ERROR_SUCCESS))
  924. {
  925. return true;
  926. }
  927. else
  928. {
  929. HINSTANCE hLib = LoadLibrary(ocxfile);
  930. if (hLib < (HINSTANCE)HINSTANCE_ERROR)
  931. {
  932. MessageBox(0, _T("权限不足,不能载入OCX文件!无法注册控件!"), _T("结果"), MB_OK);
  933. bRet = false;
  934. }
  935. else
  936. {
  937. FARPROC lpDllEntryPoint;
  938. //lpDllEntryPoint = GetProcAddress(hLib, _T("DllRegisterServer"));
  939. lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
  940. if (lpDllEntryPoint != NULL)
  941. {
  942. if (FAILED((*lpDllEntryPoint)()))
  943. {
  944. MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK);
  945. bRet = false;
  946. }
  947. else
  948. bRet = true;
  949. }
  950. else
  951. {
  952. MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK);
  953. bRet = false;
  954. }
  955. }
  956. FreeLibrary(hLib);
  957. return bRet;
  958. }
  959. }
  960. //--------------------------------------------------------------------------------
  961. // Jeff add 2014.06.23;
  962. // 函数:ErrorExit
  963. // 描述:
  964. // 参数:
  965. // lpszFunction:函数名;
  966. // dwError:错误码;
  967. //
  968. //--------------------------------------------------------------------------------
  969. void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError)
  970. {
  971. #if 1
  972. LPVOID lpMsgBuf;
  973. BOOL fOk = FormatMessage(
  974. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  975. FORMAT_MESSAGE_FROM_SYSTEM |
  976. FORMAT_MESSAGE_IGNORE_INSERTS,
  977. NULL,
  978. dwError,
  979. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  980. (LPTSTR)&lpMsgBuf,
  981. 0, NULL);
  982. if (!fOk)
  983. {
  984. // Is it a network-related error?
  985. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  986. if (hDll != NULL)
  987. {
  988. FormatMessage(
  989. FORMAT_MESSAGE_FROM_HMODULE |
  990. FORMAT_MESSAGE_FROM_SYSTEM |
  991. FORMAT_MESSAGE_IGNORE_INSERTS,
  992. hDll,
  993. dwError,
  994. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  995. (LPTSTR)&lpMsgBuf,
  996. 0,
  997. NULL);
  998. FreeLibrary(hDll);
  999. }
  1000. }
  1001. if (lpMsgBuf != NULL)
  1002. {
  1003. CString strDisplay;
  1004. strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
  1005. //WriteLog(strDisplay);
  1006. LocalFree(lpMsgBuf);
  1007. }
  1008. else
  1009. {
  1010. //WriteLog(strDescription);
  1011. }
  1012. #else
  1013. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  1014. // Get the error code's textual description
  1015. BOOL fOk = FormatMessage(
  1016. FORMAT_MESSAGE_FROM_SYSTEM |
  1017. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1018. FORMAT_MESSAGE_IGNORE_INSERTS,
  1019. NULL,
  1020. dwError,
  1021. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1022. (PTSTR)&hlocal,
  1023. 0,
  1024. NULL);
  1025. if (!fOk)
  1026. {
  1027. // Is it a network-related error?
  1028. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1029. if (hDll != NULL)
  1030. {
  1031. FormatMessage(
  1032. FORMAT_MESSAGE_FROM_HMODULE |
  1033. FORMAT_MESSAGE_FROM_SYSTEM |
  1034. FORMAT_MESSAGE_IGNORE_INSERTS,
  1035. hDll,
  1036. dwError,
  1037. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1038. (PTSTR)&hlocal,
  1039. 0,
  1040. NULL);
  1041. FreeLibrary(hDll);
  1042. }
  1043. }
  1044. if (hlocal != NULL)
  1045. {
  1046. CString strDisplay;
  1047. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  1048. //WriteLog(strDisplay);
  1049. LocalFree(hlocal);
  1050. }
  1051. else
  1052. {
  1053. //WriteLog("Error number not found.");
  1054. }
  1055. #endif
  1056. }
  1057. // The system displays a dialog box with a custom message and a message to the user to close applications within the specified time-out interval (30 seconds).
  1058. // After the time-out interval elapses, the system is restarted.
  1059. //The application must enable the SE_SHUTDOWN_NAME privilege before calling InitiateSystemShutdown
  1060. BOOL MySystemShutdown(LPTSTR lpMsg)
  1061. {
  1062. HANDLE hToken; // handle to process token
  1063. TOKEN_PRIVILEGES tkp; // pointer to token structure
  1064. BOOL fResult; // system shutdown flag
  1065. // Get the current process token handle so we can get shutdown
  1066. // privilege.
  1067. if (!OpenProcessToken(GetCurrentProcess(),
  1068. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1069. return FALSE;
  1070. // Get the LUID for shutdown privilege.
  1071. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1072. &tkp.Privileges[0].Luid);
  1073. tkp.PrivilegeCount = 1; // one privilege to set
  1074. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1075. // Get shutdown privilege for this process.
  1076. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1077. (PTOKEN_PRIVILEGES)NULL, 0);
  1078. // Cannot test the return value of AdjustTokenPrivileges.
  1079. if (GetLastError() != ERROR_SUCCESS)
  1080. return FALSE;
  1081. // Display the shutdown dialog box and start the countdown.
  1082. fResult = InitiateSystemShutdown(
  1083. NULL, // shut down local computer
  1084. lpMsg, // message for user
  1085. 30, // time-out period, in seconds
  1086. FALSE, // ask user to close apps
  1087. TRUE); // reboot after shutdown
  1088. if (!fResult)
  1089. return FALSE;
  1090. // Disable shutdown privilege.
  1091. tkp.Privileges[0].Attributes = 0;
  1092. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1093. (PTOKEN_PRIVILEGES)NULL, 0);
  1094. return TRUE;
  1095. }
  1096. // If the AbortSystemShutdown function is executed in the time-out period specified by InitiateSystemShutdown,
  1097. // the system does not shut down. For example, if PreventSystemShutdown is called after MySystemShutdown,
  1098. // the system closes the dialog box and does not restart the system.
  1099. BOOL PreventSystemShutdown()
  1100. {
  1101. HANDLE hToken; // handle to process token
  1102. TOKEN_PRIVILEGES tkp; // pointer to token structure
  1103. // Get the current process token handle so we can get shutdown
  1104. // privilege.
  1105. if (!OpenProcessToken(GetCurrentProcess(),
  1106. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1107. return FALSE;
  1108. // Get the LUID for shutdown privilege.
  1109. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1110. &tkp.Privileges[0].Luid);
  1111. tkp.PrivilegeCount = 1; // one privilege to set
  1112. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1113. // Get shutdown privilege for this process.
  1114. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1115. (PTOKEN_PRIVILEGES)NULL, 0);
  1116. if (GetLastError() != ERROR_SUCCESS)
  1117. return FALSE;
  1118. // Prevent the system from shutting down.
  1119. if (!AbortSystemShutdown(NULL))
  1120. return FALSE;
  1121. // Disable shutdown privilege.
  1122. tkp.Privileges[0].Attributes = 0;
  1123. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1124. (PTOKEN_PRIVILEGES)NULL, 0);
  1125. return TRUE;
  1126. }
  1127. // Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer
  1128. // The application must first enable the SE_SHUTDOWN_NAME privilege.
  1129. // The final parameter in the call to ExitWindowsEx indicates that the system was shut down for a planning update of the operating system.
  1130. BOOL MySystemShutdown()
  1131. {
  1132. HANDLE hToken;
  1133. TOKEN_PRIVILEGES tkp;
  1134. // Get a token for this process.
  1135. if (!OpenProcessToken(GetCurrentProcess(),
  1136. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1137. return(FALSE);
  1138. // Get the LUID for the shutdown privilege.
  1139. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1140. &tkp.Privileges[0].Luid);
  1141. tkp.PrivilegeCount = 1; // one privilege to set
  1142. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1143. // Get the shutdown privilege for this process.
  1144. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1145. (PTOKEN_PRIVILEGES)NULL, 0);
  1146. if (GetLastError() != ERROR_SUCCESS)
  1147. return FALSE;
  1148. // Shut down the system and force all applications to close.
  1149. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  1150. SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
  1151. SHTDN_REASON_MINOR_UPGRADE |
  1152. SHTDN_REASON_FLAG_PLANNED))
  1153. return FALSE;
  1154. return TRUE;
  1155. }
  1156. //----------------------------------------------------------
  1157. // 函数:CheckInternalIP
  1158. // 描述:判断IP(网络字节序)地址是否为外网地址;
  1159. // 参数:ip_addr:主机IP(必须为网络字节序);
  1160. //
  1161. // 返回:TRUE表示主机IP为外网IP; FALSE表示内网IP;
  1162. // IP地址转为化DWORD是32位二进制;
  1163. // 每8位一个点十进制;
  1164. //----------------------------------------------------------
  1165. BOOL IsInternalIPV4(const unsigned int &ip_addr)
  1166. {
  1167. // 三段保留IP段,做为私有地址,不在在公网上使用,只作内网IP;
  1168. // A类:10.0.0.0 ~ 10.255.255.255 = 0A.00.00.00 ~ 0A.FF.FF.FF
  1169. // B类:172.16.0.0 ~ 172.31.255.255 = AC.10.00.00 ~ AC.1F.FF.FF
  1170. // C类:192.168.0.0 ~ 192.168.255.255 = C0.A8.00.00 ~ C0.A8.FF.FF
  1171. // D类:127.0.0.0 ~ 127.255.255.255 = 7F.00.00.00 ! 7F.FF.FF.FF (环回地址)
  1172. //
  1173. //检查3类地址是否属于公网;
  1174. // (ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF) ||
  1175. // (ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF) ||
  1176. // (ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF)
  1177. //
  1178. // 检查3类地址是否属于内网;
  1179. // 0xa -- "10.0.0.0">>24;
  1180. // 0xc0a8--"192.168.0.0.">>16;
  1181. // 0x2b0--"127.17.0.1">>22
  1182. // D类: "127.0.0.1~127.254.254.254" >> 0x7
  1183. // 回环地址也算进去!
  1184. if ((ip_addr >> 24 == 0x0A) || (ip_addr >> 20 == 0xAC1) || (ip_addr >> 16 == 0xC0A8) || (ip_addr >> 26 == 0x7F))
  1185. return FALSE;
  1186. else
  1187. return TRUE;
  1188. }
  1189. // 获取指定主机名的IPV4地址; VC 6.0不适合使用本函数;
  1190. // 只适用于VS2010以下版本编译器;
  1191. INT GetHostIPV4( OUT vector<TString>& vtHostIPV4 )
  1192. {
  1193. WSADATA wsa = {0};
  1194. if ( 0 != WSAStartup(MAKEWORD(2, 2),&wsa) )
  1195. return -1;
  1196. CHAR szHostName[MAX_PATH] = {0};
  1197. gethostname(szHostName,MAX_PATH);
  1198. DWORD dwHostIP = 0;
  1199. PHOSTENT hostinfo = gethostbyname(szHostName);
  1200. if ( hostinfo == NULL )
  1201. return -1;
  1202. in_addr addr = {0};
  1203. LPCSTR hostip;
  1204. while(*(hostinfo->h_addr_list) != NULL) // IPV4
  1205. {
  1206. //struct in_addr ad = *(struct in_addr *) *hostinfo->h_addr_list;
  1207. hostip = inet_ntoa(*(struct in_addr *) *hostinfo->h_addr_list); // InetNtop instead;
  1208. DWORD dwIP = inet_addr(hostip);
  1209. if ( IsInternalIPV4(dwIP) )
  1210. {
  1211. #ifdef UNICODE
  1212. WCHAR *psz = ascii2unicode(hostip);
  1213. vtHostIPV4.push_back(psz);
  1214. delete []psz;
  1215. #else
  1216. vtHostIPV4.push_back(hostip);
  1217. #endif
  1218. }
  1219. hostinfo->h_addr_list++;
  1220. }
  1221. return 0;
  1222. }
  1223. // 应用于V14编译平台;
  1224. // 使用GetAddrInfo;
  1225. INT GetHostIPV4_V14(OUT vector<TString>& vtHostIPV4)
  1226. {
  1227. return 0;
  1228. }
  1229. INT get_ip()
  1230. {
  1231. struct addrinfo *ailist, *aip;
  1232. struct addrinfo hint;
  1233. struct sockaddr_in6 *sinp6;
  1234. PHOSTENT hostinfo;
  1235. char hostname[255] = {0}; //主机名
  1236. char *port = "3294"; //端口号
  1237. const char *addr;
  1238. int ilRc;
  1239. gethostname(hostname, sizeof(hostname));
  1240. if((hostinfo = gethostbyname(hostname)) == NULL) //获得本地ipv4地址
  1241. {
  1242. errno = GetLastError();
  1243. fprintf(stderr,"gethostbyname Error:%d\n", errno);
  1244. return 1;
  1245. }
  1246. LPCSTR ip;
  1247. while(*(hostinfo->h_addr_list) != NULL) //输出ipv4地址
  1248. {
  1249. ip = inet_ntoa(*(struct in_addr *) *hostinfo->h_addr_list);
  1250. printf("ipv4 addr = %s\n\n", ip);
  1251. hostinfo->h_addr_list++;
  1252. }
  1253. hint.ai_family = AF_INET6; /* hint 的限定设置 */
  1254. hint.ai_socktype = SOCK_STREAM; /* 这里可是设置 socket type 比如 SOCK——DGRAM */
  1255. hint.ai_flags = AI_PASSIVE; // flags 的标志很多 。常用的有AI_CANONNAME;
  1256. hint.ai_protocol = 0; /* 设置协议 一般为0,默认 */
  1257. hint.ai_addrlen = 0; /* 下面不可以设置,为0,或者为NULL */
  1258. hint.ai_canonname = NULL;
  1259. hint.ai_addr = NULL;
  1260. hint.ai_next = NULL;
  1261. ilRc = getaddrinfo(hostname, port, &hint, &ailist); /*通过主机名获得地址信息*/
  1262. if (ilRc < 0)
  1263. {
  1264. char str_error[100];
  1265. strcpy_s(str_error, (char *)gai_strerror(errno));
  1266. printf("str_error = %s", str_error);
  1267. return 0;
  1268. }
  1269. if(ailist == NULL)
  1270. {
  1271. printf("sorry not find the IP address,please try again \n");
  1272. }
  1273. for (aip = ailist; aip != NULL; aip = aip->ai_next) /* 显示获取的信息 */
  1274. {
  1275. aip->ai_family = AF_INET6;
  1276. sinp6 = (struct sockaddr_in6 *)aip->ai_addr; /* 为什么是for 循环 ,先向下看 */
  1277. int i;
  1278. printf("ipv6 addr = ");
  1279. for(i = 0; i < 16; i++)
  1280. {
  1281. if(((i-1)%2) && (i>0))
  1282. {
  1283. printf(":");
  1284. }
  1285. printf("%02x",sinp6->sin6_addr.u.Byte[i]);
  1286. }
  1287. printf(" \n");
  1288. printf(" \n");
  1289. }
  1290. while(1);
  1291. }
  1292. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  1293. {
  1294. try
  1295. {
  1296. HCRYPTPROV hCryptProv;
  1297. HCRYPTKEY hKey;
  1298. HCRYPTHASH hHash;
  1299. PBYTE pbBuffer;
  1300. DWORD dwBlockLen;
  1301. DWORD dwBufferLen;
  1302. DWORD dwCount;
  1303. //以下获得一个CSP句柄
  1304. if (CryptAcquireContext(
  1305. &hCryptProv,
  1306. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  1307. NULL,
  1308. PROV_RSA_FULL,
  1309. 0))
  1310. {
  1311. printf("A cryptographic provider has been acquired. \n");
  1312. }
  1313. else//密钥容器不存在
  1314. {
  1315. if (CryptAcquireContext(
  1316. &hCryptProv,
  1317. NULL,
  1318. NULL,
  1319. PROV_RSA_FULL,
  1320. CRYPT_NEWKEYSET))//创建密钥容器
  1321. {
  1322. //创建密钥容器成功,并得到CSP句柄
  1323. printf("A new key container has been created.\n");
  1324. }
  1325. else
  1326. {
  1327. return 0;
  1328. }
  1329. }
  1330. //--------------------------------------------------------------------
  1331. // 创建一个会话密钥(session key)
  1332. // 会话密钥也叫对称密钥,用于对称加密算法。
  1333. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  1334. // CryptReleaseContext 期间的阶段。)
  1335. //--------------------------------------------------------------------
  1336. // Create a hash object.
  1337. if (CryptCreateHash(
  1338. hCryptProv,
  1339. CALG_MD5,
  1340. 0,
  1341. 0,
  1342. &hHash))
  1343. {
  1344. printf("A hash object has been created. \n");
  1345. }
  1346. else
  1347. {
  1348. return 0;
  1349. }
  1350. //--------------------------------------------------------------------
  1351. // 用输入的密码产生一个散列
  1352. if (CryptHashData(
  1353. hHash,
  1354. (BYTE *)szPassword,
  1355. strlen(szPassword),
  1356. 0))
  1357. {
  1358. printf("The password has been added to the hash. \n");
  1359. }
  1360. else
  1361. {
  1362. return 0;
  1363. }
  1364. //--------------------------------------------------------------------
  1365. // 通过散列生成会话密钥(session key)
  1366. if (CryptDeriveKey(
  1367. hCryptProv,
  1368. ENCRYPT_ALGORITHM,
  1369. hHash,
  1370. KEYLENGTH,
  1371. &hKey))
  1372. {
  1373. printf("An encryption key is derived from the password hash. \n");
  1374. }
  1375. else
  1376. {
  1377. return 0;
  1378. }
  1379. //--------------------------------------------------------------------
  1380. // Destroy the hash object.
  1381. CryptDestroyHash(hHash);
  1382. hHash = NULL;
  1383. //--------------------------------------------------------------------
  1384. // The session key is now ready.
  1385. //--------------------------------------------------------------------
  1386. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  1387. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  1388. // 数据长度。
  1389. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  1390. //--------------------------------------------------------------------
  1391. // Determine the block size. If a block cipher is used,
  1392. // it must have room for an extra block.
  1393. if (ENCRYPT_BLOCK_SIZE > 1)
  1394. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  1395. else
  1396. dwBufferLen = dwBlockLen;
  1397. dwCount = dwBufferLen;
  1398. //--------------------------------------------------------------------
  1399. // In a do loop, encrypt the source file and write to the source file.
  1400. int count;
  1401. // if(leng%dwBlockLen==0)
  1402. count = leng / dwBufferLen;
  1403. // else
  1404. // count=leng/dwBlockLen+1;
  1405. int i = 0;
  1406. for ( i = 0; i < count; i++)
  1407. {
  1408. pbBuffer = buffer + i*dwBufferLen;
  1409. // 加密数据
  1410. if (!CryptEncrypt(
  1411. hKey, //密钥
  1412. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  1413. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  1414. //这里通过判断是否到文件尾来决定是否为最后一块。
  1415. 0, //保留
  1416. pbBuffer, //输入被加密数据,输出加密后的数据
  1417. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  1418. dwBufferLen)) //pbBuffer的大小。
  1419. {
  1420. return 0;
  1421. }
  1422. }
  1423. if (leng%dwBlockLen)
  1424. {
  1425. pbBuffer = buffer + i*dwBufferLen;
  1426. dwCount = leng - i*dwBufferLen;
  1427. if (!CryptEncrypt(
  1428. hKey, //密钥
  1429. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  1430. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  1431. //这里通过判断是否到文件尾来决定是否为最后一块。
  1432. 0, //保留
  1433. pbBuffer, //输入被加密数据,输出加密后的数据
  1434. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  1435. dwBufferLen)) //pbBuffer的大小。
  1436. {
  1437. return 0;
  1438. }
  1439. }
  1440. //--------------------------------------------------------------------
  1441. // Destroy session key.
  1442. if (hKey)
  1443. CryptDestroyKey(hKey);
  1444. //--------------------------------------------------------------------
  1445. // Destroy hash object.
  1446. if (hHash)
  1447. CryptDestroyHash(hHash);
  1448. //--------------------------------------------------------------------
  1449. // Release provider handle.
  1450. if (hCryptProv)
  1451. CryptReleaseContext(hCryptProv, 0);
  1452. return(TRUE);
  1453. }
  1454. catch (...)
  1455. {
  1456. }
  1457. }
  1458. // 是否运行在Wow64下;XP SP2以上系统才支持;
  1459. BOOL IsWow64()
  1460. {
  1461. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  1462. LPFN_ISWOW64PROCESS fnIsWow64Process;
  1463. BOOL bIsWow64 = FALSE;
  1464. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  1465. if (NULL != fnIsWow64Process)
  1466. {
  1467. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
  1468. {
  1469. // 句柄错误,则系统不是64位;
  1470. AfxMessageBox(_T("句柄错误"));
  1471. }
  1472. }
  1473. return bIsWow64;
  1474. }
  1475. // 2.判断系统是64位还是32位;
  1476. BOOL IsOSVerIs64()
  1477. {
  1478. BOOL bOSVerIs64 = FALSE;
  1479. SYSTEM_INFO si;
  1480. PGNSI pGNSI;
  1481. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1482. if (NULL != pGNSI)
  1483. pGNSI(&si);
  1484. else
  1485. GetSystemInfo(&si);
  1486. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  1487. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1488. {
  1489. bOSVerIs64 = TRUE;
  1490. }
  1491. return bOSVerIs64;
  1492. }
  1493. // 判断进程是否运行在Wow64下;
  1494. INT IsWow64Process(HANDLE hprocess)
  1495. {
  1496. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  1497. LPFN_ISWOW64PROCESS fnIsWow64Process;
  1498. INT nRet = 0;
  1499. BOOL bIsWow64 = FALSE;
  1500. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  1501. if (NULL != fnIsWow64Process)
  1502. {
  1503. if (!fnIsWow64Process(hprocess, &bIsWow64))
  1504. {
  1505. // 句柄错误;
  1506. AfxMessageBox(_T("句柄错误"));
  1507. DWORD dwError = GetLastError();
  1508. return -1;
  1509. }
  1510. }
  1511. return bIsWow64 ? 1 : 0;
  1512. }
  1513. INT GetNativeSystemInfo(OSVerInfo &tagOSInfo)
  1514. {
  1515. OSVERSIONINFOEX osvi;
  1516. SYSTEM_INFO si;
  1517. PGNSI pGNSI;
  1518. BOOL bOsVersionInfoEx;
  1519. ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1520. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  1521. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  1522. // If that fails, try using the OSVERSIONINFO structure.
  1523. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1524. if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
  1525. {
  1526. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1527. if (!GetVersionEx((OSVERSIONINFO *)&osvi))
  1528. return FALSE;
  1529. }
  1530. // Call GetNativeSystemInfo if supported
  1531. // or GetSystemInfo otherwise.
  1532. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1533. if (NULL != pGNSI)
  1534. pGNSI(&si);
  1535. else
  1536. GetSystemInfo(&si);
  1537. TCHAR szWindowsDescription[MAX_PATH] = { 0 };
  1538. CString str;
  1539. CString strWindows;
  1540. switch (osvi.dwPlatformId)
  1541. {
  1542. case VER_PLATFORM_WIN32_NT:
  1543. if (osvi.dwMajorVersion == 10) // Windows10或Windows Server预览版;
  1544. {
  1545. if (osvi.dwMinorVersion == 0)
  1546. {
  1547. if (osvi.wProductType == VER_NT_WORKSTATION)
  1548. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 10 "));
  1549. else
  1550. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server Technical Preview "));
  1551. }
  1552. }
  1553. if (osvi.dwMajorVersion == 6) // Windows Vista ~ Windows 8.1
  1554. {
  1555. if (osvi.dwMinorVersion == 3)
  1556. {
  1557. if (osvi.wProductType == VER_NT_WORKSTATION)
  1558. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8.1 "));
  1559. else
  1560. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 R2 "));
  1561. }
  1562. if (osvi.dwMinorVersion == 2)
  1563. {
  1564. if (osvi.wProductType == VER_NT_WORKSTATION)
  1565. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8 "));
  1566. else
  1567. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 "));
  1568. }
  1569. if (osvi.dwMinorVersion == 1)
  1570. {
  1571. if (osvi.wProductType == VER_NT_WORKSTATION)
  1572. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 7 "));
  1573. else
  1574. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 R2 "));
  1575. }
  1576. if (osvi.dwMinorVersion == 0)
  1577. {
  1578. if (osvi.wProductType == VER_NT_WORKSTATION)
  1579. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Vista "));
  1580. else
  1581. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 "));
  1582. }
  1583. }
  1584. if (osvi.dwMajorVersion == 5)
  1585. {
  1586. if (osvi.dwMinorVersion == 2)
  1587. {
  1588. if (GetSystemMetrics(SM_SERVERR2) != 0)
  1589. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 R2 "));
  1590. else
  1591. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 "));
  1592. if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
  1593. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Home Server "));
  1594. if ((osvi.wProductType == VER_NT_WORKSTATION) && (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
  1595. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP Professional x64 Edition "));
  1596. }
  1597. if (osvi.dwMinorVersion == 1)
  1598. {
  1599. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP "));
  1600. }
  1601. if (osvi.dwMinorVersion == 0)
  1602. {
  1603. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 2000 "));
  1604. }
  1605. }
  1606. if (osvi.dwMajorVersion <= 4)
  1607. {
  1608. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows NT "));
  1609. }
  1610. // Test for specific product on Windows NT 4.0 SP6 and later.
  1611. // 获取Windows NT 4.0 SP6之后的版本;
  1612. if (bOsVersionInfoEx)
  1613. {
  1614. // Test for the workstation type.
  1615. if (osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_AMD64)
  1616. {
  1617. if (osvi.dwMajorVersion == 4)
  1618. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation 4.0 "));
  1619. else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  1620. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Home Edition "));
  1621. else
  1622. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Professional "));
  1623. }
  1624. // Test for the server type.
  1625. else if (osvi.wProductType == VER_NT_SERVER || osvi.wProductType == VER_NT_DOMAIN_CONTROLLER)
  1626. {
  1627. if (osvi.dwMajorVersion == 6)
  1628. {
  1629. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1630. {
  1631. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1632. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
  1633. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1634. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
  1635. }
  1636. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1637. {
  1638. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1639. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
  1640. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1641. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
  1642. else
  1643. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
  1644. }
  1645. else
  1646. {
  1647. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1648. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
  1649. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1650. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
  1651. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1652. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
  1653. else
  1654. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
  1655. }
  1656. }
  1657. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  1658. {
  1659. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1660. {
  1661. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1662. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
  1663. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1664. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
  1665. }
  1666. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1667. {
  1668. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1669. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
  1670. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1671. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
  1672. else
  1673. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
  1674. }
  1675. else
  1676. {
  1677. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1678. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
  1679. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1680. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
  1681. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1682. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
  1683. else
  1684. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
  1685. }
  1686. }
  1687. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  1688. {
  1689. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1690. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Server "));
  1691. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1692. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
  1693. else
  1694. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
  1695. }
  1696. else if (osvi.dwMajorVersion == 4) // Windows NT 4.0
  1697. {
  1698. if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1699. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0, Enterprise Edition "));
  1700. else
  1701. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0 "));
  1702. }
  1703. }
  1704. }
  1705. // Test for specific product on Windows NT 4.0 SP5 and earlier
  1706. else
  1707. {
  1708. HKEY hKey;
  1709. TCHAR szProductType[BUFSIZE];
  1710. DWORD dwBufLen = BUFSIZE*sizeof(TCHAR);
  1711. LONG lRet;
  1712. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 0, KEY_QUERY_VALUE, &hKey);
  1713. if (lRet != ERROR_SUCCESS)
  1714. return FALSE;
  1715. lRet = RegQueryValueEx(hKey, TEXT("ProductType"), NULL, NULL, (LPBYTE)szProductType, &dwBufLen);
  1716. RegCloseKey(hKey);
  1717. if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE*sizeof(TCHAR)))
  1718. return FALSE;
  1719. if (lstrcmpi(TEXT("WINNT"), szProductType) == 0)
  1720. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation "));
  1721. if (lstrcmpi(TEXT("LANMANNT"), szProductType) == 0)
  1722. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
  1723. if (lstrcmpi(TEXT("SERVERNT"), szProductType) == 0)
  1724. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
  1725. //printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
  1726. }
  1727. // Display service pack (if any) and build number.
  1728. if (osvi.dwMajorVersion == 4 && lstrcmpi(osvi.szCSDVersion, TEXT("Service Pack 6")) == 0)
  1729. {
  1730. HKEY hKey;
  1731. LONG lRet;
  1732. // Test for SP6 versus SP6a.
  1733. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"), 0, KEY_QUERY_VALUE, &hKey);
  1734. if (lRet == ERROR_SUCCESS)
  1735. str.Format(_T("Service Pack 6a (Build %d)"), osvi.dwBuildNumber & 0xFFFF);
  1736. else // Windows NT 4.0 prior to SP6a
  1737. {
  1738. str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  1739. }
  1740. RegCloseKey(hKey);
  1741. }
  1742. else // not Windows NT 4.0
  1743. {
  1744. str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  1745. }
  1746. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, str);
  1747. break;
  1748. // Test for the Windows Me/98/95.
  1749. case VER_PLATFORM_WIN32_WINDOWS:
  1750. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
  1751. {
  1752. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 95 "));
  1753. if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
  1754. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("OSR2 "));
  1755. }
  1756. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
  1757. {
  1758. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 98 "));
  1759. if (osvi.szCSDVersion[1] == 'A' || osvi.szCSDVersion[1] == 'B')
  1760. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("SE "));
  1761. }
  1762. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
  1763. {
  1764. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Millennium Edition"));
  1765. }
  1766. break;
  1767. case VER_PLATFORM_WIN32s:
  1768. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Win32s"));
  1769. break;
  1770. }
  1771. AfxMessageBox(strWindows);
  1772. return 0;
  1773. }
  1774. BOOL GetSystemInfo(OSVerInfo &tagOSInfo)
  1775. {
  1776. OSVERSIONINFOEX osvi;
  1777. SYSTEM_INFO si;
  1778. PGNSI pGNSI;
  1779. PGPI pGPI;
  1780. BOOL bOsVersionInfoEx;
  1781. DWORD dwType;
  1782. ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1783. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  1784. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1785. if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
  1786. {
  1787. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1788. if (!GetVersionEx((OSVERSIONINFO *)&osvi))
  1789. return FALSE;
  1790. }
  1791. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1792. if (NULL != pGNSI)
  1793. pGNSI(&si);
  1794. else
  1795. GetSystemInfo(&si);
  1796. tagOSInfo.dwMajorVersion = osvi.dwMajorVersion;
  1797. tagOSInfo.dwMinorVersion = osvi.dwMinorVersion;
  1798. tagOSInfo.wProductType = osvi.wProductType;
  1799. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
  1800. {
  1801. StringCchCopy(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Microsoft "));
  1802. // Test for the specific product.
  1803. if (osvi.dwMajorVersion == 10)
  1804. {
  1805. if (osvi.dwMinorVersion == 0)
  1806. {
  1807. if (osvi.wProductType == VER_NT_WORKSTATION)
  1808. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 10 "));
  1809. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server Technical Preview "));
  1810. }
  1811. pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  1812. pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
  1813. switch (dwType)
  1814. {
  1815. case PRODUCT_ULTIMATE:
  1816. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
  1817. break;
  1818. case PRODUCT_HOME_PREMIUM:
  1819. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
  1820. break;
  1821. case PRODUCT_HOME_BASIC:
  1822. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
  1823. break;
  1824. case PRODUCT_ENTERPRISE:
  1825. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1826. break;
  1827. case PRODUCT_BUSINESS:
  1828. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
  1829. break;
  1830. case PRODUCT_STARTER:
  1831. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
  1832. break;
  1833. case PRODUCT_CLUSTER_SERVER:
  1834. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
  1835. break;
  1836. case PRODUCT_DATACENTER_SERVER:
  1837. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1838. break;
  1839. case PRODUCT_DATACENTER_SERVER_CORE:
  1840. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
  1841. break;
  1842. case PRODUCT_ENTERPRISE_SERVER:
  1843. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1844. break;
  1845. case PRODUCT_ENTERPRISE_SERVER_CORE:
  1846. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
  1847. break;
  1848. case PRODUCT_ENTERPRISE_SERVER_IA64:
  1849. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1850. break;
  1851. case PRODUCT_SMALLBUSINESS_SERVER:
  1852. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
  1853. break;
  1854. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  1855. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
  1856. break;
  1857. case PRODUCT_STANDARD_SERVER:
  1858. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1859. break;
  1860. case PRODUCT_STANDARD_SERVER_CORE:
  1861. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
  1862. break;
  1863. case PRODUCT_WEB_SERVER:
  1864. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
  1865. break;
  1866. }
  1867. }
  1868. if (osvi.dwMajorVersion == 6)
  1869. {
  1870. if (osvi.dwMinorVersion == 0)
  1871. {
  1872. if (osvi.wProductType == VER_NT_WORKSTATION)
  1873. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Vista "));
  1874. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 "));
  1875. }
  1876. if (osvi.dwMinorVersion == 1)
  1877. {
  1878. if (osvi.wProductType == VER_NT_WORKSTATION)
  1879. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 7 "));
  1880. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 R2 "));
  1881. }
  1882. if (osvi.dwMinorVersion == 2)
  1883. {
  1884. if (osvi.wProductType == VER_NT_WORKSTATION)
  1885. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8 "));
  1886. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 "));
  1887. }
  1888. if (osvi.dwMinorVersion == 3)
  1889. {
  1890. if (osvi.wProductType == VER_NT_WORKSTATION)
  1891. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8.1 "));
  1892. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 R2 "));
  1893. }
  1894. pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  1895. pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
  1896. switch (dwType)
  1897. {
  1898. case PRODUCT_ULTIMATE:
  1899. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
  1900. break;
  1901. case PRODUCT_HOME_PREMIUM:
  1902. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
  1903. break;
  1904. case PRODUCT_HOME_BASIC:
  1905. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
  1906. break;
  1907. case PRODUCT_ENTERPRISE:
  1908. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1909. break;
  1910. case PRODUCT_BUSINESS:
  1911. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
  1912. break;
  1913. case PRODUCT_STARTER:
  1914. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
  1915. break;
  1916. case PRODUCT_CLUSTER_SERVER:
  1917. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
  1918. break;
  1919. case PRODUCT_DATACENTER_SERVER:
  1920. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1921. break;
  1922. case PRODUCT_DATACENTER_SERVER_CORE:
  1923. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
  1924. break;
  1925. case PRODUCT_ENTERPRISE_SERVER:
  1926. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1927. break;
  1928. case PRODUCT_ENTERPRISE_SERVER_CORE:
  1929. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
  1930. break;
  1931. case PRODUCT_ENTERPRISE_SERVER_IA64:
  1932. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1933. break;
  1934. case PRODUCT_SMALLBUSINESS_SERVER:
  1935. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
  1936. break;
  1937. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  1938. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
  1939. break;
  1940. case PRODUCT_STANDARD_SERVER:
  1941. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1942. break;
  1943. case PRODUCT_STANDARD_SERVER_CORE:
  1944. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
  1945. break;
  1946. case PRODUCT_WEB_SERVER:
  1947. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
  1948. break;
  1949. }
  1950. }
  1951. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  1952. {
  1953. if (GetSystemMetrics(SM_SERVERR2))
  1954. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003 R2, "));
  1955. else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
  1956. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Storage Server 2003"));
  1957. else if (osvi.wSuiteMask == VER_SUITE_WH_SERVER)
  1958. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Home Server"));
  1959. else if (osvi.wProductType == VER_NT_WORKSTATION &&
  1960. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1961. {
  1962. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP Professional x64 Edition"));
  1963. }
  1964. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003, "));
  1965. // Test for the server type.
  1966. if (osvi.wProductType != VER_NT_WORKSTATION)
  1967. {
  1968. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1969. {
  1970. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1971. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems"));
  1972. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1973. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1974. }
  1975. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1976. {
  1977. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1978. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter x64 Edition"));
  1979. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1980. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise x64 Edition"));
  1981. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard x64 Edition"));
  1982. }
  1983. else
  1984. {
  1985. if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
  1986. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Compute Cluster Edition"));
  1987. else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1988. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1989. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1990. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1991. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1992. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Edition"));
  1993. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1994. }
  1995. }
  1996. }
  1997. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  1998. {
  1999. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP "));
  2000. if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  2001. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Edition"));
  2002. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
  2003. }
  2004. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  2005. {
  2006. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 2000 "));
  2007. if (osvi.wProductType == VER_NT_WORKSTATION)
  2008. {
  2009. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
  2010. }
  2011. else
  2012. {
  2013. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  2014. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Server"));
  2015. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  2016. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Advanced Server"));
  2017. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Server"));
  2018. }
  2019. }
  2020. // Include service pack (if any) and build number.
  2021. if (_tcslen(osvi.szCSDVersion) > 0)
  2022. {
  2023. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(" "));
  2024. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, osvi.szCSDVersion);
  2025. }
  2026. TCHAR buf[80];
  2027. StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
  2028. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, buf);
  2029. if (osvi.dwMajorVersion >= 6)
  2030. {
  2031. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  2032. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  2033. {
  2034. tagOSInfo.bIs64Bit = TRUE;
  2035. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 64-bit"));
  2036. }
  2037. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
  2038. {
  2039. tagOSInfo.bIs64Bit = FALSE;
  2040. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 32-bit"));
  2041. }
  2042. }
  2043. return TRUE;
  2044. }
  2045. else
  2046. {
  2047. _tprintf(_T("This sample does not support this version of Windows.\n"));
  2048. return FALSE;
  2049. }
  2050. }
  2051. };