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
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.