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?
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:
IWinHttpRequest
CLSID_WinHttpRequest
CreateComObject
(instead of the CreateOleObject
).Send
method defined as function Send(Body: Variant): HResult
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:
Examples\CodeAutomation2.iss
in your Inno Setup installation.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.