0

我有两个 MFC 项目,一个 exe 和一个 dll。exe引用dll。我通过从 exe 项目中提取一些类来创建 dll,这是我的起点。

dll 现在可以构建,但 exe 无法与 dll 的类之一的构造函数链接。我尝试 __declspec(dllexport)'ing 整个班级,但这给了我太多警告,所以我改为 __declspec(dllexport)'ed 所有公共成员。这解决了大多数链接错误,除了构造函数。

错误(MsgBoxTest 是 exe,CustomMessageBoxDlg 是 dll):

MsgBoxTestDlg.obj : error LNK2019: unresolved external symbol "public: __thiscall CMessageBoxDialog::CMessageBoxDialog(class CWnd *,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,unsigned int,class CPoint,unsigned int)" (??0CMessageBoxDialog@@QAE@PAVCWnd@@V?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@1IVCPoint@@I@Z) referenced in function "private: void __thiscall CMsgBoxTestDlg::OnDisplayMessageBox(void)" (?OnDisplayMessageBox@CMsgBoxTestDlg@@AAEXXZ)
1>Debug\MsgBoxTest.exe : fatal error LNK1120: 1 unresolved externals

构造函数声明(重载):

// Constructor of the class for direct providing of the message strings.
    __declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, CString strMessage,
        CString strTitle = _T(""), UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );

    // Constructor of the class for loading the strings from the resources.
    __declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, UINT nMessageID, UINT nTitleID = 0,
        UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );

错误引用的构造函数用法:

//this is a CMsgBoxTestDlg, m_strMessage and m_strTitle are CStrings, nStyle is an UINT, initialPosition is a CPoint
CMessageBoxDialog dlgMessageBox(this, m_strMessage, m_strTitle, nStyle, initialPosition);

我尝试了 Clean + Build,但没有雪茄

编辑:该类使用 DECLARE_DYNAMIC 和 IMPLEMENT_DYNAMIC 宏,并扩展 CDialog

4

1 回答 1

0

解决了!似乎 CString 解析为不同的类型,具体取决于您在项目选项(常规->字符集)中使用的是 Unicode 还是多字节字符。我的 dll 使用的是 Unicode,而我的 exe 使用的是多字节。将 dll 更改为使用 MB 并且构建良好。

于 2011-08-04T15:05:23.670 回答