Note: This code is in Delphi XE2.
I am trying to download a file without using UrlMon.dll.
I would like to use only wininet. This is what I have come up with so far:
uses Windows, Wininet;
procedure DownloadFile(URL:String;Path:String);
Var
InetHandle:Pointer;
URLHandle:Pointer;
FileHandle:Cardinal;
ReadNext:Cardinal;
DownloadBuffer:Pointer;
BytesWritten:Cardinal;
begin
InetHandle := InternetOpen(PWideChar(URL),0,0,0,0);
URLHandle := InternetOpenUrl(InetHandle,PWideChar(URL),0,0,0,0);
FileHandle := CreateFile(PWideChar(Path),GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,0);
Repeat
InternetReadFile(URLHandle,DownloadBuffer,1024,ReadNext);
WriteFile(FileHandle,DownloadBuffer,ReadNext,BytesWritten,0);
Until ReadNext = 0;
CloseHandle(FileHandle);
InternetCloseHandle(URLHandle);
InternetCloseHandle(InetHandle);
end;
I think the issue is with my loop and "ReadNext". When this code is executed, it create's the file in the correct path, yet the code finishes and the file is 0 bytes.