Click here to Skip to main content
Click here to Skip to main content

Determining the specific edition of Windows for now and in the future

By , 2 Jan 2007
Rate this:
Please Sign up or sign in to vote.

Introduction

While determining the version number of the copy of Windows that a .NET application is running on has been relatively easy with Environment.OSVersion. Knowing exactly which version of Windows it is and what edition it is (Home, Pro, Tablet, Media Center, N, etc) has been a bit more difficult and an ever changing edition set assumes one is actually detectable.

With the introduction of Windows Vista and it's 9 different editions, it seemed as if being able to easily tell the difference between them was going to be even more difficult... not so.

At last Microsoft has provided a simple API for determining which Windows edition (a.k.a. SKU or product type) is being run within a general family as well as provides a way for you to be aware of future versions of Windows without having to change your application down the line.

I present GetProductInfo:

[DllImport("Kernel32.dll")]
internal static extern bool GetProductInfo(
    int osMajorVersion,
    int osMinorVersion,
    int spMajorVersion,
    int spMinorVersion,
    out ProductType edition);

Usage

In order to determine which edition of Windows a PC is running, one need only call GetProductInfo and capture the output value of the last parameter if the method call is successful:

ProductType edition = ProductType.Undefined;

if (GetProductInfo(6, 0, 0, 0, out edition))
{
    Console.WriteLine("Edition: " + edition);
}

If we want to be less specific than just a specific version (i.e. Ultimate or not), we can also use a quick switch block to see if the version is of a specific type:

switch (edition)
{
    case ProductType.HomeBasic:
    case ProductType.HomeBasicN:
    case ProductType.HomePremium:
    case ProductType.HomeServer:
    //Is home edition
    break;
}

The Future

But how does this make your code able to deal with future unannounced or unknown versions? Why are we arbitrarily specifying a version number in our API call?

The enum that GetProductInfo() uses today is tied to the 6.0.0.0 version of Windows (Vista) and in later versions will likely be updated along with a new version number while still retaining awareness of the old one(s).

This means that when your application built today against Vista is run on a future system that uses a different ProductType value, the API will recognize that your app is only aware of ProductTypes that existed in the 6.0.0.0 version of the enum, and return a value of that type instead of returning an unknown value.

Take the switch statement from above... what happens if in the future Microsoft releases Windows Home Edition for Kids and that has a ProductType value of HomeKids... rather than returning an unknown value, you might receive HomeBasic back instead because you have already specified the version of the enum you are aware of.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Brendan Grant
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralGet Description Pinmembershawkins25-Jan-07 12:25 
This is nice if I want to compare the version to make sure that I my application will work with a specific version. However, if I am trying to simply get the name of the specific edition of Windows to pass back to me for support purposes, how do I do that? Is there an api that will return a complete description of the current OS?
AnswerRe: Get Description PinmemberMember 376225416-Nov-08 7:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 2 Jan 2007
Article Copyright 2007 by Brendan Grant
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid