c++ - writing directly to std::string internal buffers -
I was looking for a way to stuff some data across a string across the DLL border, as we have different compilers All of our DLL interfaces are easy to use. *
Is there any correct way to pass a pointer in the dell function, as it is capable of filling the string buffer directly?
string stringoffin (100, '\ 0'); FunctionInDLL (stringToFillIn.c_str), stringToFillIn.size ()); // Definitely wrong! FunctionInDLL (CONST-START & lt; char * & gt; (stringToFillIn.data ()), StringToFill Insise ()); // wrong? FunctionInDLL (and stringfoline [0], stringToFillIn.size ()); // wrong? StringToFillIn.resize (strlen (stringToFillIn.c_str ()));
The most promising one is the one & string is the right way to do this on [0], given that you think string :: data () == & Amp; ; String [0]? It appears to be inconsistent.
Or it is better to swallow an additional allocation and avoid the question:
vector & lt; Char & gt; VectorToFillIn (100); FunctionInDLL (& vectorToFillIn [0], vectortoofilineski ()); String dllGaveUs (& vectorToFillIn [0]);
I'm not sure the standard guarantees that the data std :: The string is stored as
a char *
. In the most portable way I can imagine that using a std :: vector
is guaranteed to store its data in a continuous portion of memory:
std:: vector & lt; Four & gt; Buffer (100); Function indul (and buffer [0], buffer (.)); Std :: string stringToFillIn (& amp; buffer [0]);
Of course, the data will need to be copied twice, which is a bit incompetent.
Comments
Post a Comment