I was looking for a way to stuff some data into a string across a DLL boundary. Because we use different compilers, all our dll interfaces are simple char*.
Is ther
You can use char buffer allocated in unique_ptr instead vector:
// allocate buffer
auto buf = std::make_unique(len);
// read data
FunctionInDLL(buf.get(), len);
// initialize string
std::string res { buf.get() };
You cannot write directly into string buffer using mentioned ways such as &str[0] and str.data():
#include
#include
#include
int main()
{
std::string str;
std::stringstream ss;
ss << "test string";
ss.write(&str[0], 4); // doesn't working
ss.write(str.data(), 4); // doesn't working
std::cout << str << '\n';
}
Live example.