1

I have a VS 2010 C++ solution with two projects, one generates a DLL and the other is the driver to the DLL functions. All the following code

char cstring [256];
strcpy (cstring, "C String");
std::string string1 = "Test String";
std::string string2 (string1);
std::string string3;
string3.assign (cstring);

work well in the driver. But if the same code are placed anywhere in the DLL project, none of string1, string2 and string3 can be assigned with any values successfully. Mostly, the debugger shows them as Bad Ptr. It looks that they are not well allocated in the memory.

I have tried to place the std::string as class member fields, auto variables, and static variables. But none of the methods work as expected. Can anybody help me to find out the cause?

Lucius
  • 11
  • 1
  • Standard DLL problem, happens when your DLL and the EXE do not use the exact same standard C++ library. Could be as simple as the EXE built in Debug, thus using msvcp100d.dll and the DLL built in Release, thus using msvcp100.dll. Building with /MT instead of /MD is another traditional mistake. Both projects must use the *exact* same compiler version and build settings so only one copy of the library is ever in use. Diagnose with Debug > Windows > Modules. – Hans Passant Jul 30 '17 at 15:59
  • Can be true. But it is hard to find out what the options were used to build the other libraries. Today I just found out that the driver and DLL can run to success without any problem in installations outside my VS 2010 environment. So, it sounds a run time problem. – Lucius Jul 31 '17 at 10:49
  • Well, if you don't know then the odds for a mismatch rise to 99.9% Programmers that expose std types from their DLL interface need to have a telephone, be sure to use one. – Hans Passant Jul 31 '17 at 11:00

1 Answers1

0

I think the reason for this is the lack of application binary interface standard (ABI). This problem is perfectly described in this post: How do I pass objects especially stl objects to and from a dll.

It might also be the 'properties >> c/c++ >> code generation >> runtime library settings' (/MT and /MD settings) you need to make sure library project and using project are the same.

Treebeard
  • 322
  • 1
  • 9
  • Yes, it is true that a string passed in from outside the DLL is corrupted. But the bigger question is, the locally defined strings cannot be even initialized! They are almost all Bad Ptr. – Lucius Jul 31 '17 at 07:37
  • Did you check the runtime library settings are the same in both projects (dll and implementor)? – Treebeard Jul 31 '17 at 12:15