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.
Fixing the real problem: use delay-loading, mark "Urlmon.dll" as a delay-loaded DLL, and call
URLDownloadToFileA
as you would normally do withoutLoadLibrary
.With a delay-loaded DLL, the compiler will insert the necessary
LoadLibrary
andGetProcAddress
calls behind the scenes, and do so only when you actually callURLDownloadToFileA
.