Einzelnen Beitrag anzeigen

Inspur1

Registriert seit: 28. Aug 2024
10 Beiträge
 
#3

AW: Wininet asynchronous download

  Alt 27. Sep 2024, 13:23
Delphi-Quellcode:
function DownloadAsync(const Url, FileName: string; var Progress: Single): Boolean;
const
  BufferSize = 1024 * 5;
var
  hOpen, hUrl: HINTERNET;
  buffStruct: INTERNET_BUFFERS;
  bRead: LongBool;
  e, eBuffLen: DWORD;
  eBuff: array[0..254] of Char;
begin
  result := false;
  hOpen := InternetOpen(PChar('Test'), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, INTERNET_FLAG_ASYNC) ;
  try
    InternetSetStatusCallback(hOpen, INTERNET_STATUS_CALLBACK(@StatusCallback));
    hUrl := InternetOpenUrl(hOpen, PChar(Url), nil, 0, INTERNET_FLAG_DONT_CACHE, 0) ;
    try
      repeat
        bRead := InternetReadFileEx(hUrl, @buffStruct, WININET_API_FLAG_ASYNC, 0);
        if not bRead then
        begin
          ShowMessage(SysErrorMessage(GetLastError)); // returns invalid handle
          ebufflen := SizeOf(eBuff);
          ZeroMemory(@eBuff, ebufflen);
          if InternetGetLastResponseInfo(e, @eBuff[0], ebuffLen) then
            ShowMessageFmt('%d, %d, %s', [e, eBuffLen, eBuff]); // returns 0, 0, ''
        end;
      until buffStruct.dwBufferLength = 0;
      result := true;
    finally
      InternetCloseHandle(hUrl);
    end;
  finally
    InternetCloseHandle(hOpen);
  end;
end;
  Mit Zitat antworten Zitat