Necesito obtener la descripción de un código de error de la función WinInet, La documentación de MSDN sobre las funciones de WinInet dice que debo usar la función GetLastError
para recuperar el último error código cuando una función falla. Ahora cuando miro la documentación sobre la función GetLastError
dice.Cómo puedo recuperar la descripción del error para un código de error WinInet de delphi
.To obtain an error string for system error codes, use the FormatMessage function
puedo comprobar la función SysErrorMessage
Delphi llama internamente a la función FormatMessage winapi, así que estoy usando esa función para recuperar la descripción del error, pero no está funcionando (me refiero no devuelve una descripción para un código de error de WinInet) Probé este código en Delphi 2007 y Delphi XE.
ver este código
uses
Wininet, Windows, SysUtils;
procedure TestWinInet(const AUrl : string);
var
hInter,hRemoteUrl : HINTERNET;
Code : Cardinal;
begin
hInter := InternetOpen(PChar('Explorer 5.0'), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hInter=nil then
begin
Code:=GetLastError;
raise Exception.Create(Format('Error %d Description %s',[Code,SysErrorMessage(Code)]));
end;
try
hRemoteUrl := InternetOpenUrl(hInter, PChar(AUrl), nil, 0, INTERNET_FLAG_RELOAD, 0);
if hRemoteUrl=nil then
begin
Code:=GetLastError;
raise Exception.Create(Format('Error %d Description %s',[Code,SysErrorMessage(Code)]));
end;
try
//do something else
finally
InternetCloseHandle(hRemoteUrl);
end;
finally
InternetCloseHandle(hInter);
end;
end;
begin
try
//i am passing a invalid url just to raise the error
TestWinInet('Foo');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
cuando ejecuto el código devuelve el código 12006 que se define como ERROR_INTERNET_UNRECOGNIZED_SCHEME
y la descripción asociada es The URL scheme could not be recognized or is not supported.
Así que la pregunta es How I can retrieve the error description for the WinInet error codes in delphi?
una solución al problema, pero se puede usar en lugar de 'RaiseLastOSError Código: GetLastError =; aumentar Exception.Create (Formato ('Error% d Descripción% s', [Código, SysErrorMessage (Código)])); ' –
@Gerry, gracias pero me trataron su sugerencia y no recuperar cualquier descripción sobre el error. – Salvador