Search code examples
c++visual-studiodllstdstring

Can a std::string be passed by value across DLL boundries?


Can a std::string be passed by value across DLL boundries between DLLs built with different version of Visual Studio?


Solution

  • No, because templated code is generated separately per module.

    So, when your EXE instantiates a std::string and passes it to the DLL, the DLL will begin using a completely different implementation on it. The result is a total mess, but it often sorta almost works because implementations are very similar, or the mess is hard to detect because it's some kind of subtle heap corruption.

    Even if they're both built with the same version of VS, it's very precarious / fragile, and I would not recommend it. Either use a C-style interface between modules (for example, COM), or just don't use a DLL.

    More detailed explanation here: Creating c++ DLL without static methods

    and here: How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?