locked
Build error while using IXMLHTTPRequest2 api

    Question

  • I create a blank project in windows metro style with c++,

    and I want to use use the IXMLHTTPRequest2 interface to send HTTP GET and POST requests to a web service.

    I copy this code into my MainPage.xaml.cpp file:

    #include <SDKDDKVer.h>
    #include <tchar.h>
    #include <msxml6.h>
    #include <wrl.h>
    #include "XMLHttpRequest2Callback.h"
    using namespace Microsoft::WRL;
    VOID Get(_In_ PCWSTR pcwszUrl)
    {
        HRESULT hr = S_OK;
        DWORD dwStatus = 0;
        BOOL fAbort = TRUE;
        ComPtr<IXMLHTTPRequest2> spXHR;
        ComPtr<CXMLHttpRequest2Callback> spMyXhrCallback;
        ComPtr<IXMLHTTPRequest2Callback> spXhrCallback;
        //
        // Create an object of the IID_IXMLHTTPRequest2 class.
        //
        hr = CoCreateInstance(CLSID_FreeThreadedXMLHTTP60,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_PPV_ARGS(&spXHR));
        if (FAILED(hr))
        {
            goto Exit;
        }
        //
        // Create an object of the CXMLHttpRequest2Callback class and initialize it.
        //
        hr = MakeAndInitialize<CXMLHttpRequest2Callback>(&spMyXhrCallback);
        if (FAILED(hr))
        {
            goto Exit;
        }
        hr = spMyXhrCallback.As(&spXhrCallback);
        if (FAILED(hr))
        {
            goto Exit;
        }
        //
        // Send a HTTP GET request.
        //
        hr = spXHR->Open(L"GET",                // Method.
                         pcwszUrl,              // Url.
                         spXhrCallback.Get(),   // Callback.
                         NULL,                  // Username.
                         NULL,                  // Password.
                         NULL,                  // Proxy username.
                         NULL);                 // Proxy password.
        if (FAILED(hr))
        {
            goto Exit;
        }
        //
        // Send the request to the server.
        //
        hr = spXHR->Send(NULL, 0);
        if (FAILED(hr))
        {
            goto Exit;
        }
        //
        // Waiting for the completion of the request.
        // Callers needing to receive completion or status events on a STA or UI
        // thread must use a mechanism that will not block the threads window message
        // pump. One example is by posting a window message to the STA or UI thread
        // window handle.
        //
        hr = spMyXhrCallback->WaitForComplete(&dwStatus);
        if (FAILED(hr))
        {
            goto Exit;
        }
        fAbort = FALSE;
    Exit:
        if (FAILED(hr))
        {
            wprintf(L"Failed, Error code = 0x%x.\n", hr);
        }
        else
        {
            wprintf(L"Succeed, Status code = %d.\n", dwStatus);
        }
        if (fAbort)
        {
            spXHR->Abort();
        }
    }
    then build the project, I got this error:

    error LNK2001: unresolved external symbol  _CLSID_FreeThreadedXMLHTTP60

    how to fix this error link? Anyone have solution?

    thanks a lot

    Friday, July 06, 2012 3:14 AM

Answers

  • You need to link with "msxml6.lib".

    • Marked as answer by donarlee Wednesday, July 11, 2012 3:31 AM
    Tuesday, July 10, 2012 8:08 PM

All replies

  • Hello,

    You can try to include these head files and namespace

    #include <Windows.h>
    #include <wrl.h>
    #include <wrl\client.h>
    #include <wrl\implements.h>
    #include <MsXml6.h>

    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Details;

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 09, 2012 9:34 AM
  • Thanks Jesse,

    I have add the include head files and namespace as you said,

    but still got this error:

    error LNK2001: unresolved external symbol  _CLSID_FreeThreadedXMLHTTP60

    should I add some additional library?

    this is my  MainPage.xaml.cpp file

    //
    // MainPage.xaml.cpp
    // Implementation of the MainPage class.
    //
    #include "pch.h"
    #include "MainPage.xaml.h"
    #include <wrl.h>
    #include <Msxml6.h>
    #include "XMLHttpRequest2Callback.h"
    #include <Windows.h>
    #include <wrl.h> 
    #include <wrl\client.h> 
    #include <wrl\implements.h> 
    using namespace testApp;
    using namespace Platform;
    using namespace Windows::Foundation;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::UI::Xaml;
    using namespace Windows::UI::Xaml::Controls;
    using namespace Windows::UI::Xaml::Controls::Primitives;
    using namespace Windows::UI::Xaml::Data;
    using namespace Windows::UI::Xaml::Input;
    using namespace Windows::UI::Xaml::Media;
    using namespace Windows::UI::Xaml::Navigation;
    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Details; 
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    MainPage::MainPage()
    {
    	InitializeComponent();
    	
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
    {
    	(void) e;	// Unused parameter
    }
    int MainPage::Get(Platform::String^ uriString)
    {
    	HRESULT hr = S_OK;
    	DWORD dwStatus = 0;
    	BOOL fAbort = TRUE;
    	ComPtr<IXMLHTTPRequest2> spXHR;
    	ComPtr<CXMLHttpRequest2Callback> spMyXhrCallback;
    	ComPtr<IXMLHTTPRequest2Callback> spXhrCallback;
    	hr = CoCreateInstance(CLSID_FreeThreadedXMLHTTP60,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_PPV_ARGS(&spXHR));
        if (FAILED(hr))
        {
    		return 0;
        }
    	//
        // Create an object of the CXMLHttpRequest2Callback class and initialize it.
        //
        hr = MakeAndInitialize<CXMLHttpRequest2Callback>(&spMyXhrCallback);
        if (FAILED(hr))
        {
            goto Exit;
        }
        hr = spMyXhrCallback.As(&spXhrCallback);
        if (FAILED(hr))
        {
            goto Exit;
        }
    Exit:
        if (FAILED(hr))
        {
            wprintf(L"Failed, Error code = 0x%x.\n", hr);
        }
        else
        {
            wprintf(L"Succeed, Status code = %d.\n", dwStatus);
        }
        if (fAbort)
        {
            spXHR->Abort();
        }
    	return 1;
    }

    Tuesday, July 10, 2012 1:48 AM
  • You need to link with "msxml6.lib".

    • Marked as answer by donarlee Wednesday, July 11, 2012 3:31 AM
    Tuesday, July 10, 2012 8:08 PM
  • thanks,  henador

    add "msxml6.lib", then build OK

    Wednesday, July 11, 2012 3:31 AM