Retrieve default internet timeout values?

2019-08-12 21:07发布

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条回答
放荡不羁爱自由
2楼-- · 2019-08-12 21:35

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));
查看更多
登录 后发表回答