Visual Studio C ++ dll库崩溃在Qt应用程序 [英] Visual Studio C++ dll library crashes in Qt application

查看:1056
本文介绍了Visual Studio C ++ dll库崩溃在Qt应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在MS Visual C ++ DLL库和Qt程序之间共享std :: string数据。



我有:




    在Visual C ++ 2010 Express中编写的DLL库,它导出一个方法:

      externC__declspec(dllexport)int Init(ITest * commandTest); 


  • 抽象接口ITest和实现它的类:

      class CTest:public ITest 
    {
    public:
    CTest
    virtual〜CTest();
    virtual void getVersion(std :: string& version)const;
    };


  • Qt GUI应用程序需要:

      *动态加载DLL 
    *实例化CTest并将其传递给导出的Init方法。




在DLL的Init :getVersion()方法被调用。我希望它会得到&版本字符串填充。我得到的是崩溃在行上,当我填充&版本与新的字符串。



我已经做了:




  • 下载了Qt libraries 4.8.3 for Windows(VS 2010,235 MB)从 http://qt-project.org/downloads ,安装它,并在QtCreator的项目设置中选择它。


    我认为它会克服这个问题,因为我使用Qt库编译与VS 2010和Qt GUI也编译与VS C ++工具链然后。不幸的是,问题没有消失,所以我试着最后一步:


  • 在Visual Studio中创建Win32控制台应用程序,通过LoadLibrary加载我的DLL,




小观察



在CTest :: getVersion()我打印这个通过引用控制台传递的版本字符串。当使用VS C ++控制台应用程序作为主机时,它是正确打印输出。当使用Qt应用程序 - 版本字符串打印一些垃圾(例如┌►☻qwerty27)



这让我认为ABI的Qt应用程序和我的DLL仍然不兼容,即使使用上述Qt VS 2010库。



问题:




  • 是否使用Windows(VS 2010)和Visual Studio工具链的Qt库不足以克服ABI兼容性问题?

  • 这是否意味着我应该自己编译Qt框架? / li>
  • 请帮助任何想法欢迎...


解决方案

在一个项目中,我有一个很像你的情况,两个DLL,相同的编译器(VC ++ 2010)。我正在将std :: string从一个传递给另一个,并得到很多崩溃。



问题是一个DLL是用多线程调试DLL (/ MDd),另一个使用多线程调试(/ MTd)会导致两个DLL之间的二进制不兼容(崩溃)。
同样的版本必须匹配,使用DEBUG或RELEASE两个DLL。



看看你的项目,这两个DLL似乎都使用 Multi -threaded Debug DLL(/ MDd)。这意味着两个都使用 MSVCP100D.dll 。这是确定,唯一的问题是来自QT网站的qt的VC ++版本编译为RELEASE模式,并使用 MSVCP100.DLL



我的建议是将您的运行时库更改为多线程DLL(/ MD),用于您的DEBUG配置。



我的第二个建议是按照Rebert的建议,使用 char * 而不是std :: string。



您也可以使用多线程调试DLL(/ MDd)重新编译QT / strong>,并使用该版本的QT为您的DEBUG配置(但这似乎很多工作)。


I have a problem sharing "std::string" data between MS Visual C++ DLL library and Qt program.

What I have are:

  • DLL library written in Visual C++ 2010 Express, which exports one method:

    extern "C" __declspec(dllexport) int Init(ITest* commandTest);
    

  • Abstract interface "ITest" and a class implementing it:

    class CTest: public ITest
    {
    public:
        CTest();
        virtual ~CTest();
        virtual void getVersion(std::string & version) const;
    };
    

  • Qt GUI application that needs to:

    * load the DLL dynamically
    * instantiate CTest and pass it to exported Init method.
    

In DLL's "Init" a "CTest::getVersion()" method is called. I'd expect it would get the "&version" string filled in. What I get instead are crashes on the line when I fill "&version" with new string.

What I already did:

  • downloaded "Qt libraries 4.8.3 for Windows (VS 2010, 235 MB)" from http://qt-project.org/downloads, installed it and selected it in QtCreator's project settings.

  • in QtCreator switched from MinGW toolchain to the one installed with MS Visual Studio 2010 Express.

    I thought that it will overcome the problem , because I used Qt libraries compiled with VS 2010 and the Qt GUI was also compiled with VS C++ toolchain then. Unfortunately the problem was not gone, so I tried the last step:

  • created Win32 Console application in Visual Studio, loaded my DLL via LoadLibrary, used "Init" method the same way as I did in Qt GUI... and it worked!!

Small observation

In "CTest::getVersion()" I am printing this "version" string passed by reference to the console. When using VS C++ console app as a host it is printed out correctly. When using Qt app - the "version" string is printed with some garbage around (e.g. ┌►☻qwerty27)

This makes me think that ABI of Qt app and my DLL is still incompatible, even when using Qt VS 2010 libraries mentioned above.

Questions:

  • Is using Qt libraries for Windows (VS 2010) and Visual Studio toolchain not enough to overcome ABI compatibility issues?
  • Does it mean I should compile the Qt framework on my own?
  • Please help - any ideas are welcome...

解决方案

In a project I had a situation much like yours, two DLLs, same compiler (VC++ 2010). I was passing std::string from one to the other and getting a lot of crashes.

The problem was one DLL was compiled with Multi-threaded Debug DLL (/MDd) and the other with Multi-threaded Debug (/MTd) this caused binary incompatibility between the two DLLs (crashes). Also the versions have to match, either use DEBUG or RELEASE for both DLLs.

Looking at your project, both DLLs seem to be using Multi-threaded Debug DLL (/MDd). this means both are using MSVCP100D.dll. This is ok, the only problem is that the VC++ version of qt from the QT website is compiled in RELEASE mode and is using MSVCP100.DLL

My recommendation is to change your runtime library to Multi-threaded DLL (/MD) for your DEBUG configuration.

My second recommendation is to follow Rebert's advice and use char* instead of std::string. char* will be compatible no matter what.

You can also recompile QT with Multi-threaded Debug DLL (/MDd) and use that version of QT for your DEBUG configuration (but this seems like to much work).

这篇关于Visual Studio C ++ dll库崩溃在Qt应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