AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi Wininet asynchronous download
Thema durchsuchen
Ansicht
Themen-Optionen

Wininet asynchronous download

Ein Thema von Inspur1 · begonnen am 27. Sep 2024 · letzter Beitrag vom 6. Nov 2024
 
Inspur1

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

Wininet asynchronous download

  Alt 27. Sep 2024, 11: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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:51 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz