MilesAhead Posted December 14, 2008 Share Posted December 14, 2008 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() My Freeware Page Link to comment Share on other sites More sharing options...
MilesAhead Posted May 17, 2009 Author Share Posted May 17, 2009 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 My Freeware Page Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted May 17, 2009 Share Posted May 17, 2009 You could save one line by doing: Return SetError(1, 0, $WIN_UNKNOWN) .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
MilesAhead Posted May 19, 2009 Author Share Posted May 19, 2009 True. I'll have to remember that next time I set @error in a function. My Freeware Page Link to comment Share on other sites More sharing options...
trancexx Posted May 19, 2009 Share Posted May 19, 2009 And if I do this:... SetError(1, 0, $WIN_UNKNOWN) ReturnWhat would you expect to be returned? Don't test and don't consult help file before the answer. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
MilesAhead Posted June 12, 2009 Author Share Posted June 12, 2009 (edited) Here's _OsLevel() reworked to avoid array and loop... just a switch expandcollapse popup; 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 June 12, 2009 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
MilesAhead Posted June 12, 2009 Author Share Posted June 12, 2009 (edited) 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 June 12, 2009 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
MilesAhead Posted June 13, 2009 Author Share Posted June 13, 2009 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. ;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 My Freeware Page Link to comment Share on other sites More sharing options...
KaFu Posted June 13, 2009 Share Posted June 13, 2009 (edited) Hi Miles,doing some research herehttp://msdn.microsoft.com/en-us/library/ms724439(VS.85).aspxI read that this function is superseded. "New applications should use GetVersionEx or VerifyVersionInfo." Reading thishttp://www.codeguru.com/cpp/w-p/system/sys...ticle.php/c8973I 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).aspxI 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 June 13, 2009 by KaFu  OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
MilesAhead Posted June 17, 2009 Author Share Posted June 17, 2009 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 RegardsPerhaps 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. My Freeware Page Link to comment Share on other sites More sharing options...
MilesAhead Posted November 4, 2009 Author Share Posted November 4, 2009 (edited) 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 November 4, 2009 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now