gpt4 book ai didi

c++ - Qt/MFC 迁移框架工具 : properly exiting DLL?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:35:31 26 4
gpt4 key购买 nike

我正在按照这个示例使用 Qt/MFC 迁移框架工具: http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-qt-dll-example.html

我构建的 dll 由基于 MFC 的第 3 方应用程序加载。第 3 方应用程序基本上调用我导出的 DLL 函数之一来启动我的插件和另一个函数来关闭我的应用程序。目前我的关机功能什么也没做。

当我在第 3 方应用程序中加载我的 DLL 时,启动函数被调用并且我的 DLL 成功启动并且我可以看到我的消息框。但是,如果我关闭我的插件然后尝试再次启动它,我会收到以下错误:

Debug Error!

Program: <my 3rd party app>
Module: 4.7.1
File: global\qglobal.cpp
Line: 2262
ASSERT failure in QWidget: "Widgets must be created in the GUI
thread.", file kernel\qwidget.cpp line 1233

(Press Retry to debug the application)

Abort Retry Ignore

这让我觉得我没有做任何事情来正确关闭我的插件。我需要做什么才能正确关闭它?

更新: http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html说:

The DLL also has to make sure that it can be loaded together with other Qt based DLLs in the same process (in which case a QApplication object will probably exist already), and that the DLL that creates the QApplication object remains loaded in memory to avoid other DLLs using memory that is no longer available to the process.

所以我想知道是否存在一些问题,无论如何我都需要以某种方式保持原始 DLL 加载?

最佳答案

我得到了这个工作!经过许多令人沮丧的工作时间后,我开始工作了。我使用的是 Qt/MFC 迁移文档中提供的代码,其中包含:

 BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason,
LPVOID /*lpvReserved*/ )
{
static bool ownApplication = FALSE;

if ( dwReason == DLL_PROCESS_ATTACH )
ownApplication = QMfcApp::pluginInstance( hInstance );
if ( dwReason == DLL_PROCESS_DETACH && ownApplication )
delete qApp;

return TRUE;
}

此结构适用于我的插件 DLL 的单次加载,但在后续加载时失败。我不认为 DllMain 是调用 pluginInstance 的正确位置(至少对于我的用例而言)。我认为问题是我的第 3 方应用程序只调用 DllMain 一次,并且在应用程序退出之前不会卸载 DLL。因此,当我启动我的插件然后关闭它然后再次启动它时,通过调用 pluginInstance 初始化的 QApplication 仍然存在。我认为我的第 3 方应用程序在我的插件每次启动时都会启动单独的线程,所以当我第二次启动我的插件时它是一个新线程但仍然尝试在 DllMain 中使用原始 QApplication 设置(DLL 仍在加载)。因此我的错误是因为它是试图写入 GUI 的新线程。

我正在构建的第 3 方 MFC 应用程序希望在我的 DLL 中有两个导出,Startup() 和 Shutdown(),它会在相关时间调用它们。

因此,我没有按照演练的建议进行操作,而是按如下方式进行(伪代码):

extern "C" __declspec(dllexport) void Startup()
{
QMfcApp::pluginInstance( 3rdPartyApp::GetPluginHandle() );
QWinWidget win( 3rdPartyApp::GetParentWindow() );
win.showCentered();
QMessageBox::about( &win, "About", "Hello World" );
}

extern "C" __declspec(dllexport) void Shutdown()
{
qApp->quit();
delete qApp;
}

这对于单个插件来说没问题,但是如果我创建多个插件,我不确定它的效果如何,因为事件循环集成我认为所有插件应该只有一个 QApplication(根据我的阅读Qt/MFC 文档)。

关于c++ - Qt/MFC 迁移框架工具 : properly exiting DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com