Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetVersion (kernel32)
 
.
Summary
Summary
Obtains extended information about the version of the operating system that is currently running.

C# Signatures:

// This must be used if OSVERSIONINFO is defined as a struct or Class - The previous [In,Out] parameter defined for
// a class could cause buffer overrun errors.
[ DllImport( "kernel32" )]
static extern bool GetVersionEx( ref OSVERSIONINFO osvi );  

VB.Net Signatures:

' This must be used if OSVERSIONINFO is defined as a struct

<DllImport("kernel32")> _
Private Shared Function GetVersionEx(ByRef osvi As OSVERSIONINFO) As Boolean
End Function

' This must be used if OSVERSIONINFO is defined as a class
<DllImport("kernel32")> _
Private Shared Function GetVersionEx(<[In](), Out()> ByVal osvi As OSVERSIONINFO) As Boolean
End Function

C# Signature:

[DllImport("kernel32.dll")]
static extern uint GetVersion();

User-Defined Types:

OSVERSIONINFO

Notes:

None.

Tips & Tricks:

If you forget to set the OSVersionInfoSize field of the OSVERSIONINFO struct, the function will return false. GetLastError() will return:

Notes:

Retrieves the version number of the current operating system

127

ERROR_PROC_NOT_FOUND

"The specified procedure could not be found."

Tips & Tricks:

Please add some!

Sample Code:

Using the OSVERSIONINFO class and corresponding signature:

Console.WriteLine( "\nPassing OSVERSIONINFO as class" );

using System;

using System.Text;

using System.Runtime.InteropServices;

OSVERSIONINFO osvi = new OSVERSIONINFO();
osvi.OSVersionInfoSize = Marshal.SizeOf( osvi );

namespace testt

{

    class Program
    {
    static void Main(string[] args)
    {
        StringBuilder str = new StringBuilder(100);
        uint dwWersion = WinAPI.GetVersion();

GetVersionEx( osvi );

Console.WriteLine( "Class size:    {0}", osvi.OSVersionInfoSize );

Using the OSVERSIONINFO struct and corresponding signature:

        str.Append(WinAPI.LoByte((ushort)WinAPI.LoWord(dwWersion)));
        str.Append(".");
        str.Append(WinAPI.HiByte((ushort)WinAPI.LoWord(dwWersion)));
        if (dwWersion < 0x80000000)
        {
        str.Append("." + WinAPI.HiWord(dwWersion).ToString());
        }
        Console.WriteLine("OS Version is " + str.ToString());
    }
    }

Console.WriteLine( "\nPassing OSVERSIONINFO as struct" );
    static class WinAPI
    {
    public static uint LoWord(uint dwValue)
    {
        return dwValue & 0xFFFF;
    }

OSVERSIONINFO osvi2 = new OSVERSIONINFO();
osvi2.OSVersionInfoSize = Marshal.SizeOf(ref typeof(OSVERSIONINFO) );
    public static uint HiWord(uint dwValue)
    {
        return (dwValue >> 16) & 0xFFFF;
    }

GetVersionEx( ref osvi2 );
    public static ushort LoByte(ushort wValue)
    {
        return (ushort)(wValue & 0xFF);
    }

Console.WriteLine( "Struct size:   {0}", osvi2.OSVersionInfoSize );

Alternative Managed API:

System.Environment.OSVersion property

    public static ushort HiByte(ushort wValue)
    {
        return (ushort)((wValue >> 8) & 0xFF);
    }

    Friend Function IsWinVista() As Boolean
    Dim osInfo As System.OperatingSystem = System.Environment.OSVersion
    Return (osInfo.Platform = PlatformID.Win32NT AndAlso osInfo.Version.Major = 6)
    End Function
    [DllImport("kernel32.dll")]
    public static extern uint GetVersion();
    }

}

    Friend Function IsWinXP() As Boolean
    Dim osInfo As System.OperatingSystem = System.Environment.OSVersion
    Return (osInfo.Platform = PlatformID.Win32NT AndAlso osInfo.Version.Major = 5 AndAlso osInfo.Version.Minor >= 1)
    End Function

Documentation

Alternative Managed API:

GetVersionEx(), Operating System Version(), VerifyVersionInfo()

Documentation
<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx">GetVersion</href>@msdn on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions