underlyingapi.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804
  1. #include "stdafx.h"
  2. #include "underlyingapi.h"
  3. #include <shlobj.h>
  4. namespace underlyingapi
  5. {
  6. //-------------------------------------------
  7. // 系统定义;
  8. #define BUFSIZE 256
  9. #define PRODUCT_ULTIMATE 0x00000001
  10. #define PRODUCT_HOME_BASIC 0x00000002
  11. #define PRODUCT_HOME_PREMIUM 0x00000003
  12. #define PRODUCT_ENTERPRISE 0x00000004
  13. #define PRODUCT_HOME_BASIC_N 0x00000005
  14. #define PRODUCT_BUSINESS 0x00000006
  15. #define PRODUCT_STANDARD_SERVER 0x00000007
  16. #define PRODUCT_DATACENTER_SERVER 0x00000008
  17. #define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
  18. #define PRODUCT_ENTERPRISE_SERVER 0x0000000A
  19. #define PRODUCT_STARTER 0x0000000B
  20. #define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
  21. #define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
  22. #define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
  23. #define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
  24. #define PRODUCT_BUSINESS_N 0x00000010
  25. #define PRODUCT_WEB_SERVER 0x00000011
  26. #define PRODUCT_CLUSTER_SERVER 0x00000012
  27. #define PRODUCT_HOME_SERVER 0x00000013
  28. #define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
  29. #define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
  30. #define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
  31. #define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
  32. #define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
  33. #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
  34. #define PRODUCT_HOME_PREMIUM_N 0x0000001A
  35. #define PRODUCT_ENTERPRISE_N 0x0000001B
  36. #define PRODUCT_ULTIMATE_N 0x0000001C
  37. #define PRODUCT_WEB_SERVER_CORE 0x0000001D
  38. #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
  39. #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
  40. #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
  41. #define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021
  42. #define PRODUCT_HOME_PREMIUM_SERVER 0x00000022
  43. #define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023
  44. #define PRODUCT_STANDARD_SERVER_V 0x00000024
  45. #define PRODUCT_DATACENTER_SERVER_V 0x00000025
  46. #define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
  47. #define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
  48. #define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
  49. #define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
  50. #define PRODUCT_HYPERV 0x0000002A
  51. #define SM_TABLETPC 86
  52. #define SM_MEDIACENTER 87
  53. #define SM_STARTER 88
  54. #define SM_SERVERR2 89
  55. #define VER_SERVER_NT 0x80000000
  56. #define VER_WORKSTATION_NT 0x40000000
  57. #define VER_SUITE_SMALLBUSINESS 0x00000001
  58. #define VER_SUITE_ENTERPRISE 0x00000002
  59. #define VER_SUITE_BACKOFFICE 0x00000004
  60. #define VER_SUITE_COMMUNICATIONS 0x00000008
  61. #define VER_SUITE_TERMINAL 0x00000010
  62. #define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020
  63. #define VER_SUITE_EMBEDDEDNT 0x00000040
  64. #define VER_SUITE_DATACENTER 0x00000080
  65. #define VER_SUITE_SINGLEUSERTS 0x00000100
  66. #define VER_SUITE_PERSONAL 0x00000200
  67. #define VER_SUITE_BLADE 0x00000400
  68. #define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800
  69. #define VER_SUITE_SECURITY_APPLIANCE 0x00001000
  70. #define VER_SUITE_STORAGE_SERVER 0x00002000
  71. #define VER_SUITE_COMPUTE_SERVER 0x00004000
  72. #define VER_SUITE_WH_SERVER 0x00008000
  73. typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
  74. typedef BOOL(WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
  75. //-------------------------------------------
  76. static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
  77. {
  78. switch (uMsg)
  79. {
  80. case BFFM_INITIALIZED:
  81. SendMessage(hwnd, BFFM_SETSELECTION, TRUE, NULL);
  82. break;
  83. case BFFM_SELCHANGED:
  84. {
  85. TCHAR szFileName[MAX_PATH];
  86. LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
  87. SHGetPathFromIDList(pidlCurrent, szFileName);
  88. SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
  89. }
  90. break;
  91. // 这个不能让默认目录为桌面;
  92. // case BFFM_INITIALIZED:
  93. // {
  94. // // WParam is TRUE since you are passing a path.
  95. // // It would be FALSE if you were passing a pidl.
  96. // TCHAR szDir[MAX_PATH] = { 0 };
  97. // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
  98. // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
  99. // }
  100. // break;
  101. }
  102. return 0;
  103. }
  104. BOOL GetFilePath(TCHAR *FilePath, HWND hParentWnd, TCHAR* InstructionString, UINT ulFlags)
  105. {
  106. BROWSEINFO bInfo;
  107. LPITEMIDLIST pidl, pidlDesktop;
  108. ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
  109. SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
  110. bInfo.pidlRoot = pidlDesktop;
  111. bInfo.hwndOwner = hParentWnd;
  112. bInfo.pszDisplayName = FilePath;
  113. bInfo.lpszTitle = InstructionString;
  114. bInfo.ulFlags = ulFlags;
  115. bInfo.lpfn = BrowseCallbackProc;
  116. bInfo.lParam = 0;
  117. if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL) return FALSE;
  118. if (::SHGetPathFromIDList(pidl, FilePath) == FALSE) return FALSE;
  119. return TRUE;
  120. }
  121. BOOL extmerge(IN CONST TString& ext1, IN CONST TString& ext2, IN TString &merge) // 合并扩展名;
  122. {
  123. INT nRet = iscorrectext(ext1);
  124. if ( nRet == -1 ) return FALSE;
  125. if (nRet == 0)
  126. {
  127. merge = _T("*.*");
  128. return TRUE;
  129. }
  130. merge = ext1;
  131. nRet = iscorrectext(ext2);
  132. if (nRet != 1) return TRUE;
  133. if (merge[merge.length() - 1] != _T('|'))
  134. merge.append(_T("|"));
  135. merge.append(ext2);
  136. return TRUE;
  137. }
  138. INT iscorrectext(IN const TString &fext, OUT TString* lpMistakenExt /*= NULL*/)
  139. {
  140. if (fext.size() == 0) return FALSE;
  141. if (fext.find(_T("*.*")) != TString::npos) return 0;
  142. TString ext = lowercase(fext);
  143. if (ext[ext.length() - 1] != _T('|'))
  144. ext.append(_T("|"));
  145. INT nRet = 1;
  146. CONST TString sCorrectExt = _T("*.jpg|*.jpeg|*.png|*.bmp|*.cr2|*.nef|*.raw");
  147. int nIndex = 0;
  148. do
  149. {
  150. nIndex = ext.find(_T('|'));
  151. if (nIndex != TString::npos)
  152. {
  153. if (sCorrectExt.find(ext.substr(0, nIndex)) == TString::npos)
  154. {
  155. if (lpMistakenExt)
  156. *lpMistakenExt = ext.substr(0, nIndex);
  157. nRet = -1;
  158. break;
  159. }
  160. ext = ext.substr(nIndex + 1);
  161. }
  162. } while (ext.find(_T('|')) != TString::npos);
  163. return nRet;
  164. }
  165. TString lowercase(IN const TString &Str)
  166. {
  167. const TCHAR aazz = _T('z') - _T('Z');
  168. TString sResult;
  169. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  170. {
  171. if (_T('A') <= *it && *it <= _T('Z'))
  172. sResult.append(1, *it + aazz);
  173. else
  174. sResult.append(1, *it);
  175. }
  176. return sResult;
  177. }
  178. TString uppercase(IN const TString &Str)
  179. {
  180. const TCHAR aazz = _T('z') - _T('Z');
  181. TString sResult;
  182. for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
  183. {
  184. if (_T('a') <= *it && *it <= _T('z'))
  185. sResult.append(1, *it - aazz);
  186. else
  187. sResult.append(1, *it);
  188. }
  189. return sResult;
  190. }
  191. int match(IN CONST TString &sFileExt, IN CONST TString &sFile)
  192. {
  193. TString fext = uppercase(sFileExt);
  194. TString file = uppercase(sFile);
  195. int pos = file.find_last_of(_T('.'));
  196. if (TString::npos != pos)
  197. file = file.substr(pos);
  198. return (TString::npos != fext.find(file));
  199. }
  200. void GetDirveInfo()
  201. {
  202. UINT DiskType;
  203. size_t szAllDriveStrings = GetLogicalDriveStrings(0, NULL);
  204. TCHAR *pDriveStrings = new TCHAR[szAllDriveStrings + sizeof(_T(" "))];
  205. GetLogicalDriveStrings(szAllDriveStrings, pDriveStrings);
  206. size_t szDriveString = _tcslen(pDriveStrings);
  207. while (szDriveString > 0)
  208. {
  209. AfxMessageBox(pDriveStrings);
  210. DiskType = GetDriveType(pDriveStrings);
  211. switch (DiskType)
  212. {
  213. case DRIVE_NO_ROOT_DIR:
  214. return;
  215. case DRIVE_REMOVABLE:
  216. ::AfxMessageBox(_T("移动存储设备 "));
  217. break;
  218. case DRIVE_FIXED:
  219. ::AfxMessageBox(_T("固定硬盘驱动器 "));
  220. break;
  221. case DRIVE_REMOTE:
  222. ::AfxMessageBox(_T("这是网络驱动器 "));
  223. break;
  224. case DRIVE_CDROM:
  225. ::AfxMessageBox(_T("这是光盘驱动器 "));
  226. break;
  227. }
  228. pDriveStrings += szDriveString + 1;
  229. szDriveString = _tcslen(pDriveStrings);
  230. }
  231. }
  232. BOOL OpenFloder(IN CStringArray &vtFiles, IN LPCTSTR lpFilter )
  233. {
  234. // 获取桌面路径;
  235. TCHAR szDesktop[MAX_PATH];
  236. SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
  237. CString strFindFile = _T("");
  238. LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
  239. //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
  240. LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
  241. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
  242. CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
  243. dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
  244. dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
  245. // 最多可以打开nMaxFile个文件
  246. // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
  247. dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
  248. dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
  249. ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
  250. // 显示文件对话框,获得文件名集合;
  251. if (dlg.DoModal() == IDCANCEL)
  252. {
  253. delete[]dlg.m_ofn.lpstrFile;
  254. dlg.m_ofn.lpstrFile = NULL;
  255. return FALSE;
  256. }
  257. POSITION pos_file = dlg.GetStartPosition();
  258. while (pos_file != NULL)
  259. vtFiles.Add((LPCTSTR)dlg.GetNextPathName(pos_file));
  260. // 释放内存;
  261. delete[]dlg.m_ofn.lpstrFile;
  262. dlg.m_ofn.lpstrFile = NULL;
  263. return TRUE;
  264. }
  265. BOOL OpenFloder(IN STR_VEC &vtFiles, IN LPCTSTR lpFilter)
  266. {
  267. // 获取桌面路径;
  268. TCHAR szDesktop[MAX_PATH];
  269. SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
  270. CString strFindFile = _T("");
  271. LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
  272. //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
  273. LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
  274. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
  275. CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
  276. dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
  277. dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
  278. // 最多可以打开nMaxFile个文件
  279. // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
  280. dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
  281. dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
  282. ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
  283. // 显示文件对话框,获得文件名集合;
  284. if (dlg.DoModal() == IDCANCEL)
  285. {
  286. delete[]dlg.m_ofn.lpstrFile;
  287. dlg.m_ofn.lpstrFile = NULL;
  288. return FALSE;
  289. }
  290. POSITION pos_file = dlg.GetStartPosition();
  291. while (pos_file != NULL)
  292. vtFiles.push_back((LPCTSTR)dlg.GetNextPathName(pos_file));
  293. // 释放内存;
  294. delete[]dlg.m_ofn.lpstrFile;
  295. dlg.m_ofn.lpstrFile = NULL;
  296. return TRUE;
  297. }
  298. // 区分大小写;
  299. BOOL IsStringExist(IN CString& str, IN CStringArray &tagAry)
  300. {
  301. int nSize = tagAry.GetSize();
  302. if (nSize == 0) return FALSE;
  303. BOOL bExist = FALSE;
  304. for (int i = 0; i < nSize; i++)
  305. {
  306. if (str == tagAry.ElementAt(i))
  307. {
  308. bExist = TRUE;
  309. break;
  310. }
  311. }
  312. return bExist;
  313. }
  314. BOOL IsStringExist(IN TString& str, IN STR_VEC &tagVt)
  315. {
  316. int nSize = tagVt.size();
  317. if (nSize == 0) return FALSE;
  318. BOOL bExist = FALSE;
  319. for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  320. {
  321. if (str.compare(it->c_str()) == 0)
  322. {
  323. bExist = TRUE;
  324. break;
  325. }
  326. }
  327. return bExist;
  328. }
  329. // 不区分大小写;
  330. BOOL IsStringExistNoCase(IN CString& str, IN CStringArray &tagAry)
  331. {
  332. int nSize = tagAry.GetSize();
  333. if (nSize == 0) return FALSE;
  334. BOOL bExist = FALSE;
  335. for (int i = 0; i < nSize; i++)
  336. {
  337. if (str.CompareNoCase(tagAry.ElementAt(i)) == 0)
  338. {
  339. bExist = TRUE;
  340. break;
  341. }
  342. }
  343. return bExist;
  344. }
  345. BOOL IsStringExistNoCase(IN CONST TString& str, IN STR_VEC &tagVt)
  346. {
  347. int nSize = tagVt.size();
  348. if (nSize == 0) return FALSE;
  349. BOOL bExist = FALSE;
  350. TString stmp1 = uppercase(str);
  351. TString stmp2;
  352. for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
  353. {
  354. stmp2 = uppercase(*it);
  355. if (stmp1.compare(stmp2) == 0)
  356. {
  357. bExist = TRUE;
  358. break;
  359. }
  360. }
  361. return bExist;
  362. }
  363. /************************************************************************/
  364. /*
  365. 函数:IsValidpath
  366. 描述:判断一个路径是否是本地可以盘符或可用的网络路径;
  367. 参数:
  368. IN: strPath 要判断的路径;
  369. 返回:盘符或网络路径有效返回TRUE;
  370. */
  371. /************************************************************************/
  372. BOOL IsValidpath(IN CONST TString &strPath)
  373. {
  374. // 文件表现形式有三种:
  375. // 1.本地形式: C:\myfolder\you
  376. // 2.共享形式: \\192.168.1.112\myfolder\\you
  377. if (strPath.size() < 2) return FALSE;
  378. // 含":"为本地路径;
  379. if (strPath[1] == _T(':'))
  380. {
  381. DWORD dwDiskType = 0;
  382. dwDiskType = GetDriveType(strPath.substr(0, 3).c_str());
  383. switch (dwDiskType)
  384. {
  385. case DRIVE_UNKNOWN: // 未知驱动盘符;
  386. case DRIVE_NO_ROOT_DIR: // 没有根目录;
  387. //case DRIVE_REMOVABLE: // 移动存储设备盘符;
  388. // case DRIVE_FIXED: // 硬盘;
  389. case DRIVE_CDROM: // CD盘符;
  390. case DRIVE_RAMDISK: // ;
  391. return FALSE;
  392. break;
  393. }
  394. }
  395. else if (strPath.substr(0, 2).find(_T("\\\\")) != TString::npos)
  396. {
  397. // 共享路径;
  398. TString strtmp = strPath.substr(2);
  399. int n = strPath.substr(2).find(_T("\\"));
  400. if (n == TString::npos)
  401. return FALSE;
  402. strtmp = strPath.substr(0, n + 2);
  403. // 需要判断网络路径可用否;
  404. DWORD dwRet = 0;
  405. NETRESOURCE nr;
  406. memset(&nr, 0, sizeof(nr));
  407. nr.dwScope = RESOURCE_CONNECTED;
  408. nr.dwType = RESOURCETYPE_ANY;
  409. nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
  410. nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
  411. #ifndef UNICODE
  412. nr.lpRemoteName = (LPSTR)strtmp.c_str();
  413. #else
  414. nr.lpRemoteName = (LPWSTR)(strtmp.c_str());
  415. #endif
  416. dwRet = WNetAddConnection2(&nr, NULL, NULL, CONNECT_UPDATE_PROFILE);
  417. if (dwRet == ERROR_BAD_NETPATH)// 无效的网络路径,表示没有这个网络地址;
  418. return FALSE;
  419. else if (dwRet == ERROR_LOGON_FAILURE)// 登陆这个网络路径失败,因为当前用户名和密码错误; 没有权限;
  420. return FALSE;
  421. }
  422. return TRUE;
  423. }
  424. void Splitpath(IN CONST TCHAR *path, IN TCHAR * drive, IN size_t driveNumberOfElements, IN TCHAR * dir, IN size_t dirNumberOfElements, IN CONST int &nTimes)
  425. {
  426. int i = 0;
  427. int npoint = 0; // '.'
  428. int nsprit = 0; // '\\'
  429. int ncolon = 0; // ':'
  430. int ntimes = -1;
  431. #ifdef UNICODE // 使用_tcslen来代替wcslen和strlen有问题,在UNICODE时还没有问题,一但转成多字节后_tcslen就不准确;
  432. int nlen = wcslen(path);
  433. #else
  434. int nlen = strlen(path);
  435. #endif
  436. while (nlen > i++)
  437. {
  438. if (path[i] == _T(':'))
  439. ncolon = i;
  440. else if (path[i] == _T('\\'))
  441. {
  442. nsprit = i;
  443. if (nTimes == ++ntimes) break;
  444. }
  445. }
  446. //memcpy_s(drive, driveNumberOfElements*sizeof(TCHAR), path, (ncolon + 1)*sizeof(TCHAR));
  447. //memcpy_s(dir, dirNumberOfElements*sizeof(TCHAR), &path[ncolon + 1], (nsprit - ncolon)*sizeof(TCHAR));
  448. #ifdef UNICODE
  449. wmemcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
  450. wmemcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
  451. #else
  452. memcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
  453. memcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
  454. #endif
  455. }
  456. /************************************************************************/
  457. /*
  458. 函数:全局函数JCreateDirectory,多字节版本,支持UNICODE
  459. 描述:创建目录及子目录文件夹;
  460. 参数:
  461. lpDestDirectory:目录,必须是"E:\lyfzdb\数据\"格式,目录最后必须有"\",否则最后一个子目录无法创建!
  462. 建议使用系统_tsplitpath来分隔目录,这样会保存最后一个"\"斜杠;
  463. 返回:成功创建返回TRUE;
  464. */
  465. /************************************************************************/
  466. BOOL CreateDirectoryEx(IN LPCTSTR lpNewDirectory)
  467. {
  468. // 判断路径有效性
  469. if (!lpNewDirectory || !IsValidpath(lpNewDirectory)) return FALSE;
  470. BOOL bExists = FALSE;
  471. TCHAR szNewFileName[_MAX_PATH] = { 0 };
  472. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  473. TCHAR szDir[_MAX_DIR] = { 0 };
  474. TCHAR szFna[_MAX_FNAME] = { 0 };
  475. TCHAR szExt[_MAX_EXT] = { 0 };
  476. TCHAR szNewDirectory[_MAX_PATH] = { 0 };
  477. if (lpNewDirectory[_tcslen(lpNewDirectory) - 1] != _T('\\'))
  478. StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s\\"), lpNewDirectory);
  479. else
  480. StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s"), lpNewDirectory);
  481. int nIndex = 0;
  482. do
  483. {
  484. bExists = PathFileExists(szNewDirectory);
  485. if (!bExists)
  486. {
  487. //memset(szDrive, 0, _MAX_DRIVE*sizeof(TCHAR));
  488. //memset(szDir, 0, _MAX_DIR*sizeof(TCHAR));
  489. //memset(szFna, 0, _MAX_FNAME*sizeof(TCHAR));
  490. //memset(szExt, 0, _MAX_EXT*sizeof(TCHAR));
  491. #ifdef UNICODE
  492. wmemset(szDrive, 0, _MAX_DRIVE);
  493. wmemset(szDir, 0, _MAX_DIR);
  494. wmemset(szFna, 0, _MAX_FNAME);
  495. wmemset(szExt, 0, _MAX_EXT);
  496. #else
  497. memset(szDrive, 0, _MAX_DRIVE);
  498. memset(szDir, 0, _MAX_DIR);
  499. memset(szFna, 0, _MAX_FNAME);
  500. memset(szExt, 0, _MAX_EXT);
  501. #endif
  502. Splitpath(szNewDirectory, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, ++nIndex);
  503. StringCchPrintf(szNewFileName, _MAX_PATH, _T("%s%s"), szDrive, szDir);
  504. if (!CreateDirectory(szNewFileName, NULL))
  505. {
  506. DWORD dwError = GetLastError();
  507. switch (dwError)
  508. {
  509. case ERROR_PATH_NOT_FOUND: // 路径未找到,上一层目录未创建导致该错误;
  510. case ERROR_ACCESS_DENIED: // 没有权限访问;
  511. case ERROR_BAD_NET_NAME: // 错误的网络名称;
  512. //case ERROR_BAD_PATHNAME: //
  513. return FALSE;
  514. break;
  515. case ERROR_ALREADY_EXISTS: // 文件存在,不需要创建;
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. }
  522. } while (!bExists);
  523. return TRUE;
  524. }
  525. BOOL GetOrientation(IN Image *pImg)
  526. {
  527. if (pImg == NULL) return FALSE;
  528. UINT totalBufferSize;
  529. UINT numProperties;
  530. pImg->GetPropertySize(&totalBufferSize, &numProperties);
  531. // Allocate the buffer that will receive the property items.
  532. PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
  533. // Fill the buffer.
  534. pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
  535. // Print the id data member of each property item.
  536. for (UINT j = 0; j < numProperties; ++j)
  537. {
  538. if (PropertyTagOrientation == pAllItems[j].id)
  539. {
  540. short* ptrLong = (short*)(pAllItems[j].value);
  541. int ret = (int)*ptrLong;
  542. free(pAllItems);
  543. return ret;
  544. }
  545. }
  546. free(pAllItems);
  547. return TRUE;
  548. }
  549. /************************************************************************/
  550. /*
  551. 函数:GetEncoderClsid
  552. 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
  553. 参数:
  554. IN: format 要获取的图像格式;
  555. OUT: pClsid 返回符合条件的图像编码器信息;
  556. 返回:成功返回编码器索引,否则返回-1;
  557. */
  558. /************************************************************************/
  559. int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
  560. {
  561. // CD+支持的图像编码器数量;
  562. UINT numEncoders = 0;
  563. // 每个图像编码器以数组形式保存的大小;
  564. UINT nSize = 0;
  565. ImageCodecInfo* pImageCodecInfo = NULL;
  566. // 2.获取GDI+支持的图像格式编码器种类数以及ImageCodecInfo数组的存放大小
  567. GetImageEncodersSize(&numEncoders, &nSize);
  568. if (nSize == 0)
  569. return -1;
  570. //3.为ImageCodecInfo数组分配足额空间
  571. pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
  572. if (pImageCodecInfo == NULL)
  573. return -1;
  574. //4.获取所有的图像编码器信息
  575. GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
  576. //5.查找符合的图像编码器的Clsid
  577. for (UINT i = 0; i < numEncoders; ++i)
  578. {
  579. if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
  580. {
  581. *pClsid = pImageCodecInfo[i].Clsid;
  582. free(pImageCodecInfo);
  583. return i; // Success
  584. }
  585. }
  586. //6.释放步骤3分配的内存
  587. free(pImageCodecInfo);
  588. return -1;
  589. }
  590. /************************************************************************/
  591. /*
  592. 函数:SaveImg2newfile
  593. 描述:将Image对象另存为其他格式的文件;
  594. 参数:
  595. IN: pImg GDI+ Image对象指针,需要保存的图像对象;
  596. IN: lpnewfile 其他格式的新文件名(jpg,bmp,png,jpeg);
  597. IN: uQuality 另存为新文件时的图像质量值;
  598. 返回:
  599. 注意:Image::Save 若文件存在,会覆盖存在的文件;
  600. */
  601. /************************************************************************/
  602. BOOL SaveImg2newfile(IN Image* pImg, IN CString strNewfile, IN ULONG uQuality)
  603. {
  604. if (pImg == NULL) return FALSE;
  605. // 需要判断路径是否存在,不存在创建目录;
  606. int nIndex = strNewfile.ReverseFind(_T('\\'));
  607. if (nIndex == -1)
  608. return FALSE;
  609. if (!PathFileExists(strNewfile.Left(nIndex)))
  610. {
  611. // 如果文件夹不存在,创建;
  612. CreateDirectoryEx(strNewfile.Left(nIndex));
  613. }
  614. nIndex = strNewfile.ReverseFind(_T('.'));
  615. if (nIndex == -1)
  616. return FALSE;
  617. Status stat = GenericError;
  618. CLSID encoderClsid = { 0 };
  619. BSTR newfile = strNewfile.AllocSysString();
  620. strNewfile = strNewfile.Mid(nIndex + 1);
  621. if (strNewfile.CompareNoCase(_T("bmp")) == 0)
  622. {
  623. GetEncoderClsid(L"image/bmp", &encoderClsid);
  624. stat = pImg->Save(newfile, &encoderClsid, NULL);
  625. }
  626. else if (strNewfile.CompareNoCase(_T("png")) == 0)
  627. {
  628. GetEncoderClsid(L"image/png", &encoderClsid);
  629. stat = pImg->Save(newfile, &encoderClsid, NULL);
  630. }
  631. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  632. {
  633. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  634. EncoderParameters encoderParameters;
  635. encoderParameters.Count = 1;
  636. encoderParameters.Parameter[0].Guid = EncoderQuality;
  637. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  638. encoderParameters.Parameter[0].NumberOfValues = 1;
  639. // Save the image as a JPEG with quality level 100.
  640. encoderParameters.Parameter[0].Value = &uQuality;
  641. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  642. }
  643. SysFreeString(newfile);
  644. return stat == Ok ? TRUE : FALSE;
  645. }
  646. BOOL SaveImg2newfile(IN Image* pImg, IN TString strNewfile, IN ULONG uQuality)
  647. {
  648. if (pImg == NULL) return FALSE;
  649. // 需要判断路径是否存在,不存在创建目录;
  650. int nIndex = strNewfile.find_last_of(_T('\\'));
  651. if (nIndex == TString::npos)
  652. return FALSE;
  653. if (!PathFileExists(strNewfile.substr(0, nIndex).c_str()))
  654. {
  655. // 如果文件夹不存在,创建;
  656. CreateDirectoryEx(strNewfile.substr(0, nIndex).c_str());
  657. }
  658. nIndex = strNewfile.find_last_of(_T('.'));
  659. if (nIndex == TString::npos)
  660. return FALSE;
  661. Status stat = GenericError;
  662. CLSID encoderClsid = { 0 };
  663. BSTR newfile = _bstr_t(strNewfile.c_str());
  664. strNewfile = strNewfile.substr(nIndex + 1);
  665. if (strNewfile.find(_T("bmp")) == 0)
  666. {
  667. GetEncoderClsid(L"image/bmp", &encoderClsid);
  668. stat = pImg->Save(newfile, &encoderClsid, NULL);
  669. }
  670. else if (strNewfile.find(_T("png")) == 0)
  671. {
  672. GetEncoderClsid(L"image/png", &encoderClsid);
  673. stat = pImg->Save(newfile, &encoderClsid, NULL);
  674. }
  675. else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
  676. {
  677. GetEncoderClsid(L"image/jpeg", &encoderClsid);
  678. EncoderParameters encoderParameters;
  679. encoderParameters.Count = 1;
  680. encoderParameters.Parameter[0].Guid = EncoderQuality;
  681. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  682. encoderParameters.Parameter[0].NumberOfValues = 1;
  683. // Save the image as a JPEG with quality level 100.
  684. encoderParameters.Parameter[0].Value = &uQuality;
  685. stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
  686. }
  687. SysFreeString(newfile);
  688. return stat == Ok ? TRUE : FALSE;
  689. }
  690. /************************************************************************/
  691. /*
  692. 函数:ZoomImg
  693. 描述:缩放到指定大小的区域中,返回缩放后的尺寸;
  694. 参数:
  695. IN: Imgrc 图像的大小;
  696. IN: dwDisplayPix 图像要显示的区域的大小;
  697. OUT: dwZoomPix 图像在显示区域内缩放后的尺寸;
  698. 返回:
  699. 注意:dwZoomPix的尺寸必须在dwDisplayPix内;
  700. */
  701. /************************************************************************/
  702. int ZoomImg(IN CRect &Imgrc, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
  703. {
  704. double fDisplayWidth = GET_XPIX(dwDisplayPix);
  705. double fDisplayHeight = GET_YPIX(dwDisplayPix);
  706. // 显示区域长宽比;
  707. double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
  708. // 图片长宽比;
  709. double fImgAspectRatio = ((double)Imgrc.Width()) / ((double)Imgrc.Height());
  710. double fZoomWidth;
  711. double fZoomHeight;
  712. if (fDisplayAspectRatio > fImgAspectRatio)
  713. {
  714. fZoomWidth = fDisplayHeight*fImgAspectRatio;
  715. fZoomHeight = fDisplayHeight;
  716. }
  717. else
  718. {
  719. fZoomWidth = fDisplayWidth;
  720. fZoomHeight = fDisplayWidth / fImgAspectRatio;
  721. }
  722. dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
  723. //////////////////////////////////////////////////////////////////////////
  724. int nRetval = 0;
  725. if ((fDisplayWidth == Imgrc.Width()) && (fDisplayHeight == Imgrc.Height()))
  726. {
  727. nRetval = ZoomNull;
  728. }
  729. else if ((fDisplayWidth > Imgrc.Width()) && (fDisplayHeight > Imgrc.Height()))
  730. {
  731. nRetval = ZoomOut;
  732. }
  733. else
  734. {
  735. nRetval = ZoomIn;
  736. }
  737. return nRetval;
  738. }
  739. int ZoomImg(IN CONST DWORD &dwImgPix, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
  740. {
  741. double fDisplayWidth = GET_XPIX(dwDisplayPix);
  742. double fDisplayHeight = GET_YPIX(dwDisplayPix);
  743. // 显示区域长宽比;
  744. double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
  745. // 图片长宽比;
  746. double fImgAspectRatio = ((double)GET_XPIX(dwImgPix)) / ((double)GET_YPIX(dwImgPix));
  747. double fZoomWidth;
  748. double fZoomHeight;
  749. if (fDisplayAspectRatio > fImgAspectRatio)
  750. {
  751. fZoomWidth = fDisplayHeight*fImgAspectRatio;
  752. fZoomHeight = fDisplayHeight;
  753. }
  754. else
  755. {
  756. fZoomWidth = fDisplayWidth;
  757. fZoomHeight = fDisplayWidth / fImgAspectRatio;
  758. }
  759. dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
  760. //////////////////////////////////////////////////////////////////////////
  761. int nRetval = 0;
  762. if ((fDisplayWidth == GET_XPIX(dwImgPix)) && (fDisplayHeight == GET_YPIX(dwImgPix)))
  763. {
  764. nRetval = ZoomNull;
  765. }
  766. else if ((fDisplayWidth > GET_XPIX(dwImgPix)) && (fDisplayHeight > GET_YPIX(dwImgPix)))
  767. {
  768. nRetval = ZoomOut;
  769. }
  770. else
  771. {
  772. nRetval = ZoomIn;
  773. }
  774. return nRetval;
  775. }
  776. /************************************************************************/
  777. /*
  778. 函数:ImgThumbnail
  779. 描述:生成高质量的图像缩略图;
  780. 参数:
  781. 返回:
  782. 注意:为了健壮性,strNewfile的路径可以在函数内再次判断是否有效,防止在函数外没有进行经判断;
  783. */
  784. /************************************************************************/
  785. BOOL ImgThumbnail(IN Image* pImg, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
  786. {
  787. if (pImg == NULL) return FALSE;
  788. DWORD dwZoomPix;
  789. int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
  790. if (retval == ZoomOut)
  791. {
  792. return FALSE;
  793. }
  794. else if (retval == ZoomNull)
  795. {
  796. return SaveImg2newfile(pImg, strNewfile, uQuality);
  797. }
  798. // 绘制缩略图;
  799. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  800. Graphics *graphic = Graphics::FromImage(&bitmap);
  801. graphic->Clear(Color(255, 255, 255, 255));
  802. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  803. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  804. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  805. graphic->SetCompositingMode(CompositingModeSourceOver);
  806. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  807. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  808. graphic->DrawImage(pImg, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  809. delete graphic;
  810. // 生成缩略图;
  811. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  812. }
  813. BOOL ImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
  814. {
  815. if (!PathFileExists(lpImgpath))
  816. return FALSE;
  817. #ifdef UNICODE
  818. Image Img(lpImgpath);
  819. #else
  820. BSTR strtmp = _bstr_t(lpImgpath);
  821. Image Img(strtmp);
  822. SysFreeString(strtmp);
  823. #endif
  824. DWORD dwZoomPix;
  825. int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
  826. if (retval == ZoomOut)
  827. {
  828. return FALSE;
  829. }
  830. else if (retval == ZoomNull)
  831. {
  832. return SaveImg2newfile(&Img, strNewfile, uQuality);
  833. }
  834. // 绘制缩略图;
  835. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  836. Graphics *graphic = Graphics::FromImage(&bitmap);
  837. graphic->Clear(Color(255, 255, 255, 255));
  838. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  839. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  840. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  841. graphic->SetCompositingMode(CompositingModeSourceOver);
  842. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  843. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  844. graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  845. delete graphic;
  846. // 生成缩略图;
  847. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  848. }
  849. BOOL ImgThumbnail(IN CONST TString &strImgpath, IN CONST DWORD &dwDisplayPix, IN TString strNewfile, IN ULONG uQuality)
  850. {
  851. if (!PathFileExists(strImgpath.c_str()))
  852. return FALSE;
  853. #ifdef UNICODE
  854. BSTR strtmp = _bstr_t(strImgpath.c_str());
  855. Image Img(strtmp);
  856. SysFreeString(strtmp);
  857. //Image Img(strImgpath);
  858. #else
  859. BSTR strtmp = _bstr_t(strImgpath.c_str());
  860. Image Img(strtmp);
  861. SysFreeString(strtmp);
  862. #endif
  863. DWORD dwZoomPix;
  864. int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
  865. if (retval == ZoomOut)
  866. {
  867. return FALSE;
  868. }
  869. else if (retval == ZoomNull)
  870. {
  871. return SaveImg2newfile(&Img, strNewfile, uQuality);
  872. }
  873. // 绘制缩略图;
  874. Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  875. Graphics *graphic = Graphics::FromImage(&bitmap);
  876. graphic->Clear(Color(255, 255, 255, 255));
  877. graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
  878. graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
  879. graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
  880. graphic->SetCompositingMode(CompositingModeSourceOver);
  881. graphic->SetCompositingQuality(CompositingQualityHighQuality);
  882. graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
  883. graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  884. delete graphic;
  885. // 生成缩略图;
  886. return SaveImg2newfile(&bitmap, strNewfile, uQuality);
  887. }
  888. BOOL SimpleImgThumbnail(IN LPCWSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
  889. {
  890. if (!PathFileExistsW(lpImgpath))
  891. return FALSE;
  892. DWORD dwZoomPix;
  893. Image Img(lpImgpath);
  894. int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
  895. if (retval == ZoomOut)
  896. {
  897. return FALSE;
  898. }
  899. else if (retval == ZoomNull)
  900. {
  901. return SaveImg2newfile(&Img, strNewfile, uQuality);
  902. }
  903. Image *pThumbnailImg = Img.GetThumbnailImage(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
  904. if (pThumbnailImg)
  905. SaveImg2newfile(pThumbnailImg, strNewfile, uQuality);
  906. else
  907. return FALSE;
  908. if (pThumbnailImg) delete pThumbnailImg;
  909. return TRUE;
  910. }
  911. /************************************************************************/
  912. /*
  913. 函数:GetDiskSpaceInfo
  914. 描述:获取指定目录或盘符的容量信息;
  915. 参数:
  916. IN: lpRootPathName 根目录名称;
  917. INOUT: dTotalNum 总容量;
  918. INOUT: dFreeNum 可用容量;
  919. 返回:
  920. 注意:
  921. GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G);
  922. GetDiskFreeSpaceEx支持64位(16384P=1024*16384T)
  923. 能获取本机硬盘信息,但路径必须包含":"冒号, 同时还可以获取网络路径的共享硬盘大小;
  924. 调用约定:
  925. */
  926. /************************************************************************/
  927. BOOL GetDiskSpaceInfo(IN LPCTSTR lpRootPathName, IN OUT double &dTotalNum, IN OUT double &dFreeNum)
  928. {
  929. #if 1
  930. // 校验参数有效性;
  931. if (lpRootPathName == NULL || _tcscmp(lpRootPathName, _T("")) == 0)
  932. {
  933. //_tprintf_s(_T("根目录为空!"));
  934. return FALSE;
  935. }
  936. // 如果长度==1;
  937. int nlen = _tcslen(lpRootPathName);
  938. if ((1 == nlen) || (2 == nlen && lpRootPathName[nlen - 1] != _T(':')))
  939. {
  940. //_tprintf_s(_T("根目录无效!\n"));
  941. return FALSE;
  942. }
  943. if (!PathFileExists(lpRootPathName))
  944. {
  945. //_tprintf_s(_T("根目录不存在!\n"));
  946. return FALSE;
  947. }
  948. #else
  949. // 使用IsValipath判断网络路径时较慢;
  950. if (!IsValidpath(lpRootPathName) || !PathFileExists(lpRootPathName)) return FALSE;
  951. #endif
  952. BOOL fResult = FALSE;
  953. DWORD dwSectPerClust; // 每簇的扇区数;
  954. DWORD dwBytesPerSect; // 每个扇区的字节数;
  955. DWORD dwFreeClusters; // 可用簇数;
  956. DWORD dwTotalClusters; // 总簇数;
  957. typedef BOOL(WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
  958. P_GDFSE pGetDiskFreeSpaceEx = NULL;
  959. /*unsigned*/ __int64 i64FreeBytesToCaller;
  960. /*unsigned*/ __int64 i64TotalBytes;
  961. /*unsigned*/ __int64 i64FreeBytes;
  962. // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace;
  963. #ifdef UNICODE
  964. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExW");
  965. #else
  966. pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
  967. #endif
  968. if (pGetDiskFreeSpaceEx)
  969. {
  970. fResult = pGetDiskFreeSpaceEx(lpRootPathName,
  971. (PULARGE_INTEGER)&i64FreeBytesToCaller,
  972. (PULARGE_INTEGER)&i64TotalBytes,
  973. (PULARGE_INTEGER)&i64FreeBytes);
  974. if (fResult)
  975. {
  976. // 将单位由byte转为G byte;
  977. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  978. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  979. }
  980. }
  981. else
  982. {
  983. fResult = GetDiskFreeSpace(lpRootPathName,
  984. &dwSectPerClust,
  985. &dwBytesPerSect,
  986. &dwFreeClusters,
  987. &dwTotalClusters);
  988. if (fResult)
  989. {
  990. // 成功获得容量信息;
  991. i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
  992. i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
  993. // 将单位由byte转为G byte;
  994. dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
  995. dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
  996. }
  997. }
  998. return fResult;
  999. }
  1000. // 是否运行在Wow64下;XP SP2以上系统才支持;
  1001. BOOL IsWow64()
  1002. {
  1003. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  1004. LPFN_ISWOW64PROCESS fnIsWow64Process;
  1005. BOOL bIsWow64 = FALSE;
  1006. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  1007. if (NULL != fnIsWow64Process)
  1008. {
  1009. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
  1010. {
  1011. // 句柄错误,则系统不是64位;
  1012. AfxMessageBox(_T("句柄错误"));
  1013. }
  1014. }
  1015. return bIsWow64;
  1016. }
  1017. // 2.判断系统是64位还是32位;
  1018. BOOL IsOSVerIs64()
  1019. {
  1020. BOOL bOSVerIs64 = FALSE;
  1021. SYSTEM_INFO si;
  1022. PGNSI pGNSI;
  1023. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1024. if (NULL != pGNSI)
  1025. pGNSI(&si);
  1026. else
  1027. GetSystemInfo(&si);
  1028. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  1029. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1030. {
  1031. bOSVerIs64 = TRUE;
  1032. }
  1033. return bOSVerIs64;
  1034. }
  1035. // 判断进程是否运行在Wow64下;
  1036. INT IsWow64Process(HANDLE hprocess)
  1037. {
  1038. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  1039. LPFN_ISWOW64PROCESS fnIsWow64Process;
  1040. INT nRet = 0;
  1041. BOOL bIsWow64 = FALSE;
  1042. fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  1043. if (NULL != fnIsWow64Process)
  1044. {
  1045. if (!fnIsWow64Process(hprocess, &bIsWow64))
  1046. {
  1047. // 句柄错误;
  1048. AfxMessageBox(_T("句柄错误"));
  1049. DWORD dwError = GetLastError();
  1050. return -1;
  1051. }
  1052. }
  1053. return bIsWow64 ? 1 : 0;
  1054. }
  1055. INT GetNativeSystemInfo(OSVerInfo &tagOSInfo)
  1056. {
  1057. OSVERSIONINFOEX osvi;
  1058. SYSTEM_INFO si;
  1059. PGNSI pGNSI;
  1060. BOOL bOsVersionInfoEx;
  1061. ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1062. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  1063. // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
  1064. // If that fails, try using the OSVERSIONINFO structure.
  1065. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1066. if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
  1067. {
  1068. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1069. if (!GetVersionEx((OSVERSIONINFO *)&osvi))
  1070. return FALSE;
  1071. }
  1072. // Call GetNativeSystemInfo if supported
  1073. // or GetSystemInfo otherwise.
  1074. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1075. if (NULL != pGNSI)
  1076. pGNSI(&si);
  1077. else
  1078. GetSystemInfo(&si);
  1079. TCHAR szWindowsDescription[MAX_PATH] = { 0 };
  1080. CString str;
  1081. CString strWindows;
  1082. switch (osvi.dwPlatformId)
  1083. {
  1084. case VER_PLATFORM_WIN32_NT:
  1085. if (osvi.dwMajorVersion == 10) // Windows10或Windows Server预览版;
  1086. {
  1087. if (osvi.dwMinorVersion == 0)
  1088. {
  1089. if (osvi.wProductType == VER_NT_WORKSTATION)
  1090. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 10 "));
  1091. else
  1092. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server Technical Preview "));
  1093. }
  1094. }
  1095. if (osvi.dwMajorVersion == 6) // Windows Vista ~ Windows 8.1
  1096. {
  1097. if (osvi.dwMinorVersion == 3)
  1098. {
  1099. if (osvi.wProductType == VER_NT_WORKSTATION)
  1100. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8.1 "));
  1101. else
  1102. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 R2 "));
  1103. }
  1104. if (osvi.dwMinorVersion == 2)
  1105. {
  1106. if (osvi.wProductType == VER_NT_WORKSTATION)
  1107. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8 "));
  1108. else
  1109. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 "));
  1110. }
  1111. if (osvi.dwMinorVersion == 1)
  1112. {
  1113. if (osvi.wProductType == VER_NT_WORKSTATION)
  1114. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 7 "));
  1115. else
  1116. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 R2 "));
  1117. }
  1118. if (osvi.dwMinorVersion == 0)
  1119. {
  1120. if (osvi.wProductType == VER_NT_WORKSTATION)
  1121. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Vista "));
  1122. else
  1123. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 "));
  1124. }
  1125. }
  1126. if (osvi.dwMajorVersion == 5)
  1127. {
  1128. if (osvi.dwMinorVersion == 2)
  1129. {
  1130. if (GetSystemMetrics(SM_SERVERR2) != 0)
  1131. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 R2 "));
  1132. else
  1133. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 "));
  1134. if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
  1135. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Home Server "));
  1136. if ((osvi.wProductType == VER_NT_WORKSTATION) && (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
  1137. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP Professional x64 Edition "));
  1138. }
  1139. if (osvi.dwMinorVersion == 1)
  1140. {
  1141. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP "));
  1142. }
  1143. if (osvi.dwMinorVersion == 0)
  1144. {
  1145. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 2000 "));
  1146. }
  1147. }
  1148. if (osvi.dwMajorVersion <= 4)
  1149. {
  1150. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows NT "));
  1151. }
  1152. // Test for specific product on Windows NT 4.0 SP6 and later.
  1153. // 获取Windows NT 4.0 SP6之后的版本;
  1154. if (bOsVersionInfoEx)
  1155. {
  1156. // Test for the workstation type.
  1157. if (osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_AMD64)
  1158. {
  1159. if (osvi.dwMajorVersion == 4)
  1160. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation 4.0 "));
  1161. else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  1162. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Home Edition "));
  1163. else
  1164. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Professional "));
  1165. }
  1166. // Test for the server type.
  1167. else if (osvi.wProductType == VER_NT_SERVER || osvi.wProductType == VER_NT_DOMAIN_CONTROLLER)
  1168. {
  1169. if (osvi.dwMajorVersion == 6)
  1170. {
  1171. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1172. {
  1173. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1174. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
  1175. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1176. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
  1177. }
  1178. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1179. {
  1180. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1181. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
  1182. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1183. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
  1184. else
  1185. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
  1186. }
  1187. else
  1188. {
  1189. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1190. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
  1191. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1192. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
  1193. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1194. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
  1195. else
  1196. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
  1197. }
  1198. }
  1199. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  1200. {
  1201. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1202. {
  1203. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1204. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
  1205. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1206. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
  1207. }
  1208. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1209. {
  1210. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1211. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
  1212. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1213. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
  1214. else
  1215. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
  1216. }
  1217. else
  1218. {
  1219. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1220. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
  1221. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1222. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
  1223. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1224. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
  1225. else
  1226. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
  1227. }
  1228. }
  1229. else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  1230. {
  1231. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1232. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Server "));
  1233. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1234. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
  1235. else
  1236. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
  1237. }
  1238. else if (osvi.dwMajorVersion == 4) // Windows NT 4.0
  1239. {
  1240. if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1241. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0, Enterprise Edition "));
  1242. else
  1243. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0 "));
  1244. }
  1245. }
  1246. }
  1247. // Test for specific product on Windows NT 4.0 SP5 and earlier
  1248. else
  1249. {
  1250. HKEY hKey;
  1251. TCHAR szProductType[BUFSIZE];
  1252. DWORD dwBufLen = BUFSIZE*sizeof(TCHAR);
  1253. LONG lRet;
  1254. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 0, KEY_QUERY_VALUE, &hKey);
  1255. if (lRet != ERROR_SUCCESS)
  1256. return FALSE;
  1257. lRet = RegQueryValueEx(hKey, TEXT("ProductType"), NULL, NULL, (LPBYTE)szProductType, &dwBufLen);
  1258. RegCloseKey(hKey);
  1259. if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE*sizeof(TCHAR)))
  1260. return FALSE;
  1261. if (lstrcmpi(TEXT("WINNT"), szProductType) == 0)
  1262. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation "));
  1263. if (lstrcmpi(TEXT("LANMANNT"), szProductType) == 0)
  1264. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
  1265. if (lstrcmpi(TEXT("SERVERNT"), szProductType) == 0)
  1266. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
  1267. //printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
  1268. }
  1269. // Display service pack (if any) and build number.
  1270. if (osvi.dwMajorVersion == 4 && lstrcmpi(osvi.szCSDVersion, TEXT("Service Pack 6")) == 0)
  1271. {
  1272. HKEY hKey;
  1273. LONG lRet;
  1274. // Test for SP6 versus SP6a.
  1275. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"), 0, KEY_QUERY_VALUE, &hKey);
  1276. if (lRet == ERROR_SUCCESS)
  1277. str.Format(_T("Service Pack 6a (Build %d)"), osvi.dwBuildNumber & 0xFFFF);
  1278. else // Windows NT 4.0 prior to SP6a
  1279. {
  1280. str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  1281. }
  1282. RegCloseKey(hKey);
  1283. }
  1284. else // not Windows NT 4.0
  1285. {
  1286. str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
  1287. }
  1288. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, str);
  1289. break;
  1290. // Test for the Windows Me/98/95.
  1291. case VER_PLATFORM_WIN32_WINDOWS:
  1292. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
  1293. {
  1294. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 95 "));
  1295. if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
  1296. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("OSR2 "));
  1297. }
  1298. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
  1299. {
  1300. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 98 "));
  1301. if (osvi.szCSDVersion[1] == 'A' || osvi.szCSDVersion[1] == 'B')
  1302. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("SE "));
  1303. }
  1304. if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
  1305. {
  1306. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Millennium Edition"));
  1307. }
  1308. break;
  1309. case VER_PLATFORM_WIN32s:
  1310. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Win32s"));
  1311. break;
  1312. }
  1313. AfxMessageBox(strWindows);
  1314. return 0;
  1315. }
  1316. BOOL GetSystemInfo(OSVerInfo &tagOSInfo)
  1317. {
  1318. OSVERSIONINFOEX osvi;
  1319. SYSTEM_INFO si;
  1320. PGNSI pGNSI;
  1321. PGPI pGPI;
  1322. BOOL bOsVersionInfoEx;
  1323. DWORD dwType;
  1324. ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1325. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  1326. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1327. if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
  1328. {
  1329. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  1330. if (!GetVersionEx((OSVERSIONINFO *)&osvi))
  1331. return FALSE;
  1332. }
  1333. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1334. if (NULL != pGNSI)
  1335. pGNSI(&si);
  1336. else
  1337. GetSystemInfo(&si);
  1338. tagOSInfo.dwMajorVersion = osvi.dwMajorVersion;
  1339. tagOSInfo.dwMinorVersion = osvi.dwMinorVersion;
  1340. tagOSInfo.wProductType = osvi.wProductType;
  1341. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
  1342. {
  1343. StringCchCopy(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Microsoft "));
  1344. // Test for the specific product.
  1345. if (osvi.dwMajorVersion == 10)
  1346. {
  1347. if (osvi.dwMinorVersion == 0)
  1348. {
  1349. if (osvi.wProductType == VER_NT_WORKSTATION)
  1350. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 10 "));
  1351. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server Technical Preview "));
  1352. }
  1353. pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  1354. pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
  1355. switch (dwType)
  1356. {
  1357. case PRODUCT_ULTIMATE:
  1358. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
  1359. break;
  1360. case PRODUCT_HOME_PREMIUM:
  1361. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
  1362. break;
  1363. case PRODUCT_HOME_BASIC:
  1364. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
  1365. break;
  1366. case PRODUCT_ENTERPRISE:
  1367. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1368. break;
  1369. case PRODUCT_BUSINESS:
  1370. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
  1371. break;
  1372. case PRODUCT_STARTER:
  1373. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
  1374. break;
  1375. case PRODUCT_CLUSTER_SERVER:
  1376. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
  1377. break;
  1378. case PRODUCT_DATACENTER_SERVER:
  1379. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1380. break;
  1381. case PRODUCT_DATACENTER_SERVER_CORE:
  1382. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
  1383. break;
  1384. case PRODUCT_ENTERPRISE_SERVER:
  1385. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1386. break;
  1387. case PRODUCT_ENTERPRISE_SERVER_CORE:
  1388. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
  1389. break;
  1390. case PRODUCT_ENTERPRISE_SERVER_IA64:
  1391. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1392. break;
  1393. case PRODUCT_SMALLBUSINESS_SERVER:
  1394. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
  1395. break;
  1396. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  1397. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
  1398. break;
  1399. case PRODUCT_STANDARD_SERVER:
  1400. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1401. break;
  1402. case PRODUCT_STANDARD_SERVER_CORE:
  1403. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
  1404. break;
  1405. case PRODUCT_WEB_SERVER:
  1406. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
  1407. break;
  1408. }
  1409. }
  1410. if (osvi.dwMajorVersion == 6)
  1411. {
  1412. if (osvi.dwMinorVersion == 0)
  1413. {
  1414. if (osvi.wProductType == VER_NT_WORKSTATION)
  1415. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Vista "));
  1416. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 "));
  1417. }
  1418. if (osvi.dwMinorVersion == 1)
  1419. {
  1420. if (osvi.wProductType == VER_NT_WORKSTATION)
  1421. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 7 "));
  1422. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 R2 "));
  1423. }
  1424. if (osvi.dwMinorVersion == 2)
  1425. {
  1426. if (osvi.wProductType == VER_NT_WORKSTATION)
  1427. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8 "));
  1428. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 "));
  1429. }
  1430. if (osvi.dwMinorVersion == 3)
  1431. {
  1432. if (osvi.wProductType == VER_NT_WORKSTATION)
  1433. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8.1 "));
  1434. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 R2 "));
  1435. }
  1436. pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  1437. pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
  1438. switch (dwType)
  1439. {
  1440. case PRODUCT_ULTIMATE:
  1441. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
  1442. break;
  1443. case PRODUCT_HOME_PREMIUM:
  1444. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
  1445. break;
  1446. case PRODUCT_HOME_BASIC:
  1447. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
  1448. break;
  1449. case PRODUCT_ENTERPRISE:
  1450. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1451. break;
  1452. case PRODUCT_BUSINESS:
  1453. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
  1454. break;
  1455. case PRODUCT_STARTER:
  1456. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
  1457. break;
  1458. case PRODUCT_CLUSTER_SERVER:
  1459. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
  1460. break;
  1461. case PRODUCT_DATACENTER_SERVER:
  1462. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1463. break;
  1464. case PRODUCT_DATACENTER_SERVER_CORE:
  1465. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
  1466. break;
  1467. case PRODUCT_ENTERPRISE_SERVER:
  1468. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1469. break;
  1470. case PRODUCT_ENTERPRISE_SERVER_CORE:
  1471. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
  1472. break;
  1473. case PRODUCT_ENTERPRISE_SERVER_IA64:
  1474. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1475. break;
  1476. case PRODUCT_SMALLBUSINESS_SERVER:
  1477. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
  1478. break;
  1479. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  1480. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
  1481. break;
  1482. case PRODUCT_STANDARD_SERVER:
  1483. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1484. break;
  1485. case PRODUCT_STANDARD_SERVER_CORE:
  1486. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
  1487. break;
  1488. case PRODUCT_WEB_SERVER:
  1489. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
  1490. break;
  1491. }
  1492. }
  1493. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  1494. {
  1495. if (GetSystemMetrics(SM_SERVERR2))
  1496. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003 R2, "));
  1497. else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
  1498. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Storage Server 2003"));
  1499. else if (osvi.wSuiteMask == VER_SUITE_WH_SERVER)
  1500. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Home Server"));
  1501. else if (osvi.wProductType == VER_NT_WORKSTATION &&
  1502. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1503. {
  1504. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP Professional x64 Edition"));
  1505. }
  1506. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003, "));
  1507. // Test for the server type.
  1508. if (osvi.wProductType != VER_NT_WORKSTATION)
  1509. {
  1510. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1511. {
  1512. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1513. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems"));
  1514. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1515. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1516. }
  1517. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1518. {
  1519. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1520. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter x64 Edition"));
  1521. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1522. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise x64 Edition"));
  1523. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard x64 Edition"));
  1524. }
  1525. else
  1526. {
  1527. if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
  1528. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Compute Cluster Edition"));
  1529. else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1530. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
  1531. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1532. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
  1533. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1534. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Edition"));
  1535. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
  1536. }
  1537. }
  1538. }
  1539. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  1540. {
  1541. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP "));
  1542. if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  1543. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Edition"));
  1544. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
  1545. }
  1546. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  1547. {
  1548. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 2000 "));
  1549. if (osvi.wProductType == VER_NT_WORKSTATION)
  1550. {
  1551. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
  1552. }
  1553. else
  1554. {
  1555. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1556. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Server"));
  1557. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1558. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Advanced Server"));
  1559. else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Server"));
  1560. }
  1561. }
  1562. // Include service pack (if any) and build number.
  1563. if (_tcslen(osvi.szCSDVersion) > 0)
  1564. {
  1565. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(" "));
  1566. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, osvi.szCSDVersion);
  1567. }
  1568. TCHAR buf[80];
  1569. StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
  1570. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, buf);
  1571. if (osvi.dwMajorVersion >= 6)
  1572. {
  1573. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
  1574. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1575. {
  1576. tagOSInfo.bIs64Bit = TRUE;
  1577. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 64-bit"));
  1578. }
  1579. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
  1580. {
  1581. tagOSInfo.bIs64Bit = FALSE;
  1582. StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 32-bit"));
  1583. }
  1584. }
  1585. return TRUE;
  1586. }
  1587. else
  1588. {
  1589. _tprintf(_T("This sample does not support this version of Windows.\n"));
  1590. return FALSE;
  1591. }
  1592. }
  1593. };