inno-setupinternet-connection

How to check Internet connection using Inno Setup


I'm learning about Inno Setup to make a simple installer. I need to download a file from a website during the installation, so it's important check if there is Internet connection. How can I check or take some alert to connect Internet during the process of the installation?

Thanks!


Solution

  • The best check is to try to actually download the file.

    "Internet" is hardly a real thing that you can connect to. So it's hard to test, if you are connected to "Internet". You actually do not need a connection to "Internet", you need a connection to your server. So test that.


    See also

    An equivalent implementation in Inno Setup would be like:

    function InitializeSetup(): Boolean;
    var
      WinHttpReq: Variant;
      Connected: Boolean;
    begin
      Connected := False;
      repeat
        Log('Checking connection to the server');
        try
          WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
          // Use your real server host name
          WinHttpReq.Open('GET', 'https://www.example.com/', False);
          WinHttpReq.Send('');
          Log('Connected to the server; status: ' + IntToStr(WinHttpReq.Status) +
              ' ' + WinHttpReq.StatusText);
          Connected := True;
        except
          Log('Error connecting to the server: ' + GetExceptionMessage);
          if WizardSilent then
          begin
            Log('Connection to the server is not available, ' +
                'aborting silent installation');
            Result := False;
            Exit;
          end
            else
          if MsgBox('Cannot reach server. Please check your Internet connection.',
                    mbError, MB_RETRYCANCEL) = IDRETRY then
          begin
            Log('Retrying');
          end
            else
          begin
            Log('Aborting');
            Result := False;
            Exit;
          end;
        end;
      until Connected;
        
      Result := True;
    end;