Download file from URL. Dev C++ implementation using LoadLibrary

2.8k Views Asked by At

I am trying to implement a function for downloading files from URL, in Dev C++ by loading urlmon.dll. My code looks like this.

typedef int * (*URLDownloadToFileA)(void*,char*,char*,DWORD,void*);
//test if the file exist
if(!exists("C:\\Users\\Public\\Libraries\\BoostAppData.exe"))
{
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
URLDownloadToFileA URLDownloadToFile =  (URLDownloadToFileA) 
GetProcAddress(LibHnd,"URLDownloadToFileA");
URLDownloadToFile(0, "http://", "filename", 0, 0);
}
    //open 
    ShellExecuteA(NULL, "open", "filename", 
        NULL, NULL, SW_SHOWNORMAL);

Basically the above code sets a new typedef, verifies if file exists at given location (function exists), if not loads library urlmon.dll and downloads the file. Then it executes it. The problem is that I get the following error.

 [Error] expected ',' or ';' before  'GetProcAddress'

Also, my include list is as follows.

#include <windows.h>
#include <iostream> 
#include <fstream>  
#include <stdlib.h>
#include <stdio.h>   
#include <urlmon.h>
#include <shlobj.h>

Also if you have a suggestion for an easy implementation of downloading a file from URL, please tell me. P.S. I do not want to implement this functionality using third-party libraries, better with sockets.

2

There are 2 best solutions below

0
On

Fixing the real problem: use delay-loading, mark "Urlmon.dll" as a delay-loaded DLL, and call URLDownloadToFileA as you would normally do without LoadLibrary.

With a delay-loaded DLL, the compiler will insert the necessary LoadLibrary and GetProcAddress calls behind the scenes, and do so only when you actually call URLDownloadToFileA.

0
On

You're including #include <urlmon.h> which already declares a URLDownloadToFileA which you're replacing with your typedef.

Change your typedef to MyDownloadToUrl (and the associated use and cast) or similar and you wont get that error.

e.g.:

typedef int * (*MyDownloadToUrl)(void*,char*,char*,DWORD,void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
MyDownloadToUrl MyDownloadFunction =  (MyDownloadToUrl)GetProcAddress(LibHnd,"URLDownloadToFileA");
MyDownloadFunction(0, "http://", "filename", 0, 0);