How to find which operating system is running the PC in C++?

To do this, you have to use Win32 (Windows API).

Example:

void GetOS(char theos[256])
{
OSVERSIONINFO OS;
OS.dwOSVersionInfoSize = sizeof(OS);
GetVersionEx(&OS);
    switch (OS.dwPlatformId)
    {
    case 0:
        strcpy(theos,"Win3.1");
        break;
    case 1:
        switch (OS.dwMinorVersion)
        {
        case 0:
            strcpy(theos,"Win95");
            break;
        case 10:
            strcpy(theos,"Win98");
            break;
        case 98:
            strcpy(theos,"WinMe");
            break;
        }
        break;
    case 2:
        switch (OS.dwMajorVersion)
        {
        case 3:
            strcpy(theos,"WinNT");
            break;
        case 4:
            strcpy(theos,"WinNT");
            break;
        case 5:
            switch (OS.dwMinorVersion)
            {
            case 0:
                strcpy(theos,"Win2000");
                break;
            case 1:
                strcpy(theos,"WinXP");
                break;
            }
            break;

        case 6:
            switch (OS.dwMinorVersion)
            {
            case  0:
                strcpy(theos,"Vista");
                break;
            case 1:
                strcpy(theos,"Win7");
                break;
            }
            break;
        }
        break;
    }
}

Source:
http://www.l33ts.org/forum/Thread-C-GetOS

To do this, you have to use Win32 (Windows API).

Um...to determine the OS you need to use the Windows API? Doesn't that presume that the OS is Windows and then you're just drilling down to the exact version? ;)

How to find which operating system is running the PC in C++?

Well, C++ is a compiled language, meaning that a particular operating system is always targeted when compiling the code, i.e., the operating system type is known at compile-time. And all compilers will define a number of predefined MACROs that will identify (fairly completely) what the target system is. Here is an example of using that:

 #if defined(_WIN32)

   // this is a Windows environment!

 #elif defined(__linux__)

   // this is a Linux environment! (any GNU/Linux distribution)

 #elif defined(__APPLE__)

   // this is a Mac OSX environment!

 #elif defined(BSD)

   // this is a BSD environment! (OpenBSD, FreeBSD, etc.)

 #elif defined(__QNX__)

   // this is a QNX environment!

 #endif

You can find a comprehensive list of predefined macros here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.