我如何从delphi中检索WinInet错误代码的错误描述
发布时间:2020-12-15 04:03:14 所属栏目:大数据 来源:网络整理
导读:我需要获取WinInet函数错误代码的描述,关于WinInet函数的MSDN文档说明当函数失败时我必须使用 GetLastError 函数来检索最后一个错误代码.现在,当我查看有关 GetLastError 功能的文档说. .To obtain an error string for system error codes,use the 07002 fu
我需要获取WinInet函数错误代码的描述,关于WinInet函数的MSDN文档说明当函数失败时我必须使用
GetLastError 函数来检索最后一个错误代码.现在,当我查看有关
GetLastError 功能的文档说.
我检查 看到这段代码 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,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),INTERNET_FLAG_RELOAD,0); if hRemoteUrl=nil then begin Code:=GetLastError; raise Exception.Create(Format('Error %d Description %s',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,其定义为 所以问题是如何在delphi中检索WinInet错误代码的错误描述? 解决方法
我认为你应该尝试直接使用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,Buffer,SizeOf(Buffer),nil); while (Len > 0) and (Buffer[Len - 1] in [#0..#32,'.']) do Dec(Len); SetString(Result,Len); end; 您应该进行一些更改,可能使用’wininet.dll’而不是Advapi32.dll,但它应该可以工作. UPDATE 这是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,@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,Len); finally LocalFree(HLOCAL(Buffer)); end; end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |