Last active
December 21, 2015 18:49
-
-
Save moteus/6350481 to your computer and use it in GitHub Desktop.
Detecting windows and service pack versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function exec(str) | |
local f, err = io.popen(str .. " 2>&1", "r") | |
if not f then return nil, err end | |
local str, err = f:read("*all") | |
f:close() | |
if str then return str end | |
return str, err | |
end | |
local WINVER = { | |
{"6.2", "Windows 8" }, | |
{"6.1", "Windows 7" }, | |
{"6.0", "Windows Vista" }, | |
{"5.2", "Windows Server 2003" }, | |
{"5.1", "Windows XP" }, | |
{"5.0", "Windows 2000" }, | |
} | |
local function win_ver(...) | |
local str, err = exec("ver") | |
if not str then return nil, err end | |
for _, t in ipairs(WINVER) do | |
if str:find(t[1], 1, true) then | |
return unpack(t) | |
end | |
end | |
return ... | |
end | |
local function win_sp(...) | |
local COMMANDS = { | |
{[[Reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CSDversion]], "Service%s*Pack%s*(%d+)"}, | |
{[[Reg query "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v ServicePack]],"Service%s*Pack%s*(%d+)"}, | |
} | |
for _, cmd in ipairs(COMMANDS) do | |
local str, err = exec(cmd[1]) | |
if not str then return nil, err end | |
local ver = str:match(cmd[2]) | |
if ver then return tonumber(ver) end | |
end | |
return ... | |
end | |
local function get_win_val(arr) | |
local ver, err = win_ver('5.0') | |
if not ver then return nil, err end | |
local sp, err = win_sp(0) | |
if not sp then return nil, err end | |
for _, row in ipairs(arr) do | |
local key, val = row[1], row[2] | |
local kv, ks = key[1], key[2] | |
if kv == ver then | |
if not ks then return val end | |
if sp >= ks then return val end | |
end | |
end | |
end | |
-- http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx | |
local _WINNT_VER = { | |
{{"6.2" }, "0x0602"}, -- Win8 | |
{{"6.1" }, "0x0601"}, -- Win7 | |
{{"6.0" }, "0x0600"}, -- Vista, Win2k8 | |
{{"5.2", 1}, "0x0502"}, -- Win2k3 SP1 | |
{{"5.1", 2}, "0x0502"}, -- WinXP SP2 | |
{{"5.2" }, "0x0501"}, -- Win2k3 | |
{{"5.1" }, "0x0501"}, -- WinXP | |
{{"5.0" }, "0x0500"}, -- Win2k | |
} | |
-- http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx | |
local NTDDI_VERSION = { | |
{{"6.2", }, "0x06010000"}, -- Win8 | |
{{"6.1", }, "0x06010000"}, -- Win7 | |
{{"6.0", 1}, "0x06000100"}, -- Vista SP1, Win2k8 --??? | |
{{"6.0", }, "0x06000000"}, -- Vista | |
{{"5.2", 2}, "0x05020200"}, -- Win2k3 SP2 | |
{{"5.2", 1}, "0x05020100"}, -- Win2k3 SP1 | |
{{"5.2", }, "0x05020000"}, -- Win2k3 | |
{{"5.1", 3}, "0x05010300"}, -- WinXP SP3 | |
{{"5.1", 2}, "0x05010200"}, -- WinXP SP2 | |
{{"5.1", 1}, "0x05010100"}, -- WinXP SP1 | |
{{"5.1", }, "0x05010000"}, -- WinXP | |
{{"5.0", }, "0x05000000"}, -- Win2k -- ??? | |
} | |
print(get_win_val(_WINNT_VER)) | |
print(get_win_val(NTDDI_VERSION)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment