简体   繁体   English

将std :: string作为参数从一个DLL传递到另一个DLL会引发访问冲突错误

[英]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. 该应用程序具有许多C ++编译的DLL,每个DLL都公开许多C类型的接口。 The application has some std::string type configuration variables which needs to be used in the DLL interface logic. 该应用程序具有一些std :: string类型的配置变量,这些变量需要在DLL接口逻辑中使用。 While passing this std::string type parameters to these DLLs, "0xC0000005: Access violation executing location" has thrown. 在将此std :: string类型参数传递给这些DLL时,抛出了“ 0xC0000005:访问冲突执行位置”。 Is this something related to VS project settings for DLL projects? 这与DLL项目的VS项目设置有关吗? Kindly clarify. 请澄清。

You probably won't make them to work easily. 您可能不会使它们轻松工作。

std::string may not be compatible between different compilations from different libraries. std::string在不同库的不同编译之间可能不兼容。

When you say "The application has many C++ compiled DLLs", very likely you're in this scenario: 当您说“应用程序具有许多C ++编译的DLL”时,很可能您处于这种情况:

Library A: 库A:

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

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

Library B: 图书馆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. 您有2个版本的std::string并进行了程序编译,因为在程序编译期间使用的是相同的标头...但是在运行时,它们期望使用2种不同的类型。

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. 如果可以从源代码编译,请确保使用相同的STL版本。
  • if you can't compile from source, create C wrappers for them and use C-String as interface... they will always work. 如果您不能从源代码进行编译,请为它们创建C包装程序并使用C-String作为接口...它们将始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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