Trevin Corkery
Trevin Corkery

Reputation: 691

C++ | Windows - Passing STL objects to DLL

I have seen some very well questions but I decided I would ask my own because they weren't quite what I wanted to know.

There was a lot of talk about how you shouldn't pass std::string into a function (from a DLL) because everything has to match CRT, Platform version, etc but can you safely pass a const *char to a function and then inside of the .dll use std::string's?

The reason I ask is because I recently discovered how amazingly useful C++ strings are and I want to use them with my .DLL but I have discovered they are unsafe to use with .dll's because of runtime templates and such.

So my question is mainly, is there a "safe" way to use std::string in a .dll?

Upvotes: 0

Views: 274

Answers (3)

peterchen
peterchen

Reputation: 41106

There is no ABI documented, so technically, it can change with the compiler version, and can depend on compiler settings.

You should be on the safe side if caller and DLL are built by the same version of Visual C++.

However this means: you cannot use it in a public interface, and you are giving up compatibility between versions (say, running an oder EXE with a newer DLL - after upgrading your compiler).

Upvotes: 0

Hieu Phan
Hieu Phan

Reputation: 41

Feel free to use std::string in DLL interface. But you should make sure that DLL and Application must be built with the same std library to avoid std::string boundary problem.

Upvotes: 0

MSalters
MSalters

Reputation: 179981

It's entirely safe to use it internally.

And don't worry too much about using a std::string across DLL boundaries either. You can't use it in a public interface, but when you ship a DLL with your own program you control both sides. It's quite common to have both the DLL and the EXE in two Visual Studio projects within one VS solution. That ensures they're built with the same platform version etc.

Upvotes: 1

Related Questions