当前位置:  开发笔记 > 编程语言 > 正文

C++使用WinHttp/Windows SDK编译问题

如何解决《C++使用WinHttp/WindowsSDK编译问题》经验,为你挑选了1个好方法。

尝试使用SDK编译示例http类,并获得一些奇怪的链接错误...我确信它与缺少的选项或目录有关...

我不是c ++的专家,你可以看到,但寻找任何帮助.

我包括了我的样本课程.我也安装了Windows SDK.如果您需要有关我的设置或任何其他信息,请询问.我希望有人指出我正在运行的WinHttp SDK示例项目.

//START OF utils.cpp
#pragma once
#include "stdafx.h"

class http
{
public:
    http();
    ~http();

    std::string getText();

};
//END OF utils.cpp

//START OF utils.cpp
#include "stdafx.h"
#include "utils.h"

http::http()
{
}

http::~http()
{
}

std::string http::getText()
{
    DWORD dwSize = 0;
    DWORD dwDownloaded = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL, 
        hConnect = NULL,
        hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
        WINHTTP_NO_PROXY_NAME, 
        WINHTTP_NO_PROXY_BYPASS, 0 );

    // Specify an HTTP server.
    if( hSession )
        hConnect = WinHttpConnect( hSession, L"www.microsoft.com",
        INTERNET_DEFAULT_HTTPS_PORT, 0 );

    // Create an HTTP request handle.
    if( hConnect )
        hRequest = WinHttpOpenRequest( hConnect, L"GET", NULL,
        NULL, WINHTTP_NO_REFERER, 
        WINHTTP_DEFAULT_ACCEPT_TYPES, 
        WINHTTP_FLAG_SECURE );

    // Send a request.
    if( hRequest )
        bResults = WinHttpSendRequest( hRequest,
        WINHTTP_NO_ADDITIONAL_HEADERS, 0,
        WINHTTP_NO_REQUEST_DATA, 0, 
        0, 0 );


    // End the request.
    if( bResults )
        bResults = WinHttpReceiveResponse( hRequest, NULL );

    // Keep checking for data until there is nothing left.
    if( bResults )
    {
        do 
        {
            // Check for available data.
            dwSize = 0;
            if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
                printf( "Error %u in WinHttpQueryDataAvailable.\n",
                GetLastError( ) );

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
            if( !pszOutBuffer )
            {
                printf( "Out of memory\n" );
                dwSize=0;
            }
            else
            {
                // Read the data.
                ZeroMemory( pszOutBuffer, dwSize+1 );

                if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, 
                    dwSize, &dwDownloaded ) )
                    printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
                else
                    printf( "%s", pszOutBuffer );

                // Free the memory allocated to the buffer.
                delete [] pszOutBuffer;
            }
        } while( dwSize > 0 );
    }


    // Report any errors.
    if( !bResults )
        printf( "Error %d has occurred.\n", GetLastError( ) );

    // Close any open handles.
    if( hRequest ) WinHttpCloseHandle( hRequest );
    if( hConnect ) WinHttpCloseHandle( hConnect );
    if( hSession ) WinHttpCloseHandle( hSession );

    return "";
}
//END OF utils.cpp

1>------ Build started: Project: winagent, Configuration: Debug Win32 ------
1>Compiling...
1>utils.cpp
1>Linking...
1>   Creating library C:\winagent\Debug\winagent.lib and object C:\winagent\Debug\winagent.exp
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpCloseHandle@4 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReadData@16 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpQueryDataAvailable@8 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpReceiveResponse@8 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpSendRequest@28 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpenRequest@28 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpConnect@16 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>utils.obj : error LNK2019: unresolved external symbol __imp__WinHttpOpen@20 referenced in function "public: class std::basic_string,class std::allocator > __thiscall http::getText(void)" (?getText@http@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>C:\winagent\Debug\winagent.exe : fatal error LNK1120: 8 unresolved externals
1>Build log was saved at "file://c:\winagent\Debug\BuildLog.htm"
1>winagent - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

jmatthias.. 14

如果检查WinHttp*函数的MSDN参考,您将看到需要链接库Winhttp.lib.

打开项目设置,选择链接器选项,然后选择'input',并将WinHttp.lib添加到'Additional Dependencies'列表中.

或者你可以把

#pragma comment(lib, "winhttp.lib")

(如前所述)源代码中.



1> jmatthias..:

如果检查WinHttp*函数的MSDN参考,您将看到需要链接库Winhttp.lib.

打开项目设置,选择链接器选项,然后选择'input',并将WinHttp.lib添加到'Additional Dependencies'列表中.

或者你可以把

#pragma comment(lib, "winhttp.lib")

(如前所述)源代码中.

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有