0

Here is my code

#include <iostream>
#include <Windows.h>
#include <WinInet.h>

using namespace std;

int main()
{
    HINTERNET hSession, hURL;
    char* Buffer = new char[1024];
    DWORD BufferLen, BytesWritten;
    HANDLE FileHandle;

    hSession = InternetOpenA(NULL, 0, NULL, NULL, 0);
    hURL = InternetOpenUrlA(hSession, "http://www.google.co.uk", NULL, 0, 0, 0);

    FileHandle = CreateFileA("C:\\temp.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
    BytesWritten = 0;
    do
    {
        InternetReadFile(hURL, Buffer, 1024, &BufferLen);
        WriteFile(FileHandle, Buffer, BufferLen, &BytesWritten, NULL);
    } while (BufferLen != 0);
    CloseHandle(FileHandle);

    InternetCloseHandle(hURL);
    InternetCloseHandle(hSession);

    ShellExecuteA(0, "open", "C:\\temp.txt", NULL, NULL, 1);

    cout << "Operation complete!";
    system("PAUSE");
    return 0;
}

Here are the errors I am encountering

error LNK2019: unresolved external symbol __imp__InternetOpenA@20 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetCloseHandle@4 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetOpenUrlA@24 referenced in function _main
error LNK2019: unresolved external symbol __imp__InternetReadFile@16 referenced in function _main

I don't understand where I am going wrong. I have imported Wininet.h and Windows.h. Why is it still having trouble locating these functions? Regards.

Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
  • 4
    You have to do more than add WinInet.h to your source - you have to actually link the library. This is why you are getting linker errors. Include Wininet.lib in your project: (Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies) – SubSevn Oct 08 '14 at 16:38
  • Because they just tell the compiler how to call the functions. They dont actually include the code. Look at the MSDN page for the functions mentioned, and make sure you link the libraries that they're contained in. simples. – enhzflep Oct 08 '14 at 16:39
  • This is a linker error, so most likely the compiler can't find the WinInet library file. While the functions are declared in WinInet.h, the compiler needs access to the actual object code (which lives in the WinInet.lib file) to compile the program properly. – Moritz Oct 08 '14 at 16:41

1 Answers1

1

What you have done is include the header file, but not linked to the actual code which implements it.

Include Wininet.lib in your project.

Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies

SubSevn
  • 1,008
  • 2
  • 10
  • 27