C/C++ Windows API——获取计算机信息 转

函数头文件作用
GetVersionEx <windows.h> 获取系统版本信息(deprecated)
VerifyVersionInfo <VersionHelpers.h> 判断当前系统信息是否符合条件
GetComputerName <windows.h> 获取计算机名称
GetUserName <windows.h> 获取用户名
memset <windows.h> 对结构体或数组进行清零操作,跨平台
ZeroMemory <windows.h> 对结构体或数组进行清零操作,仅适用于Windows
={0}   是结构体和数组的一种初始化方式,它是将结构体中基本类型变量赋默认值,当结构体中有非基本类型(例如类对象)时,会编译错误,这也是一种保护。
LOBYTE   取1个字节(byte)的低4位
HIBYTE   取1个字节(byte)的高4位
LOWORD   取2个字节(short)的低8位
HIWORD   取2个字节(short)的高8位
// ComputerInfo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>//GetComputerName,GetUserName
#include <VersionHelpers.h>//IsWindows7OrGreater, IsWindows10OrGreater 

int main()
{
    BOOL ret;

    /*
    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;

    //Compile Error: GetVersionEx‘ declared deprecated
    BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);
    return 如果函数成功,返回值是一个非零值。
    */

    /*
    https://msdn.microsoft.com/en-us/ms724833(v=vs.85)
    typedef struct _OSVERSIONINFOEX {
        DWORD dwOSVersionInfoSize;
        DWORD dwMajorVersion;
        DWORD dwMinorVersion;
        DWORD dwBuildNumber;
        DWORD dwPlatformId;
        TCHAR szCSDVersion[128];
        WORD  wServicePackMajor;
        WORD  wServicePackMinor;
        WORD  wSuiteMask;
        BYTE  wProductType;
        BYTE  wReserved;
    } OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;

    BOOL WINAPI VerifyVersionInfo(
        _In_ LPOSVERSIONINFOEX lpVersionInfo,   //HIBYTE(_WIN32_WINNT_WIN7)
        _In_ DWORD             dwTypeMask,
        _In_ DWORDLONG         dwlConditionMask //VER_SET_CONDITION 可以是 VER_GREATER,VERSION_EQU,VERSION_LESS
    );
    */
    OSVERSIONINFOEX versionInfo;
    //Initialize the OSVERSIONINFOEX structure.
    ZeroMemory(&versionInfo, sizeof(versionInfo));
    versionInfo.dwMajorVersion = HIBYTE(_WIN32_WINNT_WIN7);
    DWORD dwTypeMask = VER_MAJORVERSION;
    DWORDLONG dwlConditionMask = 0;
    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER);
    ret = VerifyVersionInfo(&versionInfo, dwTypeMask, dwlConditionMask);
    printf("VerifyVersionInfo %d -> %d\n", versionInfo.dwMajorVersion, ret);

    ret = IsWindowsServer();
    printf("IsWindowsServer -> %d\n", ret);

    ret = IsWindows7OrGreater();
    printf("IsWindows7OrGreater -> %d\n", ret);

    TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH + 1];
    DWORD len = MAX_COMPUTERNAME_LENGTH + 1;

    /*
    BOOL WINAPI GetComputerName(
        __out   LPTSTR  lpBuffer,   //指向一块内存缓冲区,这个缓冲区用于接收计算机名称或群集的虚拟服务器名称。缓冲区的大小应该足够大,以便能够容纳MAX_COMPUTERNAME_LENGTH + 1个字符。
        __inout LPDWORD lpnSize     //在输入时,指定的缓冲区的大小,这个大小是按照TCHAR计算的;在输出的时候,字符的长度不包括终止空字符\0。
    );
    return 如果函数成功,返回值是一个非零值。
    */
    ret = GetComputerName(szComputerName, &len);
    if (!ret) {
        printf("GetComputerName fail(%ld)\n", GetLastError());
    }
    else {
        wprintf(L"GetComputerName -> %s, len=%d\n", szComputerName, len);
    }

    /*
    BOOL WINAPI GetComputerName(
        __out       LPTSTR  lpBuffer,
        __in_out    LPDWORD lpnSize
    );
    */
    ret = GetUserName(szComputerName, &len);
    if (!ret) {
        printf("GetUserName fail(%ld)\n", GetLastError());
    }
    else {
        wprintf(L"GetUserName -> %s, len=%d\n", szComputerName, len);
    }

    system("pause");
    return 0;
}
posted @ 2016-12-05 10:14  wolfplan  阅读(4335)  评论(0编辑  收藏  举报