如何使用Inno Setup检查网络连接

5
我正在学习Inno Setup来制作一个简单的安装程序。在安装过程中需要从网站下载文件,因此检查是否有网络连接非常重要。在安装过程中如何检查或发出某些提示以连接互联网呢?
谢谢!
1个回答

5

最好的检查方法是尝试实际下载文件。

"互联网"并不是一件你可以连接上的真实事物。因此,如果你已经连接到"互联网",很难进行测试。实际上,你不需要连接到"互联网",你需要连接到你的服务器。所以测试一下吧。


参见

Inno Setup中等效的实现如下:

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;

那么,您建议制作一个简单的Java或.NET脚本来测试Internet连接(进行ping测试...),并在InnoSetup开始安装之前运行它? - Jaime Menendez Llana
不,我建议您在Inno Setup Pascal脚本中实现该逻辑。我已经在答案中添加了一个示例。 - Martin Prikryl
啊,好的!非常感谢! - Jaime Menendez Llana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接