简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM