ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web

Dev C++ Networking tutorial- Interaction with internet using Wininet

Updated on May 15, 2018

This article serves the purpose of internet addicts on creating their first desktop application written in c++ to connect International network (i.e. Internet). I learned this method just doing a simple research on one of the most popular library available with MinGW compiler i.e. Wininet (Provided by Microsoft to use it on Win OS). Beginners can use Devc++ as it has built in MinGW compiler or any IDE which supports MinGW Compiler (Recommended).

Let's be Practical,

  1. Create a new project with any name,
  2. Then create a c++ file named as "main.cpp",
  3. Include these library's as shown below in code.

"<cstring>" is optional, you can use it to define a variable to store web address.

Important Note:You can face a compilation error while compiling or debugging, So, Please make sure that you have linked your library with project in any IDE before compiling this script. You can check out the help section of particular IDE or Just go to the linker section of any IDE and Add the library which is located here: "C:\MinGW\lib\libwininet.a" (Default Location).

In Devc++, You can link via Project Menu -> compiler -> Then, Look at something specified with linker name. I Use Netbeans IDE and sometimes Codeblocks for my Projects, DevC++ is good for beginners (I used it too...).

Internet map
Internet map | Source

Now, create the main function followed by int and see the code below. This is the most basic code for connecting to internet via c++.

First, We declare a variable "connect" with datatype HINTERNET (Explained below) to store data (Tecnically named as 'handle') returned by InternetOpen() function. InternetOpen() does the basic work of checking and connecting network automatically via Windows Operating System.

Second, We declare another variable OpenAddress with same HINTERNET datatype to store data returned (Another handle) by InternetOpenUrl() Function while using the previous handle (data) returned by InternetOpen() - This function does the work of making a connection to address specified in it's argument.

We are using if statements or conditions in between the code to Check if functions and library is working properly or not. There is a GetLastError() Function in scond if statement that does the work of identifying and printing the error Number/code if our application failed to load a given Web-address. You can Learn more more about such errors here at official microsoft help center .

using namespace std;
int main(){

      cout<<"Connection Failed or Syntax error";
      return 0;

HINTERNET OpenAddress = InternetOpenUrl(connect,"http://localhost/", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);

   if ( !OpenAddress )
      DWORD ErrorNum = GetLastError();
      cout<<"Failed to open URL \nError No: "<<ErrorNum;
      return 0;

   char DataReceived[4096];
   DWORD NumberOfBytesRead = 0;
   while(InternetReadFile(OpenAddress, DataReceived, 4096, &NumberOfBytesRead) && NumberOfBytesRead )
           cout << DataReceived;



Third, We declare two another variable DataReceived, NumberOfBytesRead for storing Data received via InternetReadFile() From Network (Internet). We are using while loop condition to continuously receive data till the size specified (4096) and Outputting it to console window via cout. Here, InternetReadFile() uses the data (Handle) returned by InternetOpenUrl() Function to receive data from a specified Location.

At Last, We are closing all the connections and destroying handles to successfully close application without causing any network problems via InternetCloseHandle(). system("PAUSE") Stops the screen from closing to display output to user, programmer or anyone who is interacting with application.

Basic Parts of code are explained here:

  1. HINTERNET, DWORD- Kind of Data Type to define variables similar to that of INT, Double, CHAR etc. , Just remember it.
  2. InternetOpen(), InternetOpenUrl(), GetLastError(), InternetReadFile(), InternetCloseHandle() - Functions defined in wininet library for clients to use it in their code to access internet.
  3. INTERNET_FLAG_PRAGMA_NOCACHE, INTERNET_FLAG_KEEP_CONNECTION, NULL, 0 - Are the default values of function attributes that can be used.
  4. system("PAUSE") - A built-in function of MinGW compiler to keep console window open to show output to programmer.
  5. Optional Tip - To convert Datatype string into const char*, you can do this way const char* variableName = StringVariable.c_str() as long as I remember.

You can Learn more about Wininet functions here at official microsoft Page -

Hope, Your Interest in c++ has now grown up little more...

My Quote : Programming may disclose most of the facts behind the working of real world as anything doesn't works itself.


    0 of 8192 characters used
    Post Comment

    • profile image

      UnWorld 21 months ago

      Hello, this is a great tutorial. I have used this to successfully download files from the internet. There is one concern though. The downloaded file size is always bigger than the original because of the extra 4096 bytes completed at the end. Do you know how to fix this overflow problem?

    • profile image

      4 years ago

      Don't use Win API directly, save the time. Use the portable high-level frameworks instead like Boost.Asio (, C++ Network Library ( or Unicomm ( Stop using strcpy and etc. approach. ;)

    • profile image

      Poseidon 5 years ago

      *giggle* I'm about to laugh. (The guy above)

    • profile image

      sidahmed 5 years ago

      how i can make hacker account connect to internet

    • seigfried23 profile image

      seigfried23 5 years ago

      I'd love t use that picture of worldwide internet connectivity. Great hub - a bit too technical for me lol; but then that's my fault.

    • profile image

      Martin 5 years ago

      When I try to use this, i always get a char full of EndOfFile symbols. I use exactly the same code, the only change i've made is adding a pragma comment to wininet.lib and including windows.h. Could you guess, what could be the probleb by me? Thanks Martin

    • itech profile image

      Krishna 6 years ago from Dausa, India

      Google has everything!!! U just need to about correct keyword in order to get exact results.

    • profile image

      Sumit 6 years ago

      Do we have something similar on Linux available?

    • itech profile image

      Krishna 6 years ago from Dausa, India

      Yes, Wininet is a library provided by windows. So, application developed using this would run at least on windows.

    • profile image

      Sumit 6 years ago

      I want to write a client application which access webpages/urls in C++. Is winINet going to help in it? The app is to be a commandline app. It accesses some APIs on servers and gets data

    • profile image

      Hos 6 years ago

      Very good.

    • profile image

      Krissie_59 6 years ago

      Pretty picture :)


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: ""

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)