Search code examples
inno-setuppascalscript

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


I have the following function that raises an 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;

Instead obtaining the error in a string GetExceptionMessage, I would like to obtain the error as an integer.

Is this possible?


Solution

  • Exceptions do not have any code in general. So there's no generic way to retrieve a code from an exception.

    In this particular case the exception is based on an OLE automation error, which does have a code. Though, the code is not preserved, when the Inno Setup raises the exception for IDispatch interfaces (OLE).


    What you can do, is to use the IUnknown interface. For that you need to:

    • define a Pascal-style interface for the IWinHttpRequest
    • define CLSID_WinHttpRequest
    • instantiate the object using the CreateComObject (instead of the CreateOleObject).
    • call the Send method defined as function Send(Body: Variant): HResult
    • check the returned HResult for 12038 (ERROR_INTERNET_SEC_CERT_CN_INVALID).

    Particularly the first step is a huge and an error-prone task.


    For examples and details, see:


    If you decide to stick with error message comparison, you may try to use the 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.