Use LinkObject-based passing, i.e. transfer the strings through a MathLink connection.
You would need to use the following functions:
MLTestHead
to check that you are receiving a List
, and to retrieve the number of elements
MLGetUTF8String
or one of the analogous functions to get the string with te desired encoding
- Once you're done using it, free it with the corresponding function, e.g.
MLReleaseUTF8String
The mlstream.h
header that comes with my LTemplate package makes this task considerably easier. There is an example included that shows how to receive an arbitrary number of string arguments, concatenate them, and return the result. I'll copy it here:
The StrCat.h
C++ header file (must be in the current working directory):
#include <mlstream.h>
#include <string>
#include <vector>
struct StrCat {
// Concatenate an arbitrary number of strings
void strcat(MLINK link) {
mlStream ml(link, "strcat");
// We do not check for the number of arguments;
// instead, we read all arguments into a string vector
std::vector<std::string> vec;
ml >> vec;
// Concatenate the strings
std::string result;
for (const auto &el : vec)
result += el;
ml.newPacket();
ml << result;
}
};
Mathematica code:
Needs["LTemplate`"]
template = LClass["StrCat", {LFun["strcat", LinkObject]}];
CompileTemplate[template]
LoadTemplate[template]
obj = Make[StrCat]
obj@"strcat"["foo", "bar", "baz"]
(* "foobarbaz" *)