Jump to content

simple OS enumeration


MilesAhead
 Share

Recommended Posts

A simple way to compare OS version to an enumeration for minimum OS version support.

I just reversed the order of @OSVersion in an array to make it easy to test if the current

OS version is the minimum required for whatever. If there's an easier way to get the major

number off the build number is wasn't obvious. Plus this should be easy enough to use:

; return the array index of OSVersion for minimum OS support
; Author: MilesAhead
;
Global Enum $WIN_95, $WIN_98, $WIN_ME, $WIN_NT, $WIN_2000, $WIN_XP, $WIN_2003, _
        $WIN_VISTA, $WIN_2008

Func _OSlevel()
    Local $ver = @OSVersion
    Local $winVersions[9] = ["WIN_95", "WIN_98", "WIN_ME", "WIN_NT4", "WIN_2000", _
            "WIN_XP", "WIN_2003", "WIN_VISTA", "WIN_2008"]

    For $i = $WIN_95 To UBound($winVersions) - 1
        If $winVersions[$i] = $ver Then Return $i
    Next
EndFunc  ;==>_OSlevel

;example usage
If _OSlevel >= $WIN_2000 Then _ReduceMemory()
Link to comment
Share on other sites

  • 5 months later...

I updated this function to accommodate the possibility something might go wrong. :)

; return the array index of OSVersion for minimum OS support
; if not found returns $WIN_UNKNOWN and sets @error to 1
; Author: MilesAhead
;
Global Enum $WIN_UNKNOWN = -1, $WIN_95, $WIN_98, $WIN_ME, $WIN_NT, $WIN_2000, $WIN_XP, _ $WIN_2003, $WIN_VISTA, $WIN_2008

Func _OsLevel()
    Local $ver = @OSVersion
    Local $winVersions[9] = ["WIN_95", "WIN_98", "WIN_ME", "WIN_NT4", "WIN_2000", _
            "WIN_XP", "WIN_2003", "WIN_VISTA", "WIN_2008"]

    For $i = $WIN_95 To UBound($winVersions) - 1
        If $winVersions[$i] = $ver Then Return $i
    Next
    SetError(1)
    Return $WIN_UNKNOWN
EndFunc  ;==>_OsLevel
Link to comment
Share on other sites

  • 4 weeks later...

Here's _OsLevel() reworked to avoid array and loop... just a switch

; return OS enumeration based on @OSVersion for easier comparisons.
; on failure returns $WIN_UNKNOWN and sets @error to 1
; Author: MilesAhead
;
Global Enum $WIN_UNKNOWN = -1, $WIN_95, $WIN_98, $WIN_ME, $WIN_NT, $WIN_2000, _ 
$WIN_XP, $WIN_2003, $WIN_VISTA, $WIN_2008

Func _OsLevel()
    Switch @OSVersion
        Case "WIN_95"
            Return $WIN_95

        Case "WIN_98"
            Return $WIN_98

        Case "WIN_ME"
            Return $WIN_ME

        Case "WIN_NT"
            Return $WIN_NT

        Case "WIN_2000"
            Return $WIN_2000

        Case "WIN_XP"
            Return $WIN_XP

        Case "WIN_2003"
            Return $WIN_2003

        Case "WIN_VISTA"
            Return $WIN_VISTA

        Case "WIN_2008"
            Return $WIN_2008

        Case Else
            Return SetError(1, 0, $WIN_UNKNOWN)
    EndSwitch
EndFunc ;==>_OsLevel
Edited by MilesAhead
Link to comment
Share on other sites

Since I found myself messing around with GetVersion() I figured I might as well post an AutoIt3 user function that returns a number. The function builds a string from the major and minor version number info returned from GetVersion() and applies Number() to the result. Vista would return 6 instead of 6.0 but if there's a non-zero minor version it will be returned in fixed point type format as in 6.1 for Windows7 or 5.1 for XP etc..

;return the Windows version as a number.
;example 6.1 for Windows7, 6 for Vista,
;5.1 for XP etc..
;
; Author MilesAhead
;
Func _WinVersion()
    Local $dwVersion = DllCall("kernel32.dll", "dword", "GetVersion")
    Local $versionStr = String(BitAND($dwVersion[0], 0x00FF))
    $versionStr &= "." & String(BitShift(BitAND($dwVersion[0], 0xFF00), 8))
    Return Number($versionStr)
EndFunc;==>_WinVersion

Guess if you wanted to return it as a string preserving the .0 in 6.0 you could just

return $versionStr. I'm not sure if there's a numeric conversion that would preserve

a .0. In Delphi and some other languages you'd multiply by 1.0 but it seems to have

no effect here.

Edited by MilesAhead
Link to comment
Share on other sites

Ok, with this _WinVersion you can have cake and eat it too.

pass $retAsString param as 1 for string return, 0 for numeric. Might as well take advantage that the return type is flexible. :D

