函数 | 头文件 | 作用 |
---|
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位 |
#include "stdafx.h"
#include <windows.h>
#include <VersionHelpers.h>
int main()
{
BOOL ret;
/*
typedef struct {
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;
BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);
return 如果函数成功,返回值是一个非零值。
*/
/*
https:
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,
_In_ DWORD dwTypeMask,
_In_ DWORDLONG dwlConditionMask
);
*/
OSVERSIONINFOEX versionInfo;
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,
__inout LPDWORD lpnSize
);
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;
}