Determine the Windows operating system version

Source: Internet
Author: User

From msdn:

# Include <windows. h>
# Include <tchar. h>
# Include <stdio. h>

# Define bufsize 80

Typedef void (winapi * pgnsi) (lpsystem_info );

Int _ tmain ()
{
Osversioninfoex osvi;
System_info Si;
Pgnsi;
Bool bosversioninfoex;

Zeromemory (& Si, sizeof (system_info ));
Zeromemory (& osvi, sizeof (osversioninfoex ));

// Try calling getversionex using the osversioninfoex structure.
// If that fails, try using the osversioninfo structure.

Osvi. dwosversioninfosize = sizeof (osversioninfoex );

If (! (Bosversioninfoex = getversionex (osversioninfo *) & osvi )))
{
Osvi. dwosversioninfosize = sizeof (osversioninfo );
If (! Getversionex (osversioninfo *) & osvi ))
Return false;
}

// Call getnativesysteminfo if supported or getsysteminfo otherwise.

Pgnsi = (pgnsi) getprocaddress (
Getmodulehandle (text ("kernel32.dll ")),
"Getnativesysteminfo ");
If (null! = Pgnsi)
Pgnsi (& Si );
Else getsysteminfo (& Si );

Switch (osvi. dwplatformid)
{
// Test for the Windows NT product family.

Case ver_platform_win32_nt:

// Test for the specific product.

If (osvi. dwmajorversion = 6 & osvi. dwminorversion = 0)
{
If (osvi. wproducttype = ver_nt_workstation)
Printf ("Microsoft Windows Vista ");
Else printf ("Windows Server \" Longhorn \"");
}

If (osvi. dwmajorversion = 5 & osvi. dwminorversion = 2)
{
If (getsystemmetrics (sm_serverr2 ))
Printf ("Microsoft Windows Server 2003 \" R2 \"");
Else if (osvi. wproducttype = ver_nt_workstation &&
Si. wprocessorarchitecture = processor_ubunture_amd64)
{
Printf ("Microsoft Windows XP Professional x64 Edition ");
}
Else printf ("Microsoft Windows Server 2003 ,");
}

If (osvi. dwmajorversion = 5 & osvi. dwminorversion = 1)
Printf ("Microsoft Windows XP ");

If (osvi. dwmajorversion = 5 & osvi. dwminorversion = 0)
Printf ("Microsoft Windows 2000 ");

If (osvi. dwmajorversion <= 4)
Printf ("Microsoft Windows NT ");

// Test for specific product on Windows NT 4.0 SP6 and later.
If (bosversioninfoex)
{
// Test for the workstation type.
If (osvi. wproducttype = ver_nt_workstation &&
Si. wprocessorarchitecture! = Processor_ubunture_amd64)
{
If (osvi. dwmajorversion = 4)
Printf (& quot; Workstation 4.0 & quot ");
Else if (osvi. wsuitemask & ver_suite_personal)
Printf ("Home Edition ");
Else printf ("professional ");
}

// Test for the server type.
Else if (osvi. wproducttype = ver_nt_server |
Osvi. wproducttype = ver_nt_domain_controller)
{
If (osvi. dwmajorversion = 5 & osvi. dwminorversion = 2)
{
If (Si. wprocessorarchitecture = processor_ubunture_ia64)
{
If (osvi. wsuitemask & ver_suite_datacenter)
Printf ("datacenter edition for itanium-based systems ");
Else if (osvi. wsuitemask & ver_suite_enterprise)
Printf ("Enterprise Edition for itanium-based systems ");
}

Else if (Si. wprocessorarchitecture = processor_ubunture_amd64)
{
If (osvi. wsuitemask & ver_suite_datacenter)
Printf ("datacenter x64 Edition ");
Else if (osvi. wsuitemask & ver_suite_enterprise)
Printf ("Enterprise x64 Edition ");
Else printf ("Standard x64 Edition ");
}

Else
{
If (osvi. wsuitemask & ver_suite_datacenter)
Printf ("datacenter edition ");
Else if (osvi. wsuitemask & ver_suite_enterprise)
Printf ("Enterprise Edition ");
Else if (osvi. wsuitemask = ver_suite_blade)
Printf ("web edition ");
Else printf ("Standard Edition ");
}
}
Else if (osvi. dwmajorversion = 5 & osvi. dwminorversion = 0)
{
If (osvi. wsuitemask & ver_suite_datacenter)
Printf ("datacenter server ");
Else if (osvi. wsuitemask & ver_suite_enterprise)
Printf ("Advanced Server ");
Else printf ("server ");
}
Else // Windows NT 4.0
{
If (osvi. wsuitemask & ver_suite_enterprise)
Printf ("Server 4.0, Enterprise Edition ");
Else printf ("Server 4.0 ");
}
}
}
// Test for specific product on Windows NT 4.0 SP5 and earlier
Else
{
Hkey;
Tchar szproducttype [bufsize];
DWORD dwbuflen = bufsize * sizeof (tchar );
Long LRET;

LRET = regopenkeyex (HKEY_LOCAL_MACHINE,
Text ("System \ CurrentControlSet \ Control \ productoptions "),
0, key_query_value, & hkey );
If (LRET! = Error_success)
Return false;

LRET = regqueryvalueex (hkey, text ("producttype"), null, null,
(Lpbyte) szproducttype, & dwbuflen );
Regclosekey (hkey );

If (LRET! = Error_success) | (dwbuflen> bufsize * sizeof (tchar )))
Return false;

If (lstrcmpi (text ("winnt"), szproducttype) = 0)
Printf ("workstation ");
If (lstrcmpi (text ("lanmannt"), szproducttype) = 0)
Printf ("server ");
If (lstrcmpi (text ("servernt"), szproducttype) = 0)
Printf ("Advanced Server ");
Printf ("% d. % d", osvi. dwmajorversion, osvi. dwminorversion );
}

// Display Service Pack (if any) and build number.

If (osvi. dwmajorversion = 4 &&
Lstrcmpi (osvi. szcsdversion, text ("Service Pack 6") = 0)
{
Hkey;
Long LRET;

// Test for SP6 versus SP6a.
LRET = regopenkeyex (HKEY_LOCAL_MACHINE,
Text ("SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Hotfix \ q246009 "),
0, key_query_value, & hkey );
If (LRET = error_success)
Printf ("Service Pack 6a (build % d) \ n ",
Osvi. dwbuildnumber & 0 xFFFF );
Else // Windows NT 4.0 prior to SP6a
{
_ Tprintf (text ("% s (build % d) \ n "),
Osvi. szcsdversion,
Osvi. dwbuildnumber & 0 xFFFF );
}

Regclosekey (hkey );
}
Else // not Windows NT 4.0
{
_ Tprintf (text ("% s (build % d) \ n "),
Osvi. szcsdversion,
Osvi. dwbuildnumber & 0 xFFFF );
}

Break;

// Test for the Windows ME/98/95.
Case ver_platform_win32_windows:

If (osvi. dwmajorversion = 4 & osvi. dwminorversion = 0)
{
Printf ("Microsoft Windows 95 ");
If (osvi. szcsdversion [1] = 'C' | osvi. szcsdversion [1] = 'B ')
Printf ("osr2 ");
}

If (osvi. dwmajorversion = 4 & osvi. dwminorversion = 10)
{
Printf ("Microsoft Windows 98 ");
If (osvi. szcsdversion [1] = 'A' | osvi. szcsdversion [1] = 'B ')
Printf ("se ");
}

If (osvi. dwmajorversion = 4 & osvi. dwminorversion = 90)
{
Printf ("Microsoft Windows Millennium Edition \ n ");
}
Break;

Case ver_platform_win32s:

Printf ("Microsoft win32s \ n ");
Break;
}
Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.