;return the Windows version as a number or string
;according to $retAsString param.
;example 6.1 for Windows7, 6 for Vista,
;5.1 for XP etc.. as number, as string the
;minor version will be preserved even if .0
;(Vista returns "6.0" etc.)
;
; Author MilesAhead
;
Func _WinVersion($retAsString = 0)
    Local $dwVersion = DllCall("kernel32.dll", "dword", "GetVersion")
    Local $versionStr = String(BitAND($dwVersion[0], 0x00FF))
    $versionStr &= "." & String(BitShift(BitAND($dwVersion[0], 0xFF00), 8))
    If $retAsString Then Return $versionStr
    Return Number($versionStr)
EndFunc  ;==>_WinVersion
Link to comment
Share on other sites

Hi Miles,

doing some research here

http://msdn.microsoft.com/en-us/library/ms724439(VS.85).aspx

I read that this function is superseded. "New applications should use GetVersionEx or VerifyVersionInfo." Reading this

http://www.codeguru.com/cpp/w-p/system/sys...ticle.php/c8973

I comprehend that not only the Major Version is relevant to identify an OS, but also the Minor Version, a Product Type and a Suite Mask. Reading further I found that VerifyVersionInfo (min client Windows 2000 Professional) returns those values:

http://msdn.microsoft.com/en-us/library/ms725492(VS.85).aspx

I guess to make a valid identification you'll have to use a matrix like shown on codeguru combined with the VerifyVersionInfo function.

Best Regards

Edited by KaFu
Link to comment
Share on other sites

Hi Miles,

doing some research here

...

I guess to make a valid identification you'll have to use a matrix like shown on codeguru combined with the VerifyVersionInfo function.

Best Regards

Perhaps you could translate said codeguru function to AutoIt3 for us? If you notice the heading was "simple OS enumeration." Simple suits my needs. If I need to suite matrix I'll probably worry about it then. :D

Link to comment
Share on other sites

  • 4 months later...

Hi Miles,

doing some research here

http://msdn.microsoft.com/en-us/library/ms724439(VS.85).aspx

I read that this function is superseded. "New applications should use GetVersionEx or VerifyVersionInfo." Reading this

http://www.codeguru.com/cpp/w-p/system/sys...ticle.php/c8973

I comprehend that not only the Major Version is relevant to identify an OS, but also the Minor Version, a Product Type and a Suite Mask. Reading further I found that VerifyVersionInfo (min client Windows 2000 Professional) returns those values:

http://msdn.microsoft.com/en-us/library/ms725492(VS.85).aspx

I guess to make a valid identification you'll have to use a matrix like shown on codeguru combined with the VerifyVersionInfo function.

Best Regards

Actually you are right. Sorry I bit your head off. :)

I know someone already did this but I needed to learn to

use DllStructCreate anyway. So I wrote a function to fill

in the OSVERSIONINFO structure. I don't think I've written

a program that needs to know more than what it provides afa

version info. Anyway, I added it to my misc. functions file

so I thought I'd update here. ;)

edit: I changed it so that it always returns 0 on error. You

can test return value with IsArray or test @error. On success

it returns an array of strings. On failure @error is reflected

back out of the function and @extended is set to either 1, 2 or

3 to give the locus of the function that failed.

edit2: added some lines to free the memory for the struct instead

of leaving it dangling.

;_GetVersionEx()
;
;It calls the Ansi version of GetVersionEx() WinAPI
;
;On success returns zero based 5 element array of strings
;element 0 = OS Major;1 = OS Minor; 2 = build; 3 = platform; 4 = Version String
;note Version String may be an empty string or something like "Service Pack 1"
;
;On error returns 0 and sets @error to the value contained in @error macro.
;Sets @extended to 1 if error occurred in DllStructCreate(), 2 if error
;occurred in DllStructSetData(), or 3 if error occurred in DllCall()
;
Func _GetVersionEx()
    Local $OsArray[5] = [""]
    Local $osvi = DllStructCreate("dword size;dword OsMajor;dword OsMinor;dword build;dword platform;char verString[128]")
    If @error Then
        Return SetError(@error, 1, 0)
    EndIf
    DllStructSetData($osvi, "size", 148)
    If @error Then
                $osvi = 0
        Return SetError(@error, 2, 0)
    EndIf
    Local $retVal = DllCall("Kernel32.dll", "int", "GetVersionExA", "ptr", DllStructGetPtr($osvi))
    If @error Then
                $osvi = 0
        Return SetError(@error, 3, 0)
    EndIf
    $OsArray[0] = String(DllStructGetData($osvi, "OsMajor"))
    $OsArray[1] = String(DllStructGetData($osvi, "OsMinor"))
    $OsArray[2] = String(DllStructGetData($osvi, "build"))
    $OsArray[3] = String(DllStructGetData($osvi, "platform"))
    $OsArray[4] = DllStructGetData($osvi, "verString")
        $osvi = 0
    Return $OsArray
EndFunc   ;==>_GetVersionEx
Edited by MilesAhead
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...