-1

I need to use an API named mailjet, but this one is only available for pretty much every language except C++ (php, python, ruby, java, node.js, ES2015, go and C#).
So I'd like to know, since the C# one is not a binary api but it's full C# code, if its possible to integrate C# code into a C++ one (or another of the listed languages).
Thanks !

Paradowski
  • 719
  • 1
  • 13
  • 17
Axel.A
  • 94
  • 1
  • 9
  • 1
    Why do you want to integrate something? You can just write your own c++ client for API. – Renatas M. Jul 30 '18 at 09:50
  • might check this one out [here]( https://stackoverflow.com/questions/13293888/how-to-call-a-c-sharp-library-from-native-c-using-c-cli-and-ijw) – edhu Jul 30 '18 at 09:52
  • Read about linking C# with Microsoft's C++. If you would like to import C# lib into "non Microsoft" C++, you can create a Visual C++ library and try to link it to your C++ code. – Paradowski Jul 30 '18 at 09:55
  • 1
    I agree with Reniuz: if you just want to do simple send messages then it'll be easier to start with a C++ REST library that does JSON. if you're using the full API then it might just about be worth integrating, but it's not going to be easy. – Rup Jul 30 '18 at 10:00
  • C++ is oblivious of other languages. There are libraries and interfaces that can be consumed. – Ron Jul 30 '18 at 10:10
  • Thanks everybody for your answers, im currently trying to use the cUrl version of the API with libcurl which seems to be easier, and then i'll try yours. – Axel.A Jul 30 '18 at 14:34

5 Answers5

2

Mailjet provides a standard JSON/HTTP(S) Web API, available to any language that provides HTTP and JSON support (either natively, by using libraries or by coding these protocol or formats yourself). That languages that you mentions are merely the languages that Mailjet provides a wrapper for. C++ will allow you to consume the API just fine, it would be very inefficient to use a C# wrapper and call that from C++. Just use a C++ library that supports HTTP and one that supports JSON and you will be fine. Use these to code the API call according to the Mailjet specifications, e.g. build a HTTP request with the proper HTTP headers and JSON body format and call the API. There are plenty of these C++ libraries available, any decent one will do.

Ton Plooij
  • 2,583
  • 12
  • 15
0

You can do it. Create a DLL file from your C# code and call the api from C++ code. There is option for that please see the below link.

https://support.microsoft.com/en-gb/help/828736/how-to-call-a-managed-dll-from-native-visual-c-code-in-visual-studio-n

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
0

Try write little python script and run it with Python interactive shell.

python -i -u

It's easy with Qt5. Just copy code from that answer

Petr Tripolsky
  • 1,419
  • 15
  • 23
0

In Visual studio you can use C# code from C++ project Using CLR (Common Language Runtime).

Open Project Properties.

  • Go in the left tree view under Configuration Manager->General.
  • In the Right grid under Project Default find the Common Language Runtime Support
  • If it is standard C++ project the default is: No Common Language Runtime Support you should change it to: Common Language Runtime Support (/clr)
  • in VS17 you also should change Configuration Proprties->C/C++->Language Conformance mode to No

The CLR is a layer which can be used to combine .Net and C++. The C++ syntax is the same, .Net syntax is a little different.

  • add the .Net project or DLL as reference to the CLR project.
  • .Net namespaces seperator - use :: instead of . as delimiter.
  • .Net classes must be declared with a ^ to indicate .Net reference. for example String^ is .Net String class.
  • use gcnew to create .Net instances instead of new. like String^ s = gcnew String();
  • String needed to be "translated" from c++ to .Net and back.

A short example for mixed code:

using namespace System;  
using namespace System::Runtime::InteropServices;  

#include <iostream>
using namespace std;
  
int main() {  
   String ^ str = gcnew String("Abcde");  

   Console::WriteLine(str);  //use .Net to print to screen.
   
   //extract c string from .Net String
   char *p = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();  

   cout<<p<<endl;  // use C++ to print to screen

   //free the extracted pointer.
   Marshal::FreeHGlobal(IntPtr(p));

}  
Community
  • 1
  • 1
SHR
  • 7,940
  • 9
  • 38
  • 57
0

thanks a lot for your answers. Here is the solution i got using libcurl in C++.
First, you'll have to install libcurl (great tutorial here : https://stackoverflow.com/a/30290407).
Then, after libcurl is installed, you'll have to use it this way (and customize it of course) :

#include <curl/curl.h>
#include <string>
int main(int argc, char **argv)
{
    CURL *curl = NULL;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    string message(R"({"Messages":[{"From":{"Email":"THE SENDER MAIL","Name":"Me"},"To":[{"Email":"THE RECEIVER EMAIL","Name":"TheName"}],"Subject":"TheSubject","TextPart":"TEST MAIL"}]})"); // "R" before the string is supposed to allow special characters like "\"

    curl_easy_setopt(curl, CURLOPT_URL, "https://api.mailjet.com/v3.1/send");
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
    curl_easy_setopt(curl, CURLOPT_USERNAME, "YOUR API USERNAME");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "YOUR API PASSWORD");
    curl_easy_setopt(curl, CURLOPT_HEADER, "Content-Type: application/json");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, my_strlen(message)); // my_strlen is a personnal strlen, does the same as message.length()
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message.c_str());
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
}
Axel.A
  • 94
  • 1
  • 9