如何获取Windows中的操作系统名称、版本?

发布于 2024-11-09 13:11:48 字数 233 浏览 0 评论 0原文

在以下方面哪一个更好地获取操作系统名称、Windows 操作系统版本 -

  1. 中的信息兼容性的时间
  2. 获取所有 Windows 操作系统(如 xp、vista 和更高版本的

systeminfo 或 wmic 命令) ?我想避免在 C 中使用 OSVersionInfoEx,因为必须对营销名称检测进行硬编码,并且如果引入新风格的 Windows,则会增加维护工作。请分享您的意见。

Which one is better in following aspects to get OS name, OS version in windows -

  1. time in getting information
  2. compatibility in all windows OS like xp, vista and higher

systeminfo or wmic command? I wanted to avoid the use of OSVersionInfoEx in C as one has to hardcode the marketing name detection and will add to maintenance work if new flavour of windows gets introduced. Please share your opinion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-11-16 13:11:48

GetVersionEx - 获取基本操作系统版本号的速度没有比这更快的了。但你是对的,你将无法将较新版本的操作系统映射到正确的字符串。您是否考虑过这样做:

OSVERSIONINFOEX version = {};
char szOS[MAX_OS_LENGTH];

version.dwOSVersionInfoSize = sizeof(version);
GetVersionEx((OSVERSIONINFO*)&version);

if (MyFunctionToMapVersionToString(&version, szOS) == false)
{
    sprintf(szOS, "Microsoft Windows %d.%d", version.dwMajorVersion, version.dwMinorVersion);
}

WMI - 需要编写更多代码。但是您可能只在应用程序启动时(或需要时)执行此操作,并在再次需要信息时缓存结果。应用程序查询一次后,操作系统产品名称不会改变。 :) 至于向后兼容性,我确信它在较旧的操作系统上运行良好......但是您将在将其交付给客户之前对其进行测试,对吧?

如果您想要一种未记录的方式,有一个注册表项恰好包含您想要的内容:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion ("ProductName")

GetVersionEx - You can't get any faster than this for getting a basic os version number. But you are right, you won't be able to map newer versions of the OS to the correct string. Have you considered just doing this:

OSVERSIONINFOEX version = {};
char szOS[MAX_OS_LENGTH];

version.dwOSVersionInfoSize = sizeof(version);
GetVersionEx((OSVERSIONINFO*)&version);

if (MyFunctionToMapVersionToString(&version, szOS) == false)
{
    sprintf(szOS, "Microsoft Windows %d.%d", version.dwMajorVersion, version.dwMinorVersion);
}

WMI - A bit more code to write. But you could likely just do this at app startup (or when it's needed) and cache the result if the information is needed again. It's not like the operating system product name will change after the app has queried for it once. :) As to backwards compatibility, I'm sure it work fine on older operating systems... but you are going to test it before shipping it to the customer, right?

If you want an undocumented way, there is a registry key that has exactly what you want in it:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion ("ProductName")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文