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;