c - 获取操作系统的名称和版本

标签 c windows operating-system version

如果您在 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;

和操作系统版本信息

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

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

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

最佳答案

如果想得到与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);
    }

关于c - 获取操作系统的名称和版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807990/

相关文章:

c - 字符串中最长的回文及其长度

android - 如何使用批处理文件双击安装apk文件

windows - 为什么我的 Python 多处理工作进程不使用多核?

Java - 确定操作系统位版本

raspberry-pi - ARM 处理器如何使用超过 4GB 的内存?

c - 可滚动位图 win32 可能吗?

c# - 从 C 函数返回结构,C# 中的等价物是什么?

c - 如何让 C 编译器假定十进制文字(例如 1.23)是 float 而不是 double ?

windows - 内存映射文件可选写入可能吗?

operating-system - 当堆栈发生页面错误时会发生什么?