Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it safe to assume InternetCloseHandle() won't fail, thereby allowing cleaner code?
    primarykey
    data
    text
    <p>Here's a routine to do an HTTP request using WinINet and either return the fetched string or raise an exception:</p> <pre><code>function Request(const pConnection: HINTERNET; const localpath: string): string; var Buffer: packed Array[1..5000] of Char; BytesRead: Cardinal; pRequest: HINTERNET; sent: boolean; begin Result := ''; pRequest := HTTPOpenRequest(pConnection, 'GET', pchar(localpath), nil, nil, nil, 0, 0); if pRequest &lt;&gt; nil then begin sent := HTTPSendRequest(pRequest, nil, 0, nil, 0); if sent then while InternetReadFile(pRequest, @Buffer, SizeOf(Buffer)-1 {leave room for terminator}, BytesRead) do begin Buffer[BytesRead+1] := #0; Result := Result + buffer; end; InternetCloseHandle(pRequest); if not sent then RaiseLastOSerror; // HTTPSendRequest failed end else RaiseLastOSerror; // HTTPOpenRequest failed end; </code></pre> <p>If InternetCloseHandle(pRequest) can fail even though pRequest was successfully assigned, GetLastError() will return an error code for InternetCloseHandle() instead of HTTPSendRequest(). Fixing that would require code like:</p> <pre><code>function Request(const pConnection: HINTERNET; const localpath: string): string; var Buffer: packed Array[1..5000] of Char; BytesRead: Cardinal; pRequest: HINTERNET; begin Result := ''; pRequest := HTTPOpenRequest(pConnection, 'GET', pchar(localpath), nil, nil, nil, 0, 0); if pRequest &lt;&gt; nil then begin if HTTPSendRequest(pRequest, nil, 0, nil, 0) then while InternetReadFile(pRequest, @Buffer, SizeOf(Buffer)-1 {leave room for terminator}, BytesRead) do begin Buffer[BytesRead+1] := #0; Result := Result + buffer; end else begin InternetCloseHandle(pRequest); RaiseLastOSerror; // HTTPSendRequest failed end; InternetCloseHandle(pRequest); end else RaiseLastOSerror; // HTTPOpenRequest failed end; </code></pre> <p>but that seems a lot uglier and more confusing at first glance.</p> <p>Is it safe to assume InternetCloseHandle() won't fail, thereby allowing the simpler code?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload