从DLL导出MFC对话框 [英] Exporting a MFC Dialog from a DLL

查看:437
本文介绍了从DLL导出MFC对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

7月21日:更新,请参阅底部

在VC ++ 2005中,我有2个项目。首先,一个MFC DLL项目(不是扩展DLL),它有一个简单的对话框:

In VC++ 2005 I have 2 projects. Firstly, a MFC DLL project (not an extension DLL) which has a simple dialog:

#pragma once
#include "afxwin.h"
#include "resource.h"
// CTestDlg dialog
namespace Dialogs
{
    class __declspec(dllexport) CTestDlg : public CDialog
    {
        DECLARE_DYNAMIC(CTestDlg )

    public:
        CTestDlg (CWnd* pParent = NULL);   // standard constructor
        virtual ~CTestDlg ();

    // Dialog Data
        enum { IDD = IDD_TEST_DLG };
    }
}

然后我有一个Win32控制台应用程序,带有MFC库,这样做:

Then I have a Win32 console app, with MFC libraries, that does:

#include "stdafx.h"
#include "TestApp.h"
#include <TestDlg.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        nRetCode = 1;
    }
    else
    {

        Dialogs::CTestDlg dlg;
        dlg.DoModal();
    }
    return nRetCode;
}

它构建并运行,但不显示对话框。步入DoModal()...

It builds and runs up, but no dialog appears. Stepping into DoModal()...

INT_PTR CDialog::DoModal()
{
    // can be constructed with a resource template or InitModalIndirect
    ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
        m_lpDialogTemplate != NULL);

    // load resource as necessary
    LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
    HGLOBAL hDialogTemplate = m_hDialogTemplate;
    HINSTANCE hInst = AfxGetResourceHandle();
    if (m_lpszTemplateName != NULL)
    {
        hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
        HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
        hDialogTemplate = LoadResource(hInst, hResource);
    }
    if (hDialogTemplate != NULL)
        lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

    // return -1 in case of failure to load the dialog template resource
    if (lpDialogTemplate == NULL)
        return -1;

    ... more stuff

无论什么原因, t加载资源,在复制的部分的末尾返回-1。我看过几篇关于CodeGuru的文章,没有看到任何明显的东西。我的课不是出口/进口吗?还是资源问题?或者是我试图从控制台(MFC)应用程序显示的问题?

For whatever reason it seems it can't load the resource, returning -1 at the end of the copied section. I've looked at a few articles on CodeGuru, etc, and not seen anything obvious. Is my class not being exported/imported right? Or is it a resource problem? Or is the problem that I'm trying to display it from a console (MFC) app?

7月21日更新
I创建一个重写的DoModal如下所示:

21st July Update I created an overridden DoModal as so:

INT_PTR CTestDlg::DoModal()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
    return CDialog::DoModal();
}

这似乎工作,虽然我应该覆盖不同的方法来获得功能更通用?

This seems to work although should I be overriding a different method to get the functionality more generic?

推荐答案

正如你所注意到的,问题是MFC没有完善资源,因为模块上下文设置为您的主EXE而不是包含对话资源的DLL。

As you've noted, the problem is that MFC is not fining the resource, since the module context is set to your main EXE rather than the DLL containing the dialog resource.

手动调用 AFX_MANAGE_STATE 以确保DLL上下文建立是一种工作方式,但不透明。理想的方法是将DLL编译为扩展DLL,以便MFC可以处理从扩展DLL列表中加载资源,并在DLL之间管理内存。

Manually calling AFX_MANAGE_STATE to ensure the DLL context is established is one way to work this, but it's not transparent. The ideal way is to compile your DLL as an extension DLL, so that MFC can take care of loading the resource from a list of extension DLLs and managing memory between the DLLs.

您可以快速创建扩展DLL,只需创建您自己的 CDynLinkLibrary 实例,该实例将您的DLL添加到主资源列表。我没有尝试过,宁愿使用扩展名为dll _AFXDLL的路由,所以这可能或可能不起作用。

You may be able to short-cut creating the extension DLL and simply create your own CDynLinkLibrary instance, which adds your DLL to the main resource list. I have not tried this, preferring instead to take the extension dll _AFXDLL route, so this may or may not work.

MSDN文章扩展DLL 可以帮助您确定它们是否适合您的情况,以及什么他们带来的优点/缺点。

The MSDN article on Extension DLLs may help you determine if they are suitable in your case, and what advantages/drawbacks they bring.

这篇关于从DLL导出MFC对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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