Retrieve default internet timeout values?

て烟熏妆下的殇ゞ 提交于 2020-01-05 16:49:02

问题


I'm trying to retrieve the default values for the INTERNET_OPTION_SEND_TIMEOUT, INTERNET_OPTION_SEND_TIMEOUT, and INTERNET_OPTION_RECEIVE_TIMEOUT options flags. From what I read, they are in WinInit.

The below code fails to compile with Types of actual and formal var parameters must be identical, but which parameter is incorrect here?

procedure TFrmWininetTimeOuts.FormShow(Sender: TObject);
var
  hSession     : HINTERNET;
  dwTimeOut    : DWORD;
begin
  hSession := InternetOpen('usersession', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if not Assigned(hSession) then Exit;
  try
    InternetQueryOption(hSession, INTERNET_OPTION_RECEIVE_TIMEOUT, @dwTimeOut, SizeOf(dwTimeOut));
  finally
    InternetCloseHandle(hSession);
  end;
end;

Code completion says it needs a (pointer,cardinal,pointer,cardinal).
I see code examples with a call to InternetQueryOption(nil, (which also does not compile) or with an intermediate InternetOpenUrl but I guess I don't need that.


回答1:


As you can see by looking at the declaration in WinInet.pas, the final parameter of InternetQueryOption is a var parameter:

function InternetQueryOption(hInet: HINTERNET; dwOption: DWORD;
  lpBuffer: Pointer; var lpdwBufferLength: DWORD): BOOL; stdcall;

The function receives the length of the buffer, but also tells you how many bytes it wrote to your buffer, so the value you pass in that parameter needs to be modifiable. The constant SizeOf(dwTimeOut) is not modifiable.

Store the value in a variable, and then pass the variable in that parameter. Also make sure to check the return value of the API function. It won't throw an exception on error; it will return False.

var
  BufferSize: DWord;

BufferSize := SizeOf(dwTimeOut);
Win32Check(InternetQueryOption(hSession, INTERNET_OPTION_RECEIVE_TIMEOUT,
                               @dwTimeOut, BufferSize));


来源:https://stackoverflow.com/questions/30848428/retrieve-default-internet-timeout-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!