How to dowload a file in C

//folder C:/vector1 and C:/vector1/vector1 must exist
#include <windows.h>
#include <stdio.h>
typedef HRESULT (WINAPI *UDTF)(LPVOID, LPCTSTR, LPCTSTR, DWORD, LPVOID);

int main(void)
{
int r = 1;
HMODULE hDll;
UDTF URLDownloadToFile;
//string url = "http://www.axmag.com/download/pdfurl-guide.pdf";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if((hDll = LoadLibrary("urlmon")))
{
if((URLDownloadToFile = (UDTF)GetProcAddress(hDll, "URLDownloadToFileA")))
{
if(URLDownloadToFile(0, "http://www.axmag.com/download/pdfurl-guide.pdf", "C:/vector1/pdf.pdf", 0, 0) == 0)
r = 0;
}
FreeLibrary(hDll);
}
printf("%s\n", r ? "Echec" : "Ok");
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if((hDll = LoadLibrary("urlmon")))
{
if((URLDownloadToFile = (UDTF)GetProcAddress(hDll, "URLDownloadToFileA")))
{
if(URLDownloadToFile(0, "http://www.axmag.com/download/pdfurl-guide.pdf", "C:/vector1/vector1/pdf.pdf", 0, 0) == 0)
r = 0;
}
FreeLibrary(hDll);
}
printf("%s\n", r ? "Echec" : "Ok");

getchar();
return r;
}
Last edited on
Your code looks awfully complicated. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <windows.h>
#include <Urlmon.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "Urlmon.lib") /* remove if not using Visual Studio */

int main(void)
{
  TCHAR szUrl[] = _T("http://www.axmag.com/download/pdfurl-guide.pdf");
  TCHAR szFileName[] = _T("C:\\vector1\\pdf.pdf");

  if (URLDownloadToFile(0, szUrl, szFileName, 0, 0 ) != S_OK)
  {
    printf("Windows error: %u", GetLastError());
    getchar();
    return -1;
  }

  printf("\nDownload ok. :)");

  getchar();
  return 0;
}
Topic archived. No new replies allowed.