3

I created a DLL project in visual c++, and I wanted to use the cpprestsdk/casablanca.

Then I created a RestWrapper.h header file:

#pragma once

namespace mycpprest
{
    class RestWrapper
    {
    public:
        static __declspec(dllexport) void TestApi();
    };
}

And RestWrapper.cpp source file:

#include "stdafx.h"
#include "RestWrapper.h"

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>

using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;

namespace mycpprest
{
    void RestWrapper::TestApi()
    {
        auto fileStream = std::make_shared<ostream>();

        // Open stream to output file.
        pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
        {
            *fileStream = outFile;

            // Create http_client to send the request.
            http_client client(U("http://13.231.231.252:3000/api/individual_employment_setting/detail/172"));

            // Build request URI and start the request.
            //uri_builder builder(U("/search"));
            //builder.append_query(U("q"), U("cpprestsdk github"));
            return client.request(methods::GET);
        })

        // Handle response headers arriving.
        .then([=](http_response response)
        {
            printf("Received response status code:%u\n", response.status_code());

            // Write response body into the file.
            // return response.body().read_to_end(fileStream->streambuf());
            stringstreambuf buffer;
            response.body().read_to_end(buffer).get();

            //show content in console
            printf("Response body: \n %s", buffer.collection().c_str());

            //parse content into a JSON object:
            //json::value jsonvalue = json::value::parse(buffer.collection());

            return  fileStream->print(buffer.collection()); //write to file anyway
        })

        // Close the file stream.
        .then([=](size_t)
        {
            return fileStream->close();
        });

        // Wait for all the outstanding I/O to complete and handle any exceptions
        try
        {
            requestTask.wait();
        }
        catch (const std::exception &e)
        {
            printf("Error exception:%s\n", e.what());
        }
    }
}

When I build it, It success built.

Then I created Windows Console Application in visual c++ to test the DLL project that I created.

I copy the MyCpprestDll.dll, MyCpprestDll.lib and RestWrapper.h from MycppestDll project into DllTest project.

Then in DllTest project properties, in the Linker->input->Additional Dependencies: I added MyCpprestDll.lib

And here the code of DllTest.cpp:

#include "stdafx.h"
#include "RestWrapper.h"
#include <iostream>

using namespace mycpprest;

int main()
{
    RestWrapper::TestApi();
    system("PAUSE");
    return 0;
}

It has no compile error, but when running the error says:

The procedure entry point ?TestApi@RestWrapper@mycpprest@@SAXXZ could not be located in the dynamic link library

enter image description here

I tried to search about the related issues but I don't know how to or what to set to my entry point in my dll project.

noyruto88
  • 767
  • 2
  • 18
  • 40

2 Answers2

0

You need to use dllexport when building your DLL, but dllimport when you are including it into another project

This answer shows you have to use some macros and pre-processor definitions to make it work.

Sean
  • 60,939
  • 11
  • 97
  • 136
0

In your RestWrapper.h header file do something as shown below. Note that you must use __declspec(dllimport) for the importing executable to access the DLL's public data symbols and objects. Also, make sure you define the macro RestWrapper_EXPORTS in C/C++->Preprocessor->Preprocessor Definitions in your DLL project properties.

#ifdef RestWrapper_EXPORTS
#define RestWrapper_APIS __declspec(dllexport)
#else
#define RestWrapper_APIS __declspec(dllimport)
#endif

namespace mycpprest
{
 // This class is exported from the RestWrapper.dll
 class RestWrapper 
 {
   public:
    static RestWrapper_APIS void TestApi();     
 };
}

Rebuild your DLL project. No changes needed in your DllTest project, just compile your DllTest project using the updated RestWrapper.h and RestWrapper.lib file.

Amit Rastogi
  • 926
  • 2
  • 12
  • 22