【问题标题】:Get name and version of OS获取操作系统的名称和版本
【发布时间】:2023-07-21 16:29:01
【问题描述】:

如果你在 cmd 中输入ver,你会得到类似:

Microsoft Windows [Version 10.0.17192.162]

我是否可以访问这些信息以在我的 C 程序中使用?我需要找到一个人正在运行的 Windows 版本。我已经检查了 SYSTEM_INFO:

typedef struct _SYSTEM_INFO {
  union {
    DWORD  dwOemId;
    struct {
      WORD wProcessorArchitecture;
      WORD wReserved;
    };
  };
  DWORD     dwPageSize;
  LPVOID    lpMinimumApplicationAddress;
  LPVOID    lpMaximumApplicationAddress;
  DWORD_PTR dwActiveProcessorMask;
  DWORD     dwNumberOfProcessors;
  DWORD     dwProcessorType;
  DWORD     dwAllocationGranularity;
  WORD      wProcessorLevel;
  WORD      wProcessorRevision;
} SYSTEM_INFO;

和 OSVERSIONINFO

typedef struct _OSVERSIONINFOA {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  CHAR  szCSDVersion[128];
} OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;

但都不包含完整的版本信息。

另外,对于检索操作系统的名称,除了进行#ifdef __WIN32 检查之外还有其他方法吗?

【问题讨论】:

标签: c windows operating-system version


【解决方案1】:

对于 WinVersion 或 WinVersionEx

对于较新的 Windows 版本,请使用 Version Helper functions

【讨论】:

  • VerifyVersionInfo 是谎言,不返回实际的 Windows 版本。
【解决方案2】:

如果想得到与 cmd 相同的结果,可以运行下一段代码:

ULONG GetVersionRevision(PULONG Ubr)
{
    HKEY hKey;
    ULONG dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ|KEY_WOW64_64KEY, &hKey);
    if (dwError == NOERROR)
    {
        ULONG Type;
        ULONG cb = sizeof(ULONG);
        dwError = RegQueryValueExW(hKey, L"UBR", 0, &Type, (PBYTE)Ubr, &cb);

        if (dwError == NOERROR)
        {
            if (Type != REG_DWORD || cb != sizeof(ULONG))
            {
                dwError = ERROR_GEN_FAILURE;
            }
        }
        RegCloseKey(hKey);
    }

    return dwError;
}

    ULONG M, m, b, Ubr;
    RtlGetNtVersionNumbers(&M, &m, &b);

    if (GetVersionRevision(&Ubr) == NOERROR)
    {
        DbgPrint("[Version %u.%u.%u.%u]\n", M, m, b & 0xffff, Ubr);
    }
    else
    {
        DbgPrint("[Version %u.%u.%u]\n", M, m, b & 0xffff);
    }

【讨论】:

    最近更新 更多