Je crée un CLR project
dans visual c++
avec 64 bit
configuration 64 bit
et essaie d’utiliser cpprestsdk aka casablanca 64bit
.
Mais lorsque j’ai lancé le projet, une erreur s’est produite:
1>------ Build started: Project: Timestamp, Configuration: Debug x64 ------ 1>MyForm.cpp 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: is not supported when compiling with /clr or /clr:pure. 1>Testapi.cpp 1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.13.26128\include\mutex(8): fatal error C1189: #error: is not supported when compiling with /clr or /clr:pure. 1>Generating Code... 1>Done building project "Timestamp.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Autre erreur:
E2093 a local lambda is not allowed in a member function of a managed class Timestamp c:\Users\Laptop-attendance\source\repos\Timestamp\Timestamp\Testapi.h 97
L’IDE affiche une erreur concernant les caractères '['
dans la fonction .then
, telle que .then([=](http_response response)
, qui, si vous l’avez signalé, indique "a local lambda is not allowed in a member function of a managed class"
Si j’essaie le cpprestsdk
dans une Windows Console Application
de Visual c ++ avec une configuration 64 bits, cela fonctionne bien.
J’utilise Visual Studio 2017.
Pensez-vous que cpprestsdk ne peut pas être utilisé dans le projet CLR de vc ++?
Merci.
Voici mon code, et le code sur cpprestsdk Je viens de le récupérer grâce à son tutoriel:
#ifndef TESTAPI_H #define TESTAPI_H #pragma once #include #include #include namespace Timestamp { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace utility; // Common utilities like ssortingng conversions using namespace web; // Common features like URIs. using namespace web::http; // Common HTTP functionality using namespace web::http::client; // HTTP client features using namespace concurrency::streams; // Asynchronous streams /// /// Summary for Testapi /// public ref class Testapi : public System::Windows::Forms::Form { public: Testapi(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// /// Clean up any resources being used. /// ~Testapi() { if (components) { delete components; } } private: /// /// Required designer variable. /// System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->SuspendLayout(); // // Testapi // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 261); this->Name = L"Testapi"; this->Text = L"Testapi"; this->Load += gcnew System::EventHandler(this, &Testapi::Testapi_Load); this->ResumeLayout(false); } #pragma endregion private: System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e) { auto fileStream = std::make_shared(); // Open stream to output file. pplx::task 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()); ssortingngstreambuf 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()); } } }; } #endif /*TESTAPI_H*/
À la toute fin de cette réponse , Hans Passant donne un indice utile dans votre cas. En gros, vous avez besoin d’une bibliothèque séparée c ++ (prise en charge de clr désactivée) pour cpprest
code cpprest
, lier cette bibliothèque à partir de votre projet CLR et vous assurer qu’aucun en-tête inclus n’apportera
.
Juste un exemple, une classe comme celle-ci, dans un fichier d’en-tête restwrapper.h :
class RestWrapper { public: void test(); };
Dans son fichier d’implémentation, restwrapper.cpp :
#include "restwrapper.h" #include #include #include using namespace utility; // Common utilities like ssortingng conversions using namespace web; // Common features like URIs. using namespace web::http; // Common HTTP functionality using namespace web::http::client; // HTTP client features using namespace concurrency::streams; // Asynchronous streams void RestWrapper::test() { auto fileStream = std::make_shared(); // Open stream to output file. pplx::task 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")); //etc ... }
Vous pouvez comstackr cette classe dans une DLL (lier cpprest
et toutes ses bibliothèques associées), puis associer votre projet CRL à cette bibliothèque. Dans le projet CLR, vous devez inclure uniquement restwrapper.h , ce qui n’inclut rien:
#include System::Void Testapi_Load(System::Object^ sender, System::EventArgs^ e) { RestWrapper wrapper; wrapper.test(); }
Vous pouvez appeler une classe qui utilise des mutex d’un projet CLR. Pour ce faire, vous devez créer un projet c ++ normal et créer des classes qui possèdent les fonctionnalités requirejses mais n’exposent pas
dans leurs en-têtes. Vous êtes ensuite libre d’appeler. ces classes de votre projet CLR.