【发布时间】:2011-09-30 03:31:59
【问题描述】:
我需要获取 WinInet 函数错误代码的描述,有关 WinInet 函数的 MSDN 文档说,当函数失败时,我必须使用 GetLastError 函数来检索最后一个错误代码。现在,当我查看有关 GetLastError 函数的文档时说。
.获取系统错误字符串 错误代码,使用 FormatMessage函数
我检查SysErrorMessage delphi 函数内部调用了FormatMessage winapi 函数,所以我使用该函数来检索错误描述,但不工作(我的意思是不返回 WinInet 错误代码的描述) 我在 Delphi 2007 和 Delphi XE 中测试了这段代码。
查看此代码
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.
当我执行此代码时,返回代码 12006,定义为 ERROR_INTERNET_UNRECOGNIZED_SCHEME,相关描述为 The URL scheme could not be recognized or is not supported.
所以问题是How I can retrieve the error description for the WinInet error codes in delphi?
【问题讨论】:
-
不是问题的解决方案,但您可以使用 RaiseLastOSError 代替
Code:=GetLastError; raise Exception.Create(Format('Error %d Description %s',[Code,SysErrorMessage(Code)])); -
@Gerry,谢谢,但我尝试了您的建议,但没有检索到有关错误的任何描述。
标签: delphi winapi error-handling wininet