简体   繁体   English

dll在调试模式下,在发布模式下调用程序(反之亦然)

[英]dll in debug mode, calling program in release mode (and vice versa)

I'm writing a small C++ program that tests C dlls, containing some functions. 我正在编写一个小型C ++程序来测试包含一些函数的C dll。 Those dlls exist in a debug version and a release version and I would like to load them both with the same program and compare them with a previous version. 这些dll存在于调试版本和发行版本中,我想用相同的程序加载它们并将它们与之前的版本进行比较。

The problem is, when I compile my program with release config, it can only use the dlls that are also release, and when I compile the program using debug config it can only use dlls that are also debug. 问题是,当我使用release config编译我的程序时,它只能使用同时发布的dll,当我使用debug config编译程序时,它只能使用也是调试的dll。

I load the dlls/functions using LoadLibrary and GetProcAddress functions. 我使用LoadLibrary和GetProcAddress函数加载dll /函数。

There are two types of functions: void type1(int&) and void type2(vector<string>*) . 有两种类型的函数: void type1(int&)void type2(vector<string>*) Type 1 works fine no matter the configuration. 无论配置如何,类型1都能正常工作。 But type 2 only works when the configuration matches. 但是类型2仅在配置匹配时才有效。

Code: 码:


typedef void(*GetNames)(vector<string>*);
GetNames get_var_names = (*GetNames)GetProcAddress(dll,"get_var_names");
vector<string> var_names;
get_var_names(&var_names);

The last line is where the program fails with an error like "0xC0000005: Access violation reading location 0xbaadf008." 最后一行是程序失败,出现“0xC0000005:访问冲突读取位置0xbaadf008”之类的错误。 if the configuration of calling program and dll don't match. 如果调用程序和dll的配置不匹配。 The error is a reading violation when program is release and dll is debug, but a writing violation when program is debug and dll is release. 当程序发布并且dll是调试时,错误是读取违规,但是当程序调试并且dll被释放时,写入违规。

What the function is supposed to do is just call push_back("x") a few times with different strings. 该函数应该做的只是用不同的字符串调用push_back(“x”)几次。

It doesn't seem to be totally impossible to use a debug dll in a release config program or all the functions of type 1 also wouldn't work, so it seems to have something to do with vector or string class. 在发布配置程序中使用调试dll似乎并非完全不可能,或者类型1的所有函数也不起作用,因此它似乎与向量或字符串类有关。

Anyone have any idea how to solve this or is using two executables with different configurations my only choice? 任何人都知道如何解决这个问题或使用两个不同配置的可执行文件我唯一的选择?

Many (if not all) STL classes use different layout for DEBUG-builds. 许多(如果不是全部)STL类为DEBUG构建使用不同的布局。 Therefor you cannot use such a DEBUG-compiled class from a dll (like std::string and std::vector) in a RELEASE-build of a program and vice versa. 因此,您不能在程序的RELEASE-build中使用dll(如std :: string和std :: vector)中的这样一个DEBUG编译类,反之亦然。

Should avoid using STL-types/class in your interface of your dll's and use build-in types instead. 应该避免在你的dll接口中使用STL类型/类,而是使用内置类型。 In that case you'll never have those issues. 在那种情况下,你永远不会有这些问题。

BTW: I am talking about MSVC++ of course (other compilers I don't know of). 顺便说一句:我当然在谈论MSVC ++(我不知道的其他编译器)。

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

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