Write Reg IE Shell Version.fh_lua

@Title:			Write Reg IE Shell Version
@Type:				Standard
@Author:			Mike Tate
@Version:			1.3
@LastUpdated:	19 Dec 2020
@Licence:			This plugin is copyright (c) 2020 Mike Tate & contributors and is licensed under the MIT License which is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description:	Write Windows Registry Internet Explorer Shell Version for FH Web Search Windows, Plugin Help pages & Google Map windows, etc.
@V1.3:				FH V7 Lua 3.5 IUP 3.28;
@V1.2:				Changed recommended IE Shell Version from 9 to 11;
@V1.1:				Initial Plugin Store version;

require "luacom"																	-- To access COM subsystem for WScript.Shell

require "iuplua"																	-- To support IUP GUI iup.GetParam(...)

iup.SetGlobal("CUSTOMQUITMESSAGE","YES")										-- Needed for IUP 3.28

function GetRegKey(strKey)														-- Read Windows Registry Key Value
	local luaShell = luacom.CreateObject("WScript.Shell")
	local anyValue = nil
	if pcall( function() anyValue = luaShell:RegRead(strKey) end ) then
		return anyValue																-- Return Key Value if found
	return nil
end -- function GetRegKey

function PutRegKey(strKey,anyValue,strType)									-- Write Windows Registry Key Value
	local luaShell = luacom.CreateObject("WScript.Shell")
	local strAns = nil
	if pcall( function() strAns = luaShell:RegWrite(strKey,anyValue,strType) end ) then
		return true
	return nil
end -- function PutRegKey

function paramAction(iupDialog,intIndex)
	if intIndex == iup.GETPARAM_MAP then										-- Correct button labels needed for IUP 3.28 bug	-- V1.3
		iupDialog.Button1.Title = "Apply Change"								-- Otherwise remain as OK and Cancel
		iupDialog.Button2.Title = "Cancel Plugin"
	return 1
end -- function paramAction

function Main_GUI()
	local strHKLMKey = "HKLM\\SOFTWARE\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION\\fh.exe"
	local intCurrent = GetRegKey(strHKLMKey) or 0							-- Current key is typically 9000, or 11000, or nil if undefined
	local strCurrent = tostring( intCurrent )
	local strVersion = tostring( intCurrent / 1000 )						-- Current version is Key / 1000
	local intDefault = math.max(9,math.floor(strVersion))					-- Default version is larger of 9 or current version
	if strVersion == "0" then
		strVersion = "6 or 7"														-- Undefined current version
		strCurrent = "None"
	local isOK, intDesired =														-- Prompt user for desired IE Version	-- V1.3
	iup.GetParam( "Write Registry Internet Explorer Shell Version", paramAction,
		"is the Windows Registry key for the Internet Explorer Shell Version used in Family Historian browser windows.\r\r"..
		"Current key value is '"..strCurrent.."' that means Internet Explorer Shell Version "..strVersion.." is used within Family Historian.\r\r"..
		"Select the desired Internet Explorer Shell Version value, where Version 11 is strongly recommended :\r\r\r"..
		"Often FH must be opened using 'Run as administrator' for this Plugin change to take effect.\r\r%i[6,12,1]\n"..
		"%u[Apply Change,Cancel Plugin]\n",
	if isOK then
		local strFailure = "Failed to Write Registry Internet Explorer Version {version}\n\nEnsure you are logged into an Administrator account\n&/or\nOpen Family Historian using 'Run as administrator'\n\nThen run this Plugin again . . ."
		local strVersion = tostring( intDesired )								-- Desired version number
		intDesired = tonumber( intDesired * 1000 )							-- Desired key = Version * 1000
		if PutRegKey(strHKLMKey,intDesired,"REG_DWORD") then
			if GetRegKey(strHKLMKey) == intDesired then
				fhMessageBox("Wrote the Registry Internet Explorer Version "..strVersion.." OK\n\nNow please Restart your PC for it to take effect")
			fhMessageBox((strFailure:gsub("{version}","at all")))
end -- Main_GUI