1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804 |
- #include "stdafx.h"
- #include "underlyingapi.h"
- #include <shlobj.h>
- namespace underlyingapi
- {
- //-------------------------------------------
- // 系统定义;
- #define BUFSIZE 256
- #define PRODUCT_ULTIMATE 0x00000001
- #define PRODUCT_HOME_BASIC 0x00000002
- #define PRODUCT_HOME_PREMIUM 0x00000003
- #define PRODUCT_ENTERPRISE 0x00000004
- #define PRODUCT_HOME_BASIC_N 0x00000005
- #define PRODUCT_BUSINESS 0x00000006
- #define PRODUCT_STANDARD_SERVER 0x00000007
- #define PRODUCT_DATACENTER_SERVER 0x00000008
- #define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
- #define PRODUCT_ENTERPRISE_SERVER 0x0000000A
- #define PRODUCT_STARTER 0x0000000B
- #define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
- #define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
- #define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
- #define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
- #define PRODUCT_BUSINESS_N 0x00000010
- #define PRODUCT_WEB_SERVER 0x00000011
- #define PRODUCT_CLUSTER_SERVER 0x00000012
- #define PRODUCT_HOME_SERVER 0x00000013
- #define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
- #define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
- #define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
- #define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
- #define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
- #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
- #define PRODUCT_HOME_PREMIUM_N 0x0000001A
- #define PRODUCT_ENTERPRISE_N 0x0000001B
- #define PRODUCT_ULTIMATE_N 0x0000001C
- #define PRODUCT_WEB_SERVER_CORE 0x0000001D
- #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
- #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
- #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
- #define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021
- #define PRODUCT_HOME_PREMIUM_SERVER 0x00000022
- #define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023
- #define PRODUCT_STANDARD_SERVER_V 0x00000024
- #define PRODUCT_DATACENTER_SERVER_V 0x00000025
- #define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
- #define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
- #define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
- #define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
- #define PRODUCT_HYPERV 0x0000002A
- #define SM_TABLETPC 86
- #define SM_MEDIACENTER 87
- #define SM_STARTER 88
- #define SM_SERVERR2 89
- #define VER_SERVER_NT 0x80000000
- #define VER_WORKSTATION_NT 0x40000000
- #define VER_SUITE_SMALLBUSINESS 0x00000001
- #define VER_SUITE_ENTERPRISE 0x00000002
- #define VER_SUITE_BACKOFFICE 0x00000004
- #define VER_SUITE_COMMUNICATIONS 0x00000008
- #define VER_SUITE_TERMINAL 0x00000010
- #define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020
- #define VER_SUITE_EMBEDDEDNT 0x00000040
- #define VER_SUITE_DATACENTER 0x00000080
- #define VER_SUITE_SINGLEUSERTS 0x00000100
- #define VER_SUITE_PERSONAL 0x00000200
- #define VER_SUITE_BLADE 0x00000400
- #define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800
- #define VER_SUITE_SECURITY_APPLIANCE 0x00001000
- #define VER_SUITE_STORAGE_SERVER 0x00002000
- #define VER_SUITE_COMPUTE_SERVER 0x00004000
- #define VER_SUITE_WH_SERVER 0x00008000
- typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
- typedef BOOL(WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
- //-------------------------------------------
- static INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
- {
- switch (uMsg)
- {
- case BFFM_INITIALIZED:
- SendMessage(hwnd, BFFM_SETSELECTION, TRUE, NULL);
- break;
- case BFFM_SELCHANGED:
- {
- TCHAR szFileName[MAX_PATH];
- LPITEMIDLIST pidlCurrent = (LPITEMIDLIST)lp;
- SHGetPathFromIDList(pidlCurrent, szFileName);
- SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szFileName);
- }
- break;
- // 这个不能让默认目录为桌面;
- // case BFFM_INITIALIZED:
- // {
- // // WParam is TRUE since you are passing a path.
- // // It would be FALSE if you were passing a pidl.
- // TCHAR szDir[MAX_PATH] = { 0 };
- // GetCurrentDirectory(sizeof(szDir) / sizeof(TCHAR), szDir);
- // SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
- // }
- // break;
- }
- return 0;
- }
- BOOL GetFilePath(TCHAR *FilePath, HWND hParentWnd, TCHAR* InstructionString, UINT ulFlags)
- {
- BROWSEINFO bInfo;
- LPITEMIDLIST pidl, pidlDesktop;
- ZeroMemory((PVOID)&bInfo, sizeof(BROWSEINFO));
- SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidlDesktop);
- bInfo.pidlRoot = pidlDesktop;
- bInfo.hwndOwner = hParentWnd;
- bInfo.pszDisplayName = FilePath;
- bInfo.lpszTitle = InstructionString;
- bInfo.ulFlags = ulFlags;
- bInfo.lpfn = BrowseCallbackProc;
- bInfo.lParam = 0;
- if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL) return FALSE;
- if (::SHGetPathFromIDList(pidl, FilePath) == FALSE) return FALSE;
- return TRUE;
- }
- BOOL extmerge(IN CONST TString& ext1, IN CONST TString& ext2, IN TString &merge) // 合并扩展名;
- {
- INT nRet = iscorrectext(ext1);
- if ( nRet == -1 ) return FALSE;
- if (nRet == 0)
- {
- merge = _T("*.*");
- return TRUE;
- }
- merge = ext1;
- nRet = iscorrectext(ext2);
- if (nRet != 1) return TRUE;
- if (merge[merge.length() - 1] != _T('|'))
- merge.append(_T("|"));
- merge.append(ext2);
- return TRUE;
- }
- INT iscorrectext(IN const TString &fext, OUT TString* lpMistakenExt /*= NULL*/)
- {
- if (fext.size() == 0) return FALSE;
- if (fext.find(_T("*.*")) != TString::npos) return 0;
- TString ext = lowercase(fext);
- if (ext[ext.length() - 1] != _T('|'))
- ext.append(_T("|"));
- INT nRet = 1;
- CONST TString sCorrectExt = _T("*.jpg|*.jpeg|*.png|*.bmp|*.cr2|*.nef|*.raw");
- int nIndex = 0;
- do
- {
- nIndex = ext.find(_T('|'));
- if (nIndex != TString::npos)
- {
- if (sCorrectExt.find(ext.substr(0, nIndex)) == TString::npos)
- {
- if (lpMistakenExt)
- *lpMistakenExt = ext.substr(0, nIndex);
- nRet = -1;
- break;
- }
- ext = ext.substr(nIndex + 1);
- }
- } while (ext.find(_T('|')) != TString::npos);
- return nRet;
- }
- TString lowercase(IN const TString &Str)
- {
- const TCHAR aazz = _T('z') - _T('Z');
- TString sResult;
- for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
- {
- if (_T('A') <= *it && *it <= _T('Z'))
- sResult.append(1, *it + aazz);
- else
- sResult.append(1, *it);
- }
- return sResult;
- }
- TString uppercase(IN const TString &Str)
- {
- const TCHAR aazz = _T('z') - _T('Z');
- TString sResult;
- for (TString::const_iterator it = Str.begin(); Str.end() != it; it++)
- {
- if (_T('a') <= *it && *it <= _T('z'))
- sResult.append(1, *it - aazz);
- else
- sResult.append(1, *it);
- }
- return sResult;
- }
- int match(IN CONST TString &sFileExt, IN CONST TString &sFile)
- {
- TString fext = uppercase(sFileExt);
- TString file = uppercase(sFile);
- int pos = file.find_last_of(_T('.'));
- if (TString::npos != pos)
- file = file.substr(pos);
- return (TString::npos != fext.find(file));
- }
- void GetDirveInfo()
- {
- UINT DiskType;
- size_t szAllDriveStrings = GetLogicalDriveStrings(0, NULL);
- TCHAR *pDriveStrings = new TCHAR[szAllDriveStrings + sizeof(_T(" "))];
- GetLogicalDriveStrings(szAllDriveStrings, pDriveStrings);
- size_t szDriveString = _tcslen(pDriveStrings);
- while (szDriveString > 0)
- {
- AfxMessageBox(pDriveStrings);
- DiskType = GetDriveType(pDriveStrings);
- switch (DiskType)
- {
- case DRIVE_NO_ROOT_DIR:
- return;
- case DRIVE_REMOVABLE:
- ::AfxMessageBox(_T("移动存储设备 "));
- break;
- case DRIVE_FIXED:
- ::AfxMessageBox(_T("固定硬盘驱动器 "));
- break;
- case DRIVE_REMOTE:
- ::AfxMessageBox(_T("这是网络驱动器 "));
- break;
- case DRIVE_CDROM:
- ::AfxMessageBox(_T("这是光盘驱动器 "));
- break;
- }
- pDriveStrings += szDriveString + 1;
- szDriveString = _tcslen(pDriveStrings);
- }
- }
- BOOL OpenFloder(IN CStringArray &vtFiles, IN LPCTSTR lpFilter )
- {
- // 获取桌面路径;
- TCHAR szDesktop[MAX_PATH];
- SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
- CString strFindFile = _T("");
- LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
- //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
- LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
- DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
- CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
- dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
- dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
- // 最多可以打开nMaxFile个文件
- // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
- dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
- dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
- ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
- // 显示文件对话框,获得文件名集合;
- if (dlg.DoModal() == IDCANCEL)
- {
- delete[]dlg.m_ofn.lpstrFile;
- dlg.m_ofn.lpstrFile = NULL;
- return FALSE;
- }
- POSITION pos_file = dlg.GetStartPosition();
- while (pos_file != NULL)
- vtFiles.Add((LPCTSTR)dlg.GetNextPathName(pos_file));
- // 释放内存;
- delete[]dlg.m_ofn.lpstrFile;
- dlg.m_ofn.lpstrFile = NULL;
- return TRUE;
- }
- BOOL OpenFloder(IN STR_VEC &vtFiles, IN LPCTSTR lpFilter)
- {
- // 获取桌面路径;
- TCHAR szDesktop[MAX_PATH];
- SHGetSpecialFolderPath(0, szDesktop, CSIDL_DESKTOPDIRECTORY, 0);
- CString strFindFile = _T("");
- LPTSTR lpszFile = (TCHAR*)(CONST TCHAR*)strFindFile;
- //LPTSTR lpszFilter = _T("图片 (*.jpg)|*.jpg|图片 (*.jpeg)|*.jpeg|图片 (*.raw)|*.raw|图片 (*.cr2)|*.cr2|图片 (*.nef)|*.nef|所有文件 (*.*)|*.*||");
- LPTSTR lpszFilter = _T("图片 (*.jpg;*.jpeg;*.raw;*.cr2;*.nef)||所有文件 (*.*)|*.*||");
- DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING;
- CFileDialog dlg(TRUE, _T("default Files (*.jpg)"), NULL, dwFlags, lpszFilter, NULL);
- dlg.m_ofn.lpstrTitle = _T("请选择要导入的图片"); // 显示的标题, 应该做成参数传入;
- dlg.m_ofn.lpstrInitialDir = szDesktop; // 默认打开的文件夹, 应该做成参数传入;
- // 最多可以打开nMaxFile个文件
- // 注意,这里的nMaxFile其实不是真正的个数,而是lpstrFile数组的长度,每个文件最大长度为MAX_PATH,所以个数是n*MAX_PATH;
- dlg.m_ofn.nMaxFile = 1500 * MAX_PATH;
- dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
- ZeroMemory(dlg.m_ofn.lpstrFile, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);
- // 显示文件对话框,获得文件名集合;
- if (dlg.DoModal() == IDCANCEL)
- {
- delete[]dlg.m_ofn.lpstrFile;
- dlg.m_ofn.lpstrFile = NULL;
- return FALSE;
- }
- POSITION pos_file = dlg.GetStartPosition();
- while (pos_file != NULL)
- vtFiles.push_back((LPCTSTR)dlg.GetNextPathName(pos_file));
- // 释放内存;
- delete[]dlg.m_ofn.lpstrFile;
- dlg.m_ofn.lpstrFile = NULL;
- return TRUE;
- }
- // 区分大小写;
- BOOL IsStringExist(IN CString& str, IN CStringArray &tagAry)
- {
- int nSize = tagAry.GetSize();
- if (nSize == 0) return FALSE;
- BOOL bExist = FALSE;
- for (int i = 0; i < nSize; i++)
- {
- if (str == tagAry.ElementAt(i))
- {
- bExist = TRUE;
- break;
- }
- }
- return bExist;
- }
- BOOL IsStringExist(IN TString& str, IN STR_VEC &tagVt)
- {
- int nSize = tagVt.size();
- if (nSize == 0) return FALSE;
- BOOL bExist = FALSE;
- for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
- {
- if (str.compare(it->c_str()) == 0)
- {
- bExist = TRUE;
- break;
- }
- }
- return bExist;
- }
- // 不区分大小写;
- BOOL IsStringExistNoCase(IN CString& str, IN CStringArray &tagAry)
- {
- int nSize = tagAry.GetSize();
- if (nSize == 0) return FALSE;
- BOOL bExist = FALSE;
- for (int i = 0; i < nSize; i++)
- {
- if (str.CompareNoCase(tagAry.ElementAt(i)) == 0)
- {
- bExist = TRUE;
- break;
- }
- }
- return bExist;
- }
- BOOL IsStringExistNoCase(IN CONST TString& str, IN STR_VEC &tagVt)
- {
- int nSize = tagVt.size();
- if (nSize == 0) return FALSE;
- BOOL bExist = FALSE;
- TString stmp1 = uppercase(str);
- TString stmp2;
- for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
- {
- stmp2 = uppercase(*it);
- if (stmp1.compare(stmp2) == 0)
- {
- bExist = TRUE;
- break;
- }
- }
- return bExist;
- }
- /************************************************************************/
- /*
- 函数:IsValidpath
- 描述:判断一个路径是否是本地可以盘符或可用的网络路径;
- 参数:
- IN: strPath 要判断的路径;
- 返回:盘符或网络路径有效返回TRUE;
- */
- /************************************************************************/
- BOOL IsValidpath(IN CONST TString &strPath)
- {
- // 文件表现形式有三种:
- // 1.本地形式: C:\myfolder\you
- // 2.共享形式: \\192.168.1.112\myfolder\\you
- if (strPath.size() < 2) return FALSE;
- // 含":"为本地路径;
- if (strPath[1] == _T(':'))
- {
- DWORD dwDiskType = 0;
- dwDiskType = GetDriveType(strPath.substr(0, 3).c_str());
- switch (dwDiskType)
- {
- case DRIVE_UNKNOWN: // 未知驱动盘符;
- case DRIVE_NO_ROOT_DIR: // 没有根目录;
- //case DRIVE_REMOVABLE: // 移动存储设备盘符;
- // case DRIVE_FIXED: // 硬盘;
- case DRIVE_CDROM: // CD盘符;
- case DRIVE_RAMDISK: // ;
- return FALSE;
- break;
- }
- }
- else if (strPath.substr(0, 2).find(_T("\\\\")) != TString::npos)
- {
- // 共享路径;
- TString strtmp = strPath.substr(2);
- int n = strPath.substr(2).find(_T("\\"));
- if (n == TString::npos)
- return FALSE;
- strtmp = strPath.substr(0, n + 2);
- // 需要判断网络路径可用否;
- DWORD dwRet = 0;
- NETRESOURCE nr;
- memset(&nr, 0, sizeof(nr));
- nr.dwScope = RESOURCE_CONNECTED;
- nr.dwType = RESOURCETYPE_ANY;
- nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
- nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
- #ifndef UNICODE
- nr.lpRemoteName = (LPSTR)strtmp.c_str();
- #else
- nr.lpRemoteName = (LPWSTR)(strtmp.c_str());
- #endif
- dwRet = WNetAddConnection2(&nr, NULL, NULL, CONNECT_UPDATE_PROFILE);
- if (dwRet == ERROR_BAD_NETPATH)// 无效的网络路径,表示没有这个网络地址;
- return FALSE;
- else if (dwRet == ERROR_LOGON_FAILURE)// 登陆这个网络路径失败,因为当前用户名和密码错误; 没有权限;
- return FALSE;
- }
- return TRUE;
- }
- void Splitpath(IN CONST TCHAR *path, IN TCHAR * drive, IN size_t driveNumberOfElements, IN TCHAR * dir, IN size_t dirNumberOfElements, IN CONST int &nTimes)
- {
- int i = 0;
- int npoint = 0; // '.'
- int nsprit = 0; // '\\'
- int ncolon = 0; // ':'
- int ntimes = -1;
- #ifdef UNICODE // 使用_tcslen来代替wcslen和strlen有问题,在UNICODE时还没有问题,一但转成多字节后_tcslen就不准确;
- int nlen = wcslen(path);
- #else
- int nlen = strlen(path);
- #endif
- while (nlen > i++)
- {
- if (path[i] == _T(':'))
- ncolon = i;
- else if (path[i] == _T('\\'))
- {
- nsprit = i;
- if (nTimes == ++ntimes) break;
- }
- }
- //memcpy_s(drive, driveNumberOfElements*sizeof(TCHAR), path, (ncolon + 1)*sizeof(TCHAR));
- //memcpy_s(dir, dirNumberOfElements*sizeof(TCHAR), &path[ncolon + 1], (nsprit - ncolon)*sizeof(TCHAR));
- #ifdef UNICODE
- wmemcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
- wmemcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
- #else
- memcpy_s(drive, driveNumberOfElements, path, ncolon + 1);
- memcpy_s(dir, dirNumberOfElements, &path[ncolon + 1], nsprit - ncolon);
- #endif
- }
- /************************************************************************/
- /*
- 函数:全局函数JCreateDirectory,多字节版本,支持UNICODE
- 描述:创建目录及子目录文件夹;
- 参数:
- lpDestDirectory:目录,必须是"E:\lyfzdb\数据\"格式,目录最后必须有"\",否则最后一个子目录无法创建!
- 建议使用系统_tsplitpath来分隔目录,这样会保存最后一个"\"斜杠;
- 返回:成功创建返回TRUE;
- */
- /************************************************************************/
- BOOL CreateDirectoryEx(IN LPCTSTR lpNewDirectory)
- {
- // 判断路径有效性
- if (!lpNewDirectory || !IsValidpath(lpNewDirectory)) return FALSE;
- BOOL bExists = FALSE;
- TCHAR szNewFileName[_MAX_PATH] = { 0 };
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_FNAME] = { 0 };
- TCHAR szExt[_MAX_EXT] = { 0 };
- TCHAR szNewDirectory[_MAX_PATH] = { 0 };
- if (lpNewDirectory[_tcslen(lpNewDirectory) - 1] != _T('\\'))
- StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s\\"), lpNewDirectory);
- else
- StringCchPrintf(szNewDirectory, _MAX_PATH, _T("%s"), lpNewDirectory);
- int nIndex = 0;
- do
- {
- bExists = PathFileExists(szNewDirectory);
- if (!bExists)
- {
- //memset(szDrive, 0, _MAX_DRIVE*sizeof(TCHAR));
- //memset(szDir, 0, _MAX_DIR*sizeof(TCHAR));
- //memset(szFna, 0, _MAX_FNAME*sizeof(TCHAR));
- //memset(szExt, 0, _MAX_EXT*sizeof(TCHAR));
- #ifdef UNICODE
- wmemset(szDrive, 0, _MAX_DRIVE);
- wmemset(szDir, 0, _MAX_DIR);
- wmemset(szFna, 0, _MAX_FNAME);
- wmemset(szExt, 0, _MAX_EXT);
- #else
- memset(szDrive, 0, _MAX_DRIVE);
- memset(szDir, 0, _MAX_DIR);
- memset(szFna, 0, _MAX_FNAME);
- memset(szExt, 0, _MAX_EXT);
- #endif
- Splitpath(szNewDirectory, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, ++nIndex);
- StringCchPrintf(szNewFileName, _MAX_PATH, _T("%s%s"), szDrive, szDir);
- if (!CreateDirectory(szNewFileName, NULL))
- {
- DWORD dwError = GetLastError();
- switch (dwError)
- {
- case ERROR_PATH_NOT_FOUND: // 路径未找到,上一层目录未创建导致该错误;
- case ERROR_ACCESS_DENIED: // 没有权限访问;
- case ERROR_BAD_NET_NAME: // 错误的网络名称;
- //case ERROR_BAD_PATHNAME: //
- return FALSE;
- break;
- case ERROR_ALREADY_EXISTS: // 文件存在,不需要创建;
- break;
- default:
- break;
- }
- }
- }
- } while (!bExists);
- return TRUE;
- }
- BOOL GetOrientation(IN Image *pImg)
- {
- if (pImg == NULL) return FALSE;
- UINT totalBufferSize;
- UINT numProperties;
- pImg->GetPropertySize(&totalBufferSize, &numProperties);
- // Allocate the buffer that will receive the property items.
- PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
- // Fill the buffer.
- pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
- // Print the id data member of each property item.
- for (UINT j = 0; j < numProperties; ++j)
- {
- if (PropertyTagOrientation == pAllItems[j].id)
- {
- short* ptrLong = (short*)(pAllItems[j].value);
- int ret = (int)*ptrLong;
- free(pAllItems);
- return ret;
- }
- }
- free(pAllItems);
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:GetEncoderClsid
- 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
- 参数:
- IN: format 要获取的图像格式;
- OUT: pClsid 返回符合条件的图像编码器信息;
- 返回:成功返回编码器索引,否则返回-1;
- */
- /************************************************************************/
- int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
- {
- // CD+支持的图像编码器数量;
- UINT numEncoders = 0;
- // 每个图像编码器以数组形式保存的大小;
- UINT nSize = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- // 2.获取GDI+支持的图像格式编码器种类数以及ImageCodecInfo数组的存放大小
- GetImageEncodersSize(&numEncoders, &nSize);
- if (nSize == 0)
- return -1;
- //3.为ImageCodecInfo数组分配足额空间
- pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
- if (pImageCodecInfo == NULL)
- return -1;
- //4.获取所有的图像编码器信息
- GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
- //5.查找符合的图像编码器的Clsid
- for (UINT i = 0; i < numEncoders; ++i)
- {
- if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[i].Clsid;
- free(pImageCodecInfo);
- return i; // Success
- }
- }
- //6.释放步骤3分配的内存
- free(pImageCodecInfo);
- return -1;
- }
- /************************************************************************/
- /*
- 函数:SaveImg2newfile
- 描述:将Image对象另存为其他格式的文件;
- 参数:
- IN: pImg GDI+ Image对象指针,需要保存的图像对象;
- IN: lpnewfile 其他格式的新文件名(jpg,bmp,png,jpeg);
- IN: uQuality 另存为新文件时的图像质量值;
- 返回:
- 注意:Image::Save 若文件存在,会覆盖存在的文件;
- */
- /************************************************************************/
- BOOL SaveImg2newfile(IN Image* pImg, IN CString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.ReverseFind(_T('\\'));
- if (nIndex == -1)
- return FALSE;
- if (!PathFileExists(strNewfile.Left(nIndex)))
- {
- // 如果文件夹不存在,创建;
- CreateDirectoryEx(strNewfile.Left(nIndex));
- }
- nIndex = strNewfile.ReverseFind(_T('.'));
- if (nIndex == -1)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = strNewfile.AllocSysString();
- strNewfile = strNewfile.Mid(nIndex + 1);
- if (strNewfile.CompareNoCase(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.CompareNoCase(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- BOOL SaveImg2newfile(IN Image* pImg, IN TString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.find_last_of(_T('\\'));
- if (nIndex == TString::npos)
- return FALSE;
- if (!PathFileExists(strNewfile.substr(0, nIndex).c_str()))
- {
- // 如果文件夹不存在,创建;
- CreateDirectoryEx(strNewfile.substr(0, nIndex).c_str());
- }
- nIndex = strNewfile.find_last_of(_T('.'));
- if (nIndex == TString::npos)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = _bstr_t(strNewfile.c_str());
- strNewfile = strNewfile.substr(nIndex + 1);
- if (strNewfile.find(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.find(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- /************************************************************************/
- /*
- 函数:ZoomImg
- 描述:缩放到指定大小的区域中,返回缩放后的尺寸;
- 参数:
- IN: Imgrc 图像的大小;
- IN: dwDisplayPix 图像要显示的区域的大小;
- OUT: dwZoomPix 图像在显示区域内缩放后的尺寸;
- 返回:
- 注意:dwZoomPix的尺寸必须在dwDisplayPix内;
- */
- /************************************************************************/
- int ZoomImg(IN CRect &Imgrc, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
- {
- double fDisplayWidth = GET_XPIX(dwDisplayPix);
- double fDisplayHeight = GET_YPIX(dwDisplayPix);
- // 显示区域长宽比;
- double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
- // 图片长宽比;
- double fImgAspectRatio = ((double)Imgrc.Width()) / ((double)Imgrc.Height());
- double fZoomWidth;
- double fZoomHeight;
- if (fDisplayAspectRatio > fImgAspectRatio)
- {
- fZoomWidth = fDisplayHeight*fImgAspectRatio;
- fZoomHeight = fDisplayHeight;
- }
- else
- {
- fZoomWidth = fDisplayWidth;
- fZoomHeight = fDisplayWidth / fImgAspectRatio;
- }
- dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
- //////////////////////////////////////////////////////////////////////////
- int nRetval = 0;
- if ((fDisplayWidth == Imgrc.Width()) && (fDisplayHeight == Imgrc.Height()))
- {
- nRetval = ZoomNull;
- }
- else if ((fDisplayWidth > Imgrc.Width()) && (fDisplayHeight > Imgrc.Height()))
- {
- nRetval = ZoomOut;
- }
- else
- {
- nRetval = ZoomIn;
- }
- return nRetval;
- }
- int ZoomImg(IN CONST DWORD &dwImgPix, IN CONST DWORD &dwDisplayPix, OUT DWORD &dwZoomPix)
- {
- double fDisplayWidth = GET_XPIX(dwDisplayPix);
- double fDisplayHeight = GET_YPIX(dwDisplayPix);
- // 显示区域长宽比;
- double fDisplayAspectRatio = fDisplayWidth / fDisplayHeight;
- // 图片长宽比;
- double fImgAspectRatio = ((double)GET_XPIX(dwImgPix)) / ((double)GET_YPIX(dwImgPix));
- double fZoomWidth;
- double fZoomHeight;
- if (fDisplayAspectRatio > fImgAspectRatio)
- {
- fZoomWidth = fDisplayHeight*fImgAspectRatio;
- fZoomHeight = fDisplayHeight;
- }
- else
- {
- fZoomWidth = fDisplayWidth;
- fZoomHeight = fDisplayWidth / fImgAspectRatio;
- }
- dwZoomPix = SET_PIX((int)fZoomWidth, (int)fZoomHeight);
- //////////////////////////////////////////////////////////////////////////
- int nRetval = 0;
- if ((fDisplayWidth == GET_XPIX(dwImgPix)) && (fDisplayHeight == GET_YPIX(dwImgPix)))
- {
- nRetval = ZoomNull;
- }
- else if ((fDisplayWidth > GET_XPIX(dwImgPix)) && (fDisplayHeight > GET_YPIX(dwImgPix)))
- {
- nRetval = ZoomOut;
- }
- else
- {
- nRetval = ZoomIn;
- }
- return nRetval;
- }
- /************************************************************************/
- /*
- 函数:ImgThumbnail
- 描述:生成高质量的图像缩略图;
- 参数:
- 返回:
- 注意:为了健壮性,strNewfile的路径可以在函数内再次判断是否有效,防止在函数外没有进行经判断;
- */
- /************************************************************************/
- BOOL ImgThumbnail(IN Image* pImg, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
- {
- if (pImg == NULL) return FALSE;
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(pImg->GetWidth(), pImg->GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(pImg, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- graphic->Clear(Color(255, 255, 255, 255));
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- graphic->SetCompositingMode(CompositingModeSourceOver);
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- graphic->DrawImage(pImg, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL ImgThumbnail(IN LPCTSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
- {
- if (!PathFileExists(lpImgpath))
- return FALSE;
- #ifdef UNICODE
- Image Img(lpImgpath);
- #else
- BSTR strtmp = _bstr_t(lpImgpath);
- Image Img(strtmp);
- SysFreeString(strtmp);
- #endif
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(&Img, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- graphic->Clear(Color(255, 255, 255, 255));
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- graphic->SetCompositingMode(CompositingModeSourceOver);
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL ImgThumbnail(IN CONST TString &strImgpath, IN CONST DWORD &dwDisplayPix, IN TString strNewfile, IN ULONG uQuality)
- {
- if (!PathFileExists(strImgpath.c_str()))
- return FALSE;
- #ifdef UNICODE
- BSTR strtmp = _bstr_t(strImgpath.c_str());
- Image Img(strtmp);
- SysFreeString(strtmp);
- //Image Img(strImgpath);
- #else
- BSTR strtmp = _bstr_t(strImgpath.c_str());
- Image Img(strtmp);
- SysFreeString(strtmp);
- #endif
- DWORD dwZoomPix;
- int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(&Img, strNewfile, uQuality);
- }
- // 绘制缩略图;
- Bitmap bitmap(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- Graphics *graphic = Graphics::FromImage(&bitmap);
- graphic->Clear(Color(255, 255, 255, 255));
- graphic->SetInterpolationMode(InterpolationModeHighQualityBicubic);
- graphic->SetSmoothingMode(SmoothingModeHighQuality);//SmoothingModeHighSpeed
- graphic->SetPixelOffsetMode(PixelOffsetModeHighQuality);
- graphic->SetCompositingMode(CompositingModeSourceOver);
- graphic->SetCompositingQuality(CompositingQualityHighQuality);
- graphic->SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
- graphic->DrawImage(&Img, 0, 0, GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- delete graphic;
- // 生成缩略图;
- return SaveImg2newfile(&bitmap, strNewfile, uQuality);
- }
- BOOL SimpleImgThumbnail(IN LPCWSTR lpImgpath, IN CONST DWORD &dwDisplayPix, IN CString strNewfile, IN ULONG uQuality)
- {
- if (!PathFileExistsW(lpImgpath))
- return FALSE;
- DWORD dwZoomPix;
- Image Img(lpImgpath);
- int retval = ZoomImg(SET_PIX(Img.GetWidth(), Img.GetHeight()), dwDisplayPix, dwZoomPix);
- if (retval == ZoomOut)
- {
- return FALSE;
- }
- else if (retval == ZoomNull)
- {
- return SaveImg2newfile(&Img, strNewfile, uQuality);
- }
- Image *pThumbnailImg = Img.GetThumbnailImage(GET_XPIX(dwZoomPix), GET_YPIX(dwZoomPix));
- if (pThumbnailImg)
- SaveImg2newfile(pThumbnailImg, strNewfile, uQuality);
- else
- return FALSE;
- if (pThumbnailImg) delete pThumbnailImg;
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:GetDiskSpaceInfo
- 描述:获取指定目录或盘符的容量信息;
- 参数:
- IN: lpRootPathName 根目录名称;
- INOUT: dTotalNum 总容量;
- INOUT: dFreeNum 可用容量;
- 返回:
- 注意:
- GetDiskFreeSpace函数不能满足当前超过2T的硬盘容量,因为里面的数据类型是UINT,最大支持32位(4G);
- GetDiskFreeSpaceEx支持64位(16384P=1024*16384T)
- 能获取本机硬盘信息,但路径必须包含":"冒号, 同时还可以获取网络路径的共享硬盘大小;
- 调用约定:
- */
- /************************************************************************/
- BOOL GetDiskSpaceInfo(IN LPCTSTR lpRootPathName, IN OUT double &dTotalNum, IN OUT double &dFreeNum)
- {
- #if 1
- // 校验参数有效性;
- if (lpRootPathName == NULL || _tcscmp(lpRootPathName, _T("")) == 0)
- {
- //_tprintf_s(_T("根目录为空!"));
- return FALSE;
- }
- // 如果长度==1;
- int nlen = _tcslen(lpRootPathName);
- if ((1 == nlen) || (2 == nlen && lpRootPathName[nlen - 1] != _T(':')))
- {
- //_tprintf_s(_T("根目录无效!\n"));
- return FALSE;
- }
- if (!PathFileExists(lpRootPathName))
- {
- //_tprintf_s(_T("根目录不存在!\n"));
- return FALSE;
- }
- #else
- // 使用IsValipath判断网络路径时较慢;
- if (!IsValidpath(lpRootPathName) || !PathFileExists(lpRootPathName)) return FALSE;
- #endif
- BOOL fResult = FALSE;
- DWORD dwSectPerClust; // 每簇的扇区数;
- DWORD dwBytesPerSect; // 每个扇区的字节数;
- DWORD dwFreeClusters; // 可用簇数;
- DWORD dwTotalClusters; // 总簇数;
- typedef BOOL(WINAPI *P_GDFSE)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
- P_GDFSE pGetDiskFreeSpaceEx = NULL;
- /*unsigned*/ __int64 i64FreeBytesToCaller;
- /*unsigned*/ __int64 i64TotalBytes;
- /*unsigned*/ __int64 i64FreeBytes;
- // 调用GetDiskFreeSpaceEx失败则调用GetDiskSpace;
- #ifdef UNICODE
- pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExW");
- #else
- pGetDiskFreeSpaceEx = (P_GDFSE)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA");
- #endif
- if (pGetDiskFreeSpaceEx)
- {
- fResult = pGetDiskFreeSpaceEx(lpRootPathName,
- (PULARGE_INTEGER)&i64FreeBytesToCaller,
- (PULARGE_INTEGER)&i64TotalBytes,
- (PULARGE_INTEGER)&i64FreeBytes);
- if (fResult)
- {
- // 将单位由byte转为G byte;
- dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
- dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
- }
- }
- else
- {
- fResult = GetDiskFreeSpace(lpRootPathName,
- &dwSectPerClust,
- &dwBytesPerSect,
- &dwFreeClusters,
- &dwTotalClusters);
- if (fResult)
- {
- // 成功获得容量信息;
- i64TotalBytes = (__int64)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
- i64FreeBytes = (__int64)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
- // 将单位由byte转为G byte;
- dTotalNum = (double)i64TotalBytes / 1024 / 1024 / 1024;
- dFreeNum = (double)i64FreeBytes / 1024 / 1024 / 1024;
- }
- }
- return fResult;
- }
- // 是否运行在Wow64下;XP SP2以上系统才支持;
- BOOL IsWow64()
- {
- typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process;
- BOOL bIsWow64 = FALSE;
- fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
- {
- // 句柄错误,则系统不是64位;
- AfxMessageBox(_T("句柄错误"));
- }
- }
- return bIsWow64;
- }
- // 2.判断系统是64位还是32位;
- BOOL IsOSVerIs64()
- {
- BOOL bOSVerIs64 = FALSE;
- SYSTEM_INFO si;
- PGNSI pGNSI;
- pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- if (NULL != pGNSI)
- pGNSI(&si);
- else
- GetSystemInfo(&si);
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
- si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- bOSVerIs64 = TRUE;
- }
- return bOSVerIs64;
- }
- // 判断进程是否运行在Wow64下;
- INT IsWow64Process(HANDLE hprocess)
- {
- typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process;
- INT nRet = 0;
- BOOL bIsWow64 = FALSE;
- fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(hprocess, &bIsWow64))
- {
- // 句柄错误;
- AfxMessageBox(_T("句柄错误"));
- DWORD dwError = GetLastError();
- return -1;
- }
- }
- return bIsWow64 ? 1 : 0;
- }
- INT GetNativeSystemInfo(OSVerInfo &tagOSInfo)
- {
- OSVERSIONINFOEX osvi;
- SYSTEM_INFO si;
- PGNSI pGNSI;
- BOOL bOsVersionInfoEx;
- ZeroMemory(&si, sizeof(SYSTEM_INFO));
- ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
- // If that fails, try using the OSVERSIONINFO structure.
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
- {
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- if (!GetVersionEx((OSVERSIONINFO *)&osvi))
- return FALSE;
- }
- // Call GetNativeSystemInfo if supported
- // or GetSystemInfo otherwise.
- pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- if (NULL != pGNSI)
- pGNSI(&si);
- else
- GetSystemInfo(&si);
- TCHAR szWindowsDescription[MAX_PATH] = { 0 };
- CString str;
- CString strWindows;
- switch (osvi.dwPlatformId)
- {
- case VER_PLATFORM_WIN32_NT:
- if (osvi.dwMajorVersion == 10) // Windows10或Windows Server预览版;
- {
- if (osvi.dwMinorVersion == 0)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 10 "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server Technical Preview "));
- }
- }
- if (osvi.dwMajorVersion == 6) // Windows Vista ~ Windows 8.1
- {
- if (osvi.dwMinorVersion == 3)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8.1 "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 R2 "));
- }
- if (osvi.dwMinorVersion == 2)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 8 "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2012 "));
- }
- if (osvi.dwMinorVersion == 1)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 7 "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 R2 "));
- }
- if (osvi.dwMinorVersion == 0)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Vista "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2008 "));
- }
- }
- if (osvi.dwMajorVersion == 5)
- {
- if (osvi.dwMinorVersion == 2)
- {
- if (GetSystemMetrics(SM_SERVERR2) != 0)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 R2 "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Server 2003 "));
- if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Home Server "));
- if ((osvi.wProductType == VER_NT_WORKSTATION) && (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP Professional x64 Edition "));
- }
- if (osvi.dwMinorVersion == 1)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows XP "));
- }
- if (osvi.dwMinorVersion == 0)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 2000 "));
- }
- }
- if (osvi.dwMajorVersion <= 4)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows NT "));
- }
- // Test for specific product on Windows NT 4.0 SP6 and later.
- // 获取Windows NT 4.0 SP6之后的版本;
- if (bOsVersionInfoEx)
- {
- // Test for the workstation type.
- if (osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_AMD64)
- {
- if (osvi.dwMajorVersion == 4)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation 4.0 "));
- else if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Home Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Professional "));
- }
- // Test for the server type.
- else if (osvi.wProductType == VER_NT_SERVER || osvi.wProductType == VER_NT_DOMAIN_CONTROLLER)
- {
- if (osvi.dwMajorVersion == 6)
- {
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
- }
- else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
- }
- else
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_BLADE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
- }
- }
- else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
- {
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition for Itanium-based Systems"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition for Itanium-based Systems"));
- }
- else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter x64 Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise x64 Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard x64 Edition "));
- }
- else
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Enterprise Edition "));
- else if (osvi.wSuiteMask & VER_SUITE_BLADE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Web Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Standard Edition "));
- }
- }
- else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Datacenter Server "));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
- }
- else if (osvi.dwMajorVersion == 4) // Windows NT 4.0
- {
- if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0, Enterprise Edition "));
- else
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server 4.0 "));
- }
- }
- }
- // Test for specific product on Windows NT 4.0 SP5 and earlier
- else
- {
- HKEY hKey;
- TCHAR szProductType[BUFSIZE];
- DWORD dwBufLen = BUFSIZE*sizeof(TCHAR);
- LONG lRet;
- lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 0, KEY_QUERY_VALUE, &hKey);
- if (lRet != ERROR_SUCCESS)
- return FALSE;
- lRet = RegQueryValueEx(hKey, TEXT("ProductType"), NULL, NULL, (LPBYTE)szProductType, &dwBufLen);
- RegCloseKey(hKey);
- if ((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE*sizeof(TCHAR)))
- return FALSE;
- if (lstrcmpi(TEXT("WINNT"), szProductType) == 0)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Workstation "));
- if (lstrcmpi(TEXT("LANMANNT"), szProductType) == 0)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Server "));
- if (lstrcmpi(TEXT("SERVERNT"), szProductType) == 0)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Advanced Server "));
- //printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion );
- }
- // Display service pack (if any) and build number.
- if (osvi.dwMajorVersion == 4 && lstrcmpi(osvi.szCSDVersion, TEXT("Service Pack 6")) == 0)
- {
- HKEY hKey;
- LONG lRet;
- // Test for SP6 versus SP6a.
- lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"), 0, KEY_QUERY_VALUE, &hKey);
- if (lRet == ERROR_SUCCESS)
- str.Format(_T("Service Pack 6a (Build %d)"), osvi.dwBuildNumber & 0xFFFF);
- else // Windows NT 4.0 prior to SP6a
- {
- str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
- }
- RegCloseKey(hKey);
- }
- else // not Windows NT 4.0
- {
- str.Format(_T("%s (Build %d)"), osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
- }
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, str);
- break;
- // Test for the Windows Me/98/95.
- case VER_PLATFORM_WIN32_WINDOWS:
- if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 95 "));
- if (osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("OSR2 "));
- }
- if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows 98 "));
- if (osvi.szCSDVersion[1] == 'A' || osvi.szCSDVersion[1] == 'B')
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("SE "));
- }
- if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Windows Millennium Edition"));
- }
- break;
- case VER_PLATFORM_WIN32s:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, _T("Microsoft Win32s"));
- break;
- }
- AfxMessageBox(strWindows);
- return 0;
- }
- BOOL GetSystemInfo(OSVerInfo &tagOSInfo)
- {
- OSVERSIONINFOEX osvi;
- SYSTEM_INFO si;
- PGNSI pGNSI;
- PGPI pGPI;
- BOOL bOsVersionInfoEx;
- DWORD dwType;
- ZeroMemory(&si, sizeof(SYSTEM_INFO));
- ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
- if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
- {
- osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- if (!GetVersionEx((OSVERSIONINFO *)&osvi))
- return FALSE;
- }
- pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- if (NULL != pGNSI)
- pGNSI(&si);
- else
- GetSystemInfo(&si);
- tagOSInfo.dwMajorVersion = osvi.dwMajorVersion;
- tagOSInfo.dwMinorVersion = osvi.dwMinorVersion;
- tagOSInfo.wProductType = osvi.wProductType;
- if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
- {
- StringCchCopy(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Microsoft "));
- // Test for the specific product.
- if (osvi.dwMajorVersion == 10)
- {
- if (osvi.dwMinorVersion == 0)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 10 "));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server Technical Preview "));
- }
- pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
- pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
- switch (dwType)
- {
- case PRODUCT_ULTIMATE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
- break;
- case PRODUCT_HOME_PREMIUM:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
- break;
- case PRODUCT_HOME_BASIC:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
- break;
- case PRODUCT_ENTERPRISE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
- break;
- case PRODUCT_BUSINESS:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
- break;
- case PRODUCT_STARTER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
- break;
- case PRODUCT_CLUSTER_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
- break;
- case PRODUCT_DATACENTER_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
- break;
- case PRODUCT_DATACENTER_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
- break;
- case PRODUCT_ENTERPRISE_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
- break;
- case PRODUCT_ENTERPRISE_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
- break;
- case PRODUCT_ENTERPRISE_SERVER_IA64:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
- break;
- case PRODUCT_STANDARD_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
- break;
- case PRODUCT_STANDARD_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
- break;
- case PRODUCT_WEB_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
- break;
- }
- }
- if (osvi.dwMajorVersion == 6)
- {
- if (osvi.dwMinorVersion == 0)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Vista "));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 "));
- }
- if (osvi.dwMinorVersion == 1)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 7 "));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2008 R2 "));
- }
- if (osvi.dwMinorVersion == 2)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8 "));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 "));
- }
- if (osvi.dwMinorVersion == 3)
- {
- if (osvi.wProductType == VER_NT_WORKSTATION)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 8.1 "));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2012 R2 "));
- }
- pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
- pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
- switch (dwType)
- {
- case PRODUCT_ULTIMATE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Ultimate Edition"));
- break;
- case PRODUCT_HOME_PREMIUM:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Premium Edition"));
- break;
- case PRODUCT_HOME_BASIC:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Basic Edition"));
- break;
- case PRODUCT_ENTERPRISE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
- break;
- case PRODUCT_BUSINESS:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Business Edition"));
- break;
- case PRODUCT_STARTER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Starter Edition"));
- break;
- case PRODUCT_CLUSTER_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Cluster Server Edition"));
- break;
- case PRODUCT_DATACENTER_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
- break;
- case PRODUCT_DATACENTER_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
- break;
- case PRODUCT_ENTERPRISE_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
- break;
- case PRODUCT_ENTERPRISE_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
- break;
- case PRODUCT_ENTERPRISE_SERVER_IA64:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server"));
- break;
- case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Small Business Server Premium Edition"));
- break;
- case PRODUCT_STANDARD_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
- break;
- case PRODUCT_STANDARD_SERVER_CORE:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition (core installation)"));
- break;
- case PRODUCT_WEB_SERVER:
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Server Edition"));
- break;
- }
- }
- if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
- {
- if (GetSystemMetrics(SM_SERVERR2))
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003 R2, "));
- else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Storage Server 2003"));
- else if (osvi.wSuiteMask == VER_SUITE_WH_SERVER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Home Server"));
- else if (osvi.wProductType == VER_NT_WORKSTATION &&
- si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP Professional x64 Edition"));
- }
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows Server 2003, "));
- // Test for the server type.
- if (osvi.wProductType != VER_NT_WORKSTATION)
- {
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
- }
- else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter x64 Edition"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise x64 Edition"));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard x64 Edition"));
- }
- else
- {
- if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Compute Cluster Edition"));
- else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Edition"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Enterprise Edition"));
- else if (osvi.wSuiteMask & VER_SUITE_BLADE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Web Edition"));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Standard Edition"));
- }
- }
- }
- if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows XP "));
- if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Home Edition"));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
- }
- if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Windows 2000 "));
- if (osvi.wProductType == VER_NT_WORKSTATION)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Professional"));
- }
- else
- {
- if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Datacenter Server"));
- else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Advanced Server"));
- else StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT("Server"));
- }
- }
- // Include service pack (if any) and build number.
- if (_tcslen(osvi.szCSDVersion) > 0)
- {
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(" "));
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, osvi.szCSDVersion);
- }
- TCHAR buf[80];
- StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, buf);
- if (osvi.dwMajorVersion >= 6)
- {
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
- si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- tagOSInfo.bIs64Bit = TRUE;
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 64-bit"));
- }
- else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
- {
- tagOSInfo.bIs64Bit = FALSE;
- StringCchCat(tagOSInfo.szOSVerName, BUFSIZE, TEXT(", 32-bit"));
- }
- }
- return TRUE;
- }
- else
- {
- _tprintf(_T("This sample does not support this version of Windows.\n"));
- return FALSE;
- }
- }
- };
|