Einzelnen Beitrag anzeigen

Inspur1

Registriert seit: 28. Aug 2024
8 Beiträge
 
#1

Wininet asynchronous download

  Alt Heute, 10:35
Greetings fellows!

Ich arbeite mit Lazarus und habe ein Problem mit asynchronous download und Wininet.
HUrl ist immer nil, egal welche flags InternetOpenUrl gesetzt.

Delphi-Quellcode:
uses WinInet;

procedure StatusCallback(hInet: HINTERNET; dwContext: DWORD_PTR;
  dwInternetStatus: DWORD; lpvStatusInformation: Pointer;
  dwStatusInformationLength: DWORD); stdcall;
begin
  //
end;

function DownloadAsync(const Url, FileName: string; var Progress: Single): Boolean;
const
  BufferSize = 1024 * 5;
var
  hOpen, hUrl: HINTERNET;
  buffStruct: INTERNET_BUFFERS;
  bRead: LongBool;
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
          ShowMessage(SysErrorMessage(GetLastError))
      until buffStruct.dwBufferLength = 0;
      result := true;
    finally
      InternetCloseHandle(hUrl);
    end;
  finally
    InternetCloseHandle(hOpen);
  end;
end;
Ich habe noch eine method ohne asynchronous mode.
Das funktioniert aber das Progress wird im main thread nicht aktualisiert.

Delphi-Quellcode:
function Download(const Url, FileName: string; var Progress: Single): Boolean;
const
  BUFFER_SIZE = 1024 * 5;
var
  hOpen, hUrl: HINTERNET;
  buff: array[0..BUFFER_SIZE - 1] of Char;
  buffLen: DWORD;
  currentBytes, totalBytes: DWORD;
  f: File;
begin
  result := false;
  buffLen := SizeOf(buff);
  ZeroMemory(@buff, buffLen);
  currentBytes := 0;
  totalBytes := 0;
  hOpen := InternetOpen(PChar('Test'), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
  try
    hUrl := InternetOpenUrl(hOpen, PChar(Url), nil, 0, INTERNET_FLAG_DONT_CACHE, 0) ;
    try
      HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH, @buff, buffLen, totalBytes);
      totalBytes := StrToInt(string(buff));
      AssignFile(f, FileName);
      Rewrite(f, 1);
      repeat
        InternetReadFile(hUrl, @buff, SizeOf(buff), buffLen);
        currentBytes := currentBytes + buffLen;
        Progress := currentBytes / totalBytes;
        BlockWrite(f, buff, buffLen);
      until buffLen = 0;
      CloseFile(f) ;
      result := true;
    finally
      InternetCloseHandle(hUrl);
    end;
  finally
    InternetCloseHandle(hOpen);
  end;
end;
Funktioniert INTERNET_FLAG_ASYNC nicht mehr oder mach ich etwas falsch?
  Mit Zitat antworten Zitat