Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:00:28+00:00 2026-06-15T09:00:28+00:00

Note: This code is in Delphi XE2. I am trying to download a file

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:00:28+00:00Added an answer on June 15, 2026 at 9:00 am

    I improved a bit your routine and it does the work for me:

    procedure DownloadFile(URL: string; Path: string);
    const
      BLOCK_SIZE = 1024;
    var
      InetHandle: Pointer;
      URLHandle: Pointer;
      FileHandle: Cardinal;
      BytesRead: Cardinal;
      DownloadBuffer: Pointer;
      Buffer: array [1 .. BLOCK_SIZE] of byte;
      BytesWritten: Cardinal;
    begin
      InetHandle := InternetOpen(PWideChar(URL), 0, 0, 0, 0);
      if not Assigned(InetHandle) then RaiseLastOSError;
      try
        URLHandle := InternetOpenUrl(InetHandle, PWideChar(URL), 0, 0, 0, 0);
        if not Assigned(URLHandle) then RaiseLastOSError;
        try
          FileHandle := CreateFile(PWideChar(Path), GENERIC_WRITE, FILE_SHARE_WRITE, 0,
            CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
          if FileHandle = INVALID_HANDLE_VALUE then RaiseLastOSError;
          try
            DownloadBuffer := @Buffer;
            repeat
              if (not InternetReadFile(URLHandle, DownloadBuffer, BLOCK_SIZE, BytesRead) 
                 or (not WriteFile(FileHandle, DownloadBuffer^, BytesRead, BytesWritten, 0)) then
                RaiseLastOSError;
            until BytesRead = 0;
          finally
            CloseHandle(FileHandle);
          end;
        finally
          InternetCloseHandle(URLHandle);
        end;
      finally
        InternetCloseHandle(InetHandle);
      end;
    end;
    

    For example a call:

      DownloadFile
        ('https://dl.dropbox.com/u/21226165/XE3StylesDemo/StylesDemoSrcXE2.7z',
        '.\StylesDemoXE2.7z');
    

    Works like a charm.

    The changes I made is:

    • Providing a buffer
    • Checking the result of the call to WriteFile and raising an exception if it is false or if the number of bytes written is different from the number of bytes read.
    • Changed the variable name.
    • Named constant
    • [after edited] Proper Function result checking
    • [after edited] Resource leak protection with try/finally blocks

    Edit Thanks TLama for raising aware about the last two points.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: I am using Delphi XE2 I am trying to add a registry key.
this code is correct?? String note = text.txt; FileWriter file = new FileWriter(note); Scanner
The code I'm using in my collision detection code is this: (note: Vector3f is
let's say I have a html structure like this -> (note: in my code
NOTE: This question is written in a C# like pseudo code, but I am
Please note this code was not written by me. Otherwise I would not be
Note: This code actually codes from a tutorial book I'm reading at the moment,
System.Diagnostics.Contracts.ContractException is not accessible in my test project. Note this code is purely myself
(Note: This is not a question about what is the best way with code
Note, this isn't my code, or the actual code in the form. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.