Inno Setup detect Windows Firewall state

2019-08-11 04:59发布

问题:

I need to detect the Windows Firewall state (i.e. whether it is enabled or not) in order to display a message warning that a Firewall rule may need to be configured to allow inbound connections on specific ports when the Firewall is enabled, but not when it isn't. See below code example:

[Code]
//Check if Windows Firewall is enabled
function IsWindowsFirewallEnabled(): Boolean;
begin
//Method required here
  Result := True;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
//Display a warning message on a Server install if Windows Firewall is enabled
  if CurPageID = wpSelectComponents and IsComponentSelected('Server') and IsWindowsFirewallEnabled then
    begin
      MsgBox('Windows Firewall is currently enabled.' + #13#10 + #13#10 +
        'You may need to enable inbound connections on ports 2638, 445 and 7.'
        mbInformation, MB_OK);
      Result := True;
    end;
end;

What I need is a method for the IsWindowsFirewallEnabled function. One way I have read about, and ironically has now more or less been suggested below whilst I was in the middle of updating the question with this information anyway, would appear to be reading the EnableFirewall value from the Registry:

//Check if Windows Firewall is enabled
function IsWindowsFirewallEnabled(): Boolean;
var
  crdFirewallState: Cardinal;
begin
  RegQueryDwordValue(HKLM, 'SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile',
    'EnableFirewall', crdFirewallState);
  if crdFirewallState = 1 then
    Result := True;
end;

However, I am not convinced by this method as the Registry values for all the profiles show enabled on my work PC, but looking in Control Panel the Domain profile shows disabled (I assume this is related to a Group Policy).

Note, that this needs to work for both Windows XP and Server 2003, and for Windows Vista and Server 2008 and above.

Therefore, what's the most reliable or recommended way to do this?

回答1:

You would need to determine the registry entry and then query it in a manner similar to this using Innosetup's registry query ability.

var
  Country: String;
begin
  if RegQueryStringValue(HKEY_CURRENT_USER, 'Control Panel\International',
     'sCountry', Country) then
  begin
    // Successfully read the value
    MsgBox('Your country: ' + Country, mbInformation, MB_OK);
  end;
end;

http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_regquerystringvalue

Allegedly this is the information for the registry key:

Path: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\WindowsFirewall\DomainProfile Location: Local Machine Value Name: EnableFirewall Data Type: DWORD (DWORD Value) Enabled Value: 0 Disabled Value: 1