Using Lambdas in MFC Applications – Dealing with C++ REST SDK

C++ REST SDK (also known as Casablanca) offers support for HTTP client/server, JSON, URI, asynchronous streams, WebSockets client, oAuth and PPL Tasks.
We can get C++ REST SDK from CodePlex  or use the one shipped with Visual Studio 2013.
This article gives suggestions of how to set up a Visual C++ project (particularly one that uses MFC) in order to be able to deal with Casablanca library. It also shows some simple sample code and a demo MFC project.

Set up the project for using C++ REST SDK

First, must find the location where C++ REST SDK is installed. With Visual Studio 2013 installed, I found it in the following path:
C:\Program Files (x86)\Microsoft SDKs\Cpp REST SDK for Visual Studio 2013\SDK\ which is similar to $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK.
Next, create a new MFC project or open an existing one, right click on it’s name, choose “Properties” from the context menu, then do the following:

  1. Choose All configurations in the Configuration combo box.
  2. Go to Configuration Properties / C/C++ / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\include to Additional Include Directories list.
  3. Go to Configuration Properties / C/C++ / Command Line and add -Zm200 in Additional Options field; this is necessary to avoid C3859 compiler error when include Casablanca headers in stdafx.h.
  4. Go to Configuration Properties / Linker / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\lib\$(PlatformTarget)\$(Configuration) to Additional Library Directories list.
  5. Go to Configuration Properties / Linker / Input and add casablanca120.lib to Additional Dependencies.
  6. Also in Configuration Properties / Linker / Input add casablanca120.dll to Delay Loaded DLLs list; this is necessary to avoid false memory leaks reported by the MFC framework;
  7. Go to Configuration Properties / Build Events / Post-Build Event and add the following command to Command Line field
     copy /Y “$(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\bin\$(PlatformTarget)\$(Configuration)\casablanca120.dll” “$(OutDir)”; this copies the necessary Casablanca DLL from its install folder in the output directory of our project.
  8. Close the project property pages dialog.
  9. In stdafx.h include all necesary Casablanca headers, e.g. http_client.h, json.h, pplxtasks.h and so on.

Now, we are ready to go on and use C++ REST SDK stuff in our own MFC application.


  • Remember that other versions of Visual Studio may install C++ REST SDK in other location!
  • May be other methods to set up the project paths, as for example by using Property Manager and/or edit .props files; I just presented one presumed simpler.
  • Ones would prefer to get C++ REST SDK from CodePlex; probably, I’ll describe this alternative in a future article.

Next is some code sample. You can notice it uses lambda expressions and for that reason I included this article in Using Lambdas in MFC Applications series.

Using lambdas and C++ REST SDK with MFC – sample code

This sample sends a JSON structure in URI query part to a site which gives in response also a JSON which says if the sent one is valid or not and gives an error description if necessary.

void CJsonVerifierDlg::OnClickButtonVerifyJson()

        auto task = _VerifyJsonTask();
        auto spJsonValue = task.get();
        if (spJsonValue)
    catch (std::exception& e)
        CString strMessage;
        strMessage.Format(L"Exception: %S", e.what());
        AfxMessageBox(strMessage, MB_ICONERROR);
pplx::task<std::shared_ptr<web::json::value>> CJsonVerifierDlg::_VerifyJsonTask()
    return pplx::create_task([this]()
            // base URI
            auto sBaseURI = utility::string_t(m_strBaseURI.GetString());
            // build request URI
            auto sRequestURI = web::http::uri_builder()
                .append_query(L"json", m_strInputJson.GetString()).to_string();
            // create http client
            web::http::client::http_client httpClient(sBaseURI);
            // create GET http request
            web::http::http_request httpRequest(U("GET")); //web::http::methods::GET);
            // set request URI
            // send request
            return httpClient.request(httpRequest);
            // then receive the response
        }).then([](web::http::http_response httpResponse)
                std::shared_ptr<web::json::value> spJsonValue;
                if (web::http::status_codes::OK == httpResponse.status_code())
                    spJsonValue = 
                return spJsonValue;
void CJsonVerifierDlg::_DisplayResponse(const std::shared_ptr<web::json::value>& spJsonValue)
    if ((*spJsonValue)[U("validate")].as_bool())
        m_strIsValid = _T("YES");
        m_nParseTime = (*spJsonValue)[U("parse_time_nanoseconds")].as_integer();
        m_strIsValid = _T("NO");
        m_strError = (*spJsonValue)[U("error")].as_string().c_str();

More details about project settings and source code can be found in the attached demo project.

Using lambdas and C++ REST SDK with MFC – demo project

Download: JSON Verifier - Demo Application using (1923 downloads)

JSON Verifier - Demo Application using Casablanca
JSON Verifier – Demo Application using Casablanca

References and related articles

1 thought on “Using Lambdas in MFC Applications – Dealing with C++ REST SDK”

  1. Hi, I was studying this example and found it very interesting.
    I was working in some modifications where I use STL and I get this linker error:
    Error 1 error LNK1194: cannot delay-load ‘casablanca120.dll’ due to import of data symbol ‘”__declspec(dllimport) public: static class std::basic_string<wchar_t,struct std::char_traits,class std::allocator > const web::http::methods::GET” (__imp_?GET@methods@http@web@@2V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@B)’; link without /DELAYLOAD:casablanca120.dll D:\dev\src\C++\Demo Casablanca\JsonVerifier\LINK JsonVerifier

    I just deleted the delayload, but I’m courious about this. Thanks for the article.



Leave a Comment