How do I find the name of an operating system?

前端 未结 2 1565
广开言路
广开言路 2021-02-07 13:41

The questions pretty simple. I want want a function (C++) or method which will, on call, returun something like

\"Windows\" //or
\"Unix\"

Noth

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 14:07

    From the Poco source code:

    Win32:

    std::string EnvironmentImpl::osNameImpl()
    {
        OSVERSIONINFO vi;
        vi.dwOSVersionInfoSize = sizeof(vi);
        if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
        switch (vi.dwPlatformId)
        {
        case VER_PLATFORM_WIN32s:
            return "Windows 3.x";
        case VER_PLATFORM_WIN32_WINDOWS:
            return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
        case VER_PLATFORM_WIN32_NT:
            return "Windows NT";
        default:
            return "Unknown";
        }
    }
    

    Unix:

    std::string EnvironmentImpl::osNameImpl()
    {
        struct utsname uts;
        uname(&uts);
        return uts.sysname;
    }
    

提交回复
热议问题