我用Visual c++创建了一个DLL项目,我想使用cpprestsdk/casablanca

然后,我创建了一个RestWrapper.h头文件:

#pragma once

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

RestWrapper.cpp源文件:
#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());
        }
    }
}

当我构建它时,它就成功构建了。

然后,我在可视c++中创建了Windows Console Application以测试我创建的DLL项目。

我将MyCpprestDll.dll, MyCpprestDll.lib and RestWrapper.hMycppestDll project复制到DllTest project中。

然后在DllTest项目properties中,在Linker->input->Additional Dependencies中:我添加了MyCpprestDll.lib
这是DllTest.cpp:的代码
#include "stdafx.h"
#include "RestWrapper.h"
#include <iostream>

using namespace mycpprest;

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

它没有编译错误,但是在运行错误时显示:



c&#43;&#43; - 入口点不能位于动态链接库中-C&#43;&#43;-LMLPHP

我试图搜索相关问题,但我不知道如何或在dll项目中设置我的入口点。

最佳答案

构建DLL时需要使用dllexport,但是将其包含到另一个项目中时需要使用dllimport

This answer显示您必须使用一些宏和预处理程序定义才能使其正常工作。

10-04 22:57