pb获取操作系统版本

2014-02-07 14:29:50 dpsying 阅读数 1078


1,涉及API


GetVersionEx函数原型:

  1. BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);  

结构OSVERSIONINFOEX定义如下:

typedef struct {  
	DWORD dwOSVersionInfoSize;			//在使用GetVersionEx之前要将此初始化为结构的大小  
	DWORD dwMajorVersion;					//系统主版本号  
	DWORD dwMinorVersion;				    //系统次版本号  
	DWORD dwBuildNumber;					//系统构建号  
	DWORD dwPlatformId;						//系统支持的平台  
	TCHAR szCSDVersion[128];				//系统补丁包的名称  
	WORD wServicePackMajor;				//系统补丁包的主版本  
	WORD wServicePackMinor;				//系统补丁包的次版本  
	WORD wSuiteMask;							//标识系统上的程序组  
	BYTE wProductType;							//标识系统类型  
	BYTE wReserved;								//保留,未使用  
} OSVERSIONINFOEX, *POSVERSIONINFOEX;  


在调用函数之前先初始化结构的大小并调用函数,返回true表示成功:

  1. OSVERSIONINFOEX os;  
  2. os.dwOSVersionInfoSize=sizeof(os);  
  3. if(!GetVersionEx((OSVERSIONINFO *)&os))  
  4. {  
  5.       return FALSE;  
  6. }  

函数调用成功以后就可以通过OSVERSIONINFOEX来查看系统的版本信息了。


各个操作系统的版本号列表如下: 

操作系统
版本号
Windows 7
6.1
Windows Server 2008 R2
6.1
Windows Server 2008
6.0
Windows Vista
6.0
Windows Server 2003 R2
5.2
Windows Server 2003
5.2
Windows XP
5.1
Windows 2000
5.0



2,使用

判断系统版本的代码示例:

OSVERSIONINFOEX os;  
os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);   
if(GetVersionEx((OSVERSIONINFO *)&os))                    
{  
	switch(os.dwMajorVersion)
	{ 
	case 5:
		switch(os.dwMinorVersion)
		{  
		case 0:						//Windows 2000 
			break;
		case 1:						//Windows XP
			break;
		case 2:						//Windows Server 2003
			break;
		}
		case 6:
			switch(os.dwMinorVersion)
			{
			case 0:
				if(os.wProductType==VER_NT_WORKSTATION)		//Microsoft Windows Vista
				{							
				}
				else										//Microsoft Windows Server 2008							
				{									
				}
				break;
			case 1:  
				if(os.wProductType==VER_NT_WORKSTATION)		//Microsoft Windows 7
				{
				}
				else										//Microsoft Windows Server 2008 R2
				{								
				}
				break;  
			case 2:											//Microsoft Windows 7
				break;
			}
			break;
			default:										//其他版本
				break;
	}
}


操作系统的补丁包版本可以从wServicePackMajor,wServicePackMinor获取。




2014-02-26 20:11:10 xiaokaibupabupa 阅读数 602
平时写程序时会用到系统用户名,现将实现方法贴出,大家分享!
1、申明API函数
FUNCTION ulong WNetGetUser(ref string lpName,ref string lpUserName,ref ulong lpnLength) LIBRARY "mpr.dll" ALIAS FOR "WNetGetUserA" 

2、PB脚本语言
 string ls_name, ls_username
 ulong ll_len
 ll_len = 256
 ls_username = space(ll_len)
 setnull(ls_name)
 WNetGetUser(ls_Name,ls_UserName,ll_Len) 
 messagebox("系统登录用户名",ls_username)

以上脚本在WIN2000下测试通过
2016-03-23 22:51:53 qq_23725883 阅读数 802

为这点事找了在网上查了半天好多感觉不好用,最后选定这个以备自己和需要的朋友再乱翻了,代码是抄的
就在MFC里调用了下很好用。用的RtlGetNtVersionNumbers

        //------------------------------------------------------------------------
        // 函数    : GetNtVersionNumbers
        // 功能    : 调用RtlGetNtVersionNumbers获取系统版本信息
        // 返回值  : BOOL 
        // 参数    : DWORD& dwMajorVer 主版本
        // 参数    : DWORD& dwMinorVer 次版本
        // 参数    : DWORD& dwBuildNumber build号
        // 附注    : 
        //-------------------------------------------------------------------------
    BOOL GetNtVersionNumbers( DWORD&dwMajorVer , DWORD& dwMinorVer , DWORD& dwBuildNumber )
        {
            BOOL bRet = FALSE;
            HMODULE hModNtdll = NULL;
            if ( hModNtdll = ::LoadLibraryW( _T("ntdll.dll") ) )
            {
                typedef void ( WINAPI *pfRTLGETNTVERSIONNUMBERS )( DWORD* , DWORD* , DWORD* );
                pfRTLGETNTVERSIONNUMBERS pfRtlGetNtVersionNumbers;
                pfRtlGetNtVersionNumbers = ( pfRTLGETNTVERSIONNUMBERS )::GetProcAddress(
                    hModNtdll , "RtlGetNtVersionNumbers" );
                if ( pfRtlGetNtVersionNumbers )

                {
                    pfRtlGetNtVersionNumbers( &dwMajorVer , &dwMinorVer , &dwBuildNumber );
                    dwBuildNumber &= 0x0ffff;
                    bRet = TRUE;
                }
                ::FreeLibrary( hModNtdll );
                hModNtdll = NULL;
                }
            return bRet;

        }
    void CMFCApplication3Dlg::OnBnClickedButton1( )//Button按钮直接调用
    {
        // TODO: 在此添加控件通知处理程序代码
        DWORD dwMajorVer;
        DWORD dwMinorVer;
        DWORD dwBuildNumber;
        GetNtVersionNumbers(dwMajorVer ,dwMinorVer ,dwBuildNumber );
        //CString m;
        //m.Format( L"%d.%d.%d" , dwMajorVer , dwMinorVer , dwBuildNumber );
        //AfxMessageBox( m );
        //要判断什么自己添加吧
        if ( dwMajorVer >= 6 )
        {
            AfxMessageBox( _T("您的系统是温妻") );
        } 
        else
        {
            AfxMessageBox( _T("目前不支持您的系统请换温妻") );
        }

    }
