Inno Setup:如何获取引发的异常的异常代码?

Inno Setup: How can I get the Exception Code of a raised Exception?

我有以下引发异常的函数:

procedure Test();
var
  WinHttpReq: Variant;
begin
  WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
  try
    WinHttpReq.Open('POST', 'https://tv.eurosport.com/', False);
    WinHttpReq.SetRequestHeader('Content-Type', 'application/json');
    WinHttpReq.Send('');
  except
    MsgBox(GetExceptionMessage, mbError, MB_OK);
  end;
end;

我想以整数形式获取错误,而不是获取字符串 GetExceptionMessage 中的错误。

这可能吗?

异常一般没有任何代码。所以没有从异常中检索代码的通用方法。

在这种特殊情况下,异常是基于 OLE 自动化错误,它确实有一个代码。但是,当 Inno Setup 引发 IDispatch 接口 (OLE) 的异常时,代码不会保留。


您可以做的是使用IUnknown 界面。为此,您需要:

  • IWinHttpRequest
  • 定义 Pascal 风格的界面
  • 定义CLSID_WinHttpRequest
  • 使用 CreateComObject(而不是 CreateOleObject)实例化对象。
  • 调用定义为function Send(Body: Variant): HResult
  • Send方法
  • 检查返回的 HResult 是否为 12038 (ERROR_INTERNET_SEC_CERT_CN_INVALID)。

尤其是第一步是一项艰巨且容易出错的任务。


有关示例和详细信息,请参阅:


如果您决定坚持使用错误消息比较,您可以尝试使用 FormatMessage function to retrieve the system/localized error message to compare with. See How i can retrieve the error description for a WinInet error code from delphi