使用 /clr 编译 MFC 应用程序(步骤 1:链接动态 DLL)
我已成功将使用旧版本 Developer Studio 编译的 MFC 应用程序升级到 Visual Studio 2008。由于 MFC 中存在一些重大更改,因此需要进行极少量的更改。 现在一切正常,我想采取下一步并使用 /clr
编译解决方案。
为此,我在 MSDN 网站上找到了有用的信息:
以下是我采取的步骤:
- 将运行时库设置为多线程调试 DLL (/MDd)。
- 将使用 MFC 设置为在共享 DLL 中使用 MFC。
但是,这样做会阻止我链接该项目:
无法再解析对
afxData
的引用; 不知何故,afxData
仅在静态链接到 MFC 时才可见。 在我的代码中,我有以下声明:extern AFX_DATA AUX_DATA afxData;
它适用于静态链接的 MFC 版本。
对
_afxThreadState
和_afxWinState
的引用也无法解析。
以下是完整的错误消息:
error LNK2001: unresolved external symbol "struct AUX_DATA afxData" (?afxData@@3UAUX_DATA@@A)
error LNK2001: unresolved external symbol "class CThreadLocal<class _AFX_THREAD_STATE> _afxThreadState" (?_afxThreadState@@3V?$CThreadLocal@V_AFX_THREAD_STATE@@@@A)
error LNK2001: unresolved external symbol "class CProcessLocal<class _AFX_WIN_STATE> _afxWinState" (?_afxWinState@@3V?$CProcessLocal@V_AFX_WIN_STATE@@@@A)
以防万一这可能与名称修改有关...
那么,我该怎么做才能动态链接到 MFC,但仍然引用 afxData
、_afxThreadState
和_afxWinState
?
I have successfully upgraded an MFC application which was compiled with an old version of Developer Studio to Visual Studio 2008. A very small number of changes were needed because of some breaking changes in MFC. Now that everything works, I'd like to take the next step and compile the solution with /clr
.
To do so, I have found useful information on the MSDN web site:
Here are the steps I have taken:
- Set the Runtime Library to Multi-threaded Debug DLL (/MDd).
- Set the Use of MFC to Use MFC in a Shared DLL.
However, doing so prevents me from linking the project:
The reference to
afxData
can no longer be resolved; somehow,afxData
is only visible when linking statically against MFC. In my code, I have the following declaration:extern AFX_DATA AUX_DATA afxData;
which works fine with the statically linked MFC version.
The references to
_afxThreadState
and_afxWinState
cannot be resolved either.
Here are the full error messages :
error LNK2001: unresolved external symbol "struct AUX_DATA afxData" (?afxData@@3UAUX_DATA@@A)
error LNK2001: unresolved external symbol "class CThreadLocal<class _AFX_THREAD_STATE> _afxThreadState" (?_afxThreadState@@3V?$CThreadLocal@V_AFX_THREAD_STATE@@@@A)
error LNK2001: unresolved external symbol "class CProcessLocal<class _AFX_WIN_STATE> _afxWinState" (?_afxWinState@@3V?$CProcessLocal@V_AFX_WIN_STATE@@@@A)
in case this might be related to the name mangling...
So, what can I do in order to dynamically link against MFC, but still reference afxData
, _afxThreadState
and _afxWinState
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在共享 DLL 中使用 MFC 时,必须
dllimport
导入 AUX_DATA。对于线程状态,不要使用模板类,而是使用其中之一,
具体取决于您需要的状态信息。 例如,请参阅此处了解某些成员已移至模块状态以修复错误。 请注意,AfxGetModuleThreadState 存在,但未记录,可能会在未来版本中更改。
Win状态下不响铃,怎么用? 可能有一种新方法可以实现这一目标。
When using MFC in a shared DLL, AUX_DATA must be
dllimport
ed.For thread state, rather than using template classes, use one of these
Depending on the state information you require. For example, see here for some members that were moved to module state to fix a bug. Note that AfxGetModuleThreadState exists but is undocumented and could be changed in a future version.
Win state does not ring a bell, how is it used? There is probably a new way to accomplish it.