Passing std::string as parameter from one DLL to another DLL throwing access violation error

0

The application has many C++ compiled DLLs, each exposing many C type interfaces. The application has some std::string type configuration variables which needs to be used in the DLL interface logic. While passing this std::string type parameters to these DLLs, "0xC0000005: Access violation executing location" has thrown. Is this something related to VS project settings for DLL projects? Kindly clarify.

c++
dll
asked on Stack Overflow Sep 4, 2019 by Balamurugan T • edited Sep 6, 2019 by IIRistoII

1 Answer

1

You probably won't make them to work easily.

std::string may not be compatible between different compilations from different libraries.

When you say "The application has many C++ compiled DLLs", very likely you're in this scenario:

Library A:

// STL
class std::string
{
    ... under the hood implementation of std::string (version A)
};

// Library code
std::string someFunctionInA();

Library B:

// STL
class std::string
{
    ... under the hood implementation of std::string (version B)
};

// Library code
void someFunctionInB(const std::string& myString);

The crash program:

std::string stringFromA = someFunctionInA();
someFunctionInB(stringFromA);

Got it? You have 2 versions of std::string and you program compiles because you're using the same headers during the compilations of your program... but in runtime, they are expecting 2 different types.

Size of the objects, order of data and simply the allocators may not match... and it will crash!

How to resolve:

  • if you can compile from source, make sure you use the same STL version.
  • if you can't compile from source, create C wrappers for them and use C-String as interface... they will always work.
answered on Stack Overflow Sep 4, 2019 by Wagner Patriota

User contributions licensed under CC BY-SA 3.0