Share via


Can std::string be passed across dll boundaries. (i.e) can I export a class with public functions that has std::string params?

Question

Friday, March 25, 2011 12:53 PM

 Actually I did but it crashed somewhere in dbg heap.c- Rajesh K http://thoughtsontechies.blogspot.com/

All replies (9)

Friday, March 25, 2011 12:59 PM

It's not safe in the general case, as you have discovered.  The problem is that any memory re-allocation (such as resizing) has to take place using the same memory allocator, which is not guaranteed across DLL boundaries. Answering policy: see profile.


Friday, March 25, 2011 1:31 PM | 1 vote

It will work if, and only if, both the exe and the DLL use the DLL version of the runtime library, and if both are compiled with exactly the same compiler and compiler settings (like debug vs. release).  These restrictions make it fragile and poor practice.

 

 


Friday, March 25, 2011 4:31 PM | 1 vote

An alternative that will probably make it work is to create a string class that uses its own STL memory allocator using global memory.

Be prepared though to maintain that allocator for practically every release of Visual Studio (that's been my experience). And, as Scott mentions, you'll still have to make sure that the DLL and its users are compiled with the same VS version.

 


Saturday, March 26, 2011 10:42 AM

i modified the interface class to accept char* and no crash. still i have doubt my client is compiled using vs6.0 an win32 console app and my dll is regular win32 simple dll... why did the program crash then? compiler and environment are same....- Rajesh K http://thoughtsontechies.blogspot.com/


Saturday, March 26, 2011 11:02 AM | 1 vote

On 26/03/2011 11:42, Rajesh Kanakarajan wrote:

my client is compiled using vs6.0 an win32 console app and my dll is regular win32 simple dll... why did the program crash then? compiler and environment are same....

If you use VC6 (the same compiler) for both the client and the DLL, and you build the client and the DLL with dynamic linking to CRT, and both client and DLL use the same CRT (e.g. release mode .exe with release mode .dll, debug mode .exe with debug mode .dll), then it should work even if you have std::string (or other STL classes and C++ classes in general) at the DLL interface.

But it is a very particular case, it's strongly constraining.

Giovanni


Saturday, March 26, 2011 11:04 AM | 1 vote

On 25/03/2011 17:31, cipactli wrote:

An alternative that will probably make it work is to create a string class that uses its own STL memory allocator using global memory.

It's not only a problem of allocator, it is also a problem of the actual implementation and internal structure of std::string class (and other STL classes in general). The implementation of STL classes can change from compiler version to compiler version.

Giovanni


Wednesday, December 13, 2017 10:02 AM

Yes a string can be passed across DLL boundaries, providing that server DLL and client use the same template definition and memory allocator. In practice use /MD(d) in vcxproj.


Monday, December 18, 2017 2:08 PM

 Actually I did but it crashed somewhere in dbg heap.c - Rajesh K http://thoughtsontechies.blogspot.com/

There were a lots of missed as in VS6.0 , So now forget about it as it is no use to finding out why it was used to working. instead of that can you show your code where it is crashing . Did you tried to debug your code.

Thanks

Rupesh Shukla


Monday, December 18, 2017 7:05 PM

> Actually I did but it crashed somewhere in dbg heap.c

can you show your code where it is crashing . Did you tried to debug your code.

Rupesh Shukla

Rupesh -

It looks like you didn't notice that you have replied to a post that is
more than six and a half years old. I sure hope that the OP has moved
on by now. :-)

  • Wayne