2016-06-29 10:32:43 LoveLearnling 阅读数 925

API函数调用:

Function boolean RtlGetNtVersionNumbers (Ref long Major, Ref long Minor,  ref long Optional ) library  "ntdll.dll" 



long Major, Minor, Optional


RtlGetNtVersionNumbers (Major,Minor,  Optional )
messagebox("", string(Major) + "."  + string(Minor))  //版本号


其他编程语言也调用上面API, 获取系统版本号, 这个是应该最好用的。

2012-09-04 15:42:26 Jacholy 阅读数 413

The following example uses the GetVersionExGetSystemMetricsGetProductInfo, and GetNativeSystemInfo functions to determine the version information of the currently running operating system. If compatibility mode is in effect, the example displays the operating system selected for application compatibility. The example displays the information to the console.

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the \\StringFileInfo\\<lang><codepage>\\ProductVersion subblock of the file version information.

Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version.

If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#pragma comment(lib, "User32.lib")

#define BUFSIZE 256

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);

BOOL GetOSDisplayString( LPTSTR pszOS)
{
	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);
	bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);

	if(bOsVersionInfoEx == NULL ) return 1;

	// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.

	pGNSI = (PGNSI) GetProcAddress(
		GetModuleHandle(TEXT("kernel32.dll")), 
		"GetNativeSystemInfo");
	if(NULL != pGNSI)
		pGNSI(&si);
	else GetSystemInfo(&si);

	if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && 
		osvi.dwMajorVersion > 4 )
	{
		StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));

		// Test for the specific product.

		if ( osvi.dwMajorVersion == 6 )
		{
			if( osvi.dwMinorVersion == 0 )
			{
				if( osvi.wProductType == VER_NT_WORKSTATION )
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
				else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " ));
			}

			if ( osvi.dwMinorVersion == 1 )
			{
				if( osvi.wProductType == VER_NT_WORKSTATION )
					StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
				else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " ));
			}

			pGPI = (PGPI) GetProcAddress(
				GetModuleHandle(TEXT("kernel32.dll")), 
				"GetProductInfo");

			pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

			switch( dwType )
			{
			case PRODUCT_ULTIMATE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" ));
				break;
			case PRODUCT_HOME_PREMIUM:
				StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" ));
				break;
			case PRODUCT_HOME_BASIC:
				StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" ));
				break;
			case PRODUCT_ENTERPRISE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
				break;
			case PRODUCT_BUSINESS:
				StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" ));
				break;
			case PRODUCT_STARTER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" ));
				break;
			case PRODUCT_CLUSTER_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" ));
				break;
			case PRODUCT_DATACENTER_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" ));
				break;
			case PRODUCT_DATACENTER_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" ));
				break;
			case PRODUCT_ENTERPRISE_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
				break;
			case PRODUCT_ENTERPRISE_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" ));
				break;
			case PRODUCT_ENTERPRISE_SERVER_IA64:
				StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" ));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" ));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
				StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" ));
				break;
			case PRODUCT_STANDARD_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" ));
				break;
			case PRODUCT_STANDARD_SERVER_CORE:
				StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" ));
				break;
			case PRODUCT_WEB_SERVER:
				StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" ));
				break;
			}
		}

		if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
		{
			if( GetSystemMetrics(SM_SERVERR2) )
				StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, "));
			else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
				StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003"));
			else if( osvi.wProductType == VER_NT_WORKSTATION &&
				si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
			{
				StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition"));
			}
			else StringCchCat(pszOS, 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(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" ));
					else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" ));
				}

				else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
				{
					if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" ));
					else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" ));
					else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" ));
				}

				else
				{
					if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" ));
					else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" ));
					else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" ));
					else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
						StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" ));
					else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" ));
				}
			}
		}

		if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
		{
			StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
			if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
				StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" ));
			else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
		}

		if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
		{
			StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));

			if ( osvi.wProductType == VER_NT_WORKSTATION )
			{
				StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
			}
			else 
			{
				if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
					StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" ));
				else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
					StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" ));
				else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" ));
			}
		}

		// Include service pack (if any) and build number.

		if( _tcslen(osvi.szCSDVersion) > 0 )
		{
			StringCchCat(pszOS, BUFSIZE, TEXT(" ") );
			StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
		}

		TCHAR buf[80];

		StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
		StringCchCat(pszOS, BUFSIZE, buf);

		if ( osvi.dwMajorVersion >= 6 )
		{
			if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
				StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" ));
			else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
				StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
		}

		return TRUE; 
	}

	else
	{  
		printf( "This sample does not support this version of Windows.\n");
		return FALSE;
	}
}

int __cdecl _tmain()
{
	TCHAR szOS[BUFSIZE];

	if( GetOSDisplayString( szOS ) )
		_tprintf( TEXT("\n%s\n"), szOS );
}