2014-10-08 118 views
0

這裏網頁的HTML內容是我的代碼我怎樣才能下載使用C++

#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; 
} 

這裏有錯誤,我遇到

error LNK2019: unresolved external symbol [email protected] referenced in function _main 
error LNK2019: unresolved external symbol [email protected] referenced in function _main 
error LNK2019: unresolved external symbol [email protected] referenced in function _main 
error LNK2019: unresolved external symbol [email protected] referenced in function _main 

我不明白的地方,我錯了。我已經導入了Wininet.h和Windows.h。爲什麼定位這些功能仍然有困難?問候。

+4

你必須做多添加WinInet.h到源 - 你必須真正鏈接庫。這就是你得到鏈接錯誤的原因。在項目中包含Wininet.lib :(項目 - >屬性 - >配置屬性 - >鏈接器 - >輸入 - >其他依賴項) – SubSevn 2014-10-08 16:38:29

+0

因爲他們只是告訴編譯器如何調用函數。他們實際上不包括代碼。查看所提到功能的MSDN頁面,並確保鏈接它們包含的庫。simples。 – enhzflep 2014-10-08 16:39:23

+0

這是一個鏈接器錯誤,因此編譯器很可能找不到WinInet庫文件。雖然函數是在WinInet.h中聲明的,但編譯器需要訪問實際的目標代碼(位於WinInet.lib文件中)才能正確編譯程序。 – Moritz 2014-10-08 16:41:13

回答

0

你所做的是包含頭文件,但沒有鏈接到實現它的實際代碼。

將Wininet.lib包含在您的項目中。

項目 - >屬性 - >配置屬性 - >連接器 - >輸入 - >附加依賴