【问题标题】:How i can retrieve the error description for a WinInet error code from delphi我如何从 delphi 中检索 WinInet 错误代码的错误描述
【发布时间】: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


【解决方案1】:

我认为您应该尝试直接使用 FormatMessage,因为您需要知道错误代码的来源。我找到了这个工作代码。

class function TCertificateManager.GetLastErrorText: string;
var
  code: DWORD;
  Len: Integer;
  Buffer: array[0..255] of Char;
begin
  code := GetLastError();
  Len := FormatMessage(FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM,
  Pointer(GetModuleHandle('Advapi32.dll')), code, 0, Buffer, SizeOf(Buffer), nil);
  while (Len > 0) and (Buffer[Len - 1] in [#0..#32, '.']) do Dec(Len);
  SetString(Result, Buffer, Len);
end;

您应该进行一些更改,可能使用“wininet.dll”而不是 Advapi32.dll,但它应该可以工作。

更新

这是 WinInet 函数的版本

function GetWinInetError(ErrorCode:Cardinal): string;
const
   winetdll = 'wininet.dll';
var
  Len: Integer;
  Buffer: PChar;
begin
  Len := FormatMessage(
  FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM or
  FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or  FORMAT_MESSAGE_ARGUMENT_ARRAY,
  Pointer(GetModuleHandle(winetdll)), ErrorCode, 0, @Buffer, SizeOf(Buffer), nil);
  try
    while (Len > 0) and {$IFDEF UNICODE}(CharInSet(Buffer[Len - 1], [#0..#32, '.'])) {$ELSE}(Buffer[Len - 1] in [#0..#32, '.']) {$ENDIF} do Dec(Len);
    SetString(Result, Buffer, Len);
  finally
    LocalFree(HLOCAL(Buffer));
  end;
end;

【讨论】:

  • @ErvinS,我更新了您的答案,添加了我使用的功能,以帮助其他有相同问题的用户。 :) 和 +1 为你回答
  • 示例用法存在于 Soap.SOAPHTTPTrans.pas 过程中 THTTPReqResp.RaiseCheck(ErrCode: DWORD; ShowSOAPAction: Boolean);和 Datasnap.Win.SConnect 过程 TWebConnection.Check(Error: Boolean);
【解决方案2】:

对我来说,这个错误看起来不错 - 我不知道任何名为“foo”的 URL 方案:)

尝试使用类似“http://foo.bar”的 URL 来查看是否收到其他错误消息。

【讨论】:

  • 问题不是错误。问题是我无法检索错误描述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 2013-06-06
  • 2021-01-28
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多