How to download files from the internet using C++

Hello, Coders! In this tutorial, we will learn to download files from the internet using C++.

Win32 API UrlBlockingStream

Let’s have a look at the Win32 API UrlBlockingStream function.

  • This is an extract from MSDN documentation. URL Blocking Stream is a component object model COM function.
  • It downloads the data from the Internet by creating a blocking-type stream object. By applying the IStream::Read method, the client application can read the downloaded data.
  • URL Blocking Stream accepts five arguments.


HRRESULT URLOpenBlockingStream (

               //Keep Null, not relevant in our case.
                LPUNKNOWN   pCaller,

               // URL Address(
                LPCSTR      szURL, 

               //IStream** input pointer address.
               //URL Blocking Stream uses this address to     create a stream object for us.
                LPSTREAM    *ppStream, 

              _Reserved_DWORD  dwReserved,    //Kept Null the last two parameters.


In this tutorial, we will download the homepage of facebook as a file to our system by using the URL Blocking Stream. The URL Blocking Stream will provide us with an Istream object, then we shall install a loop to collect all the available bytes, and finally display them into a console output.

Program to Download text file From Internet to the Console in C++

#pragma comment(lib, "Urlmon.lib") //urlmon library for the URL Open Blocking Stream function

#include <urlmon.h>
#include <cstdio>
#include <iostream>   //Headers for String Operations
#include <string>

using namespace std;

int main()

  //WIndows IStream interface
  IStream* stream;

  //Source URL
  const char* URL = "";

  // URLDownloadToFile returns S_OK on success 
  if (S_OK != URLOpenBlockingStreamA(0, URL, &stream, 0, 0))

    cout << "Failed.";  //Return On Failure
    return -1;


 char buff[100]; //char buffer to repeatedly get data from the stream.
 string s;
 unsigned long bytesRead;

 while(true) {
     stream -> Read(buff,100,&bytesRead); //It Read the data till available
      if(0U == bytesRead) // bytesRead are zero when end of file reached.
 s.append(buff, bytesRead); //append and collect to the string.
   stream -> Release(); //Realease the interface.

  cout << s << endl;
  return 0;



We will get the Source Code file of home page on our output console.

Download Multimedia Files from the internet to the System by using C++

To download the multimedia files like image files in JPG format, PNG format, and Music files like MP3 or any other file format, we would use the Win32 API URLDownloadToFile. It will download the file from the URL of that file.

#pragma comment(lib, "Urlmon.lib")

#include <windows.h>
#include <cstdio>
using namespace std;

int main()

  // URL of the music file, image file etc.
  const wchar_t* sURL = L"";

  // Name of the Destination file 
  const wchar_t* dFile = L"image.jpg";

  if (S_OK == URLDownloadToFile(NULL, sURL, dFile, 0, NULL))

   cout << "The file is saved to image.png" << endl;
   return 0;



    cout << "Unable to Download the file.";
    return -1;




The image file is saved in the same working directory where the program is running.

Hope this article has helped you understand how to download any file from the internet by using the C++ programming language.

Happy Coding!!!

You can also read, Fetch line from a text file in C++

Leave a Reply

Your email address will not be published. Required fields are marked *