我需要獲取WinInet函數錯誤代碼的說明,有關WinInet函數的MSDN文檔說明我必須使用GetLastError
函數檢索上次錯誤一個函數失敗時的代碼。現在,當我檢查關於GetLastError
函數的文檔說。我如何檢索來自delphi的WinInet錯誤代碼的錯誤描述
.To obtain an error string for system error codes, use the FormatMessage function
我檢查其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,而不是'代碼:= GetLastError函數;提出Exception.Create(格式('錯誤%d說明%s',[代碼,SysErrorMessage(代碼)]));' –
@Gerry,謝謝,但我試過你的建議,並沒有檢索任何關於錯誤的描述。 – Salvador