我在 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());
}
}
}
当我建造它时,它就成功建造了。
然后我Windows Console Application
用 Visual C++ 创建来测试我创建的 DLL 项目。
MyCpprestDll.dll, MyCpprestDll.lib and RestWrapper.h
我将from复制MycppestDll 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;
}
它没有编译错误,但运行时错误提示:
The procedure entry point ?TestApi@RestWrapper@mycpprest@@SAXXZ could not be located in the dynamic link library
我试图搜索相关问题,但我不知道如何或在我的 dll 项目中设置我的入口点。