HOWTO: 给 ATL 项目添加 MFC 支持

<script type="text/javascript"> </script>

文章编号:173974
最后修改:2004年1月21日
修订:2.0
本文的发布号曾为 CHS173974

<script type="text/javascript"> </script> <script src="common/script/gsfx/kbtoc.js?10" type="text/javascript"></script>

概要

<script type="text/javascript"> </script>
当使用 AppWizard 创建 ATL EXE 项目时,MFC 支持复选框未被选中。 本文讲述如何将 MFC 支持添加到 ATL EXE 项目中。 本文还解释了如果在 AppWizard 中最初不能选中“Support MFC”复选框,如何给 ATL DLL 项目添加 MFC 支持。

回到顶端

更多信息

<script type="text/javascript"> </script>

给 ATL EXE 项目添加 MFC 支持

<script type="text/javascript"> </script>

1.在包括 Atlbase.h 之前,将以下 #include 指令添加到 StdAfx.h:
 #include <afxwin.h>   // MFC core and standard components
      #include <afxext.h>   // MFC extensions
      #include <afxdisp.h>  // MFC Automation extensions
2.

 

更改项目设置以使用 MFC。 在 Project Settings 对话框中,单击 General 选项卡,然后将 Microsoft Foundation Classes 列表框中的设置更改为 MFC。

 

3.

添加 CWinApp 衍生类,并声明一个该类型的全局变量,如下所示:

class CMyApp : public CWinApp
      {
      public:
         virtual BOOL InitInstance();
         virtual int ExitInstance();
      protected:
      BOOL m_bRun;
};
4.用以下 InitInstance 和 ExitInstance 代码替换 _tWinMain 函数:
BOOL CMyApp::InitInstance()
{
// Initialize OLE libraries.
if (!AfxOleInit())
{
AfxMessageBox(_T("OLE Initialization Failed!"));
return FALSE;
}

// Initialize CcomModule.
_Module.Init(ObjectMap, m_hInstance);
_Module.dwThreadID = GetCurrentThreadId();

// Check command line arguments.
TCHAR szTokens[] = _T("-/");
m_bRun = TRUE;
LPCTSTR lpszToken = FindOneOf(m_lpCmdLine, szTokens);
while (lpszToken != NULL)
{
// Register ATL and MFC class factories.
if (lstrcmpi(lpszToken, _T("Embedding"))==0 ||
lstrcmpi(lpszToken, _T("Automation"))==0)
{
AfxOleSetUserCtrl(FALSE);
break;
}
// Unregister servers.
// There is no unregistration code for MFC
// servers. Refer to <LINK TYPE="ARTICLE" VALUE="Q186212">Q186212</LINK> "HOWTO: Unregister MFC
// Automation Servers" for adding unregistration
// code.
else if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, FALSE)));
VERIFY(SUCCEEDED(_Module.UnregisterServer(TRUE)));
m_bRun = FALSE;
break;
}
// Register ATL and MFC objects in the registry.
else if (lstrcmpi(lpszToken, _T("RegServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, TRUE)));
VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE)));
COleObjectFactory::UpdateRegistryAll();
m_bRun = FALSE;
break;
}
lpszToken = FindOneOf(lpszToken, szTokens);
}
if (m_bRun)
{
// Comment out the next line if not using VC 6-generated
// code.
_Module.StartMonitor();

VERIFY(SUCCEEDED(_Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE)));
VERIFY(COleObjectFactory::RegisterAll());
// To run the EXE standalone, you need to create a window
// and assign the CWnd* to m_pMainWnd.
LPCTSTR szClass = AfxRegisterWndClass(NULL);
m_pMainWnd = new CWnd;
m_pMainWnd->CreateEx(0, szClass, _T("SomeName"), 0, CRect(0, 0, 0, 0), NULL, 1234);
}
return TRUE;
}

int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
{
_Module.RevokeClassObjects();
        Sleep(dwPause); //wait for any threads to finish
    }

    _Module.Term();
return 0;
}
5.对于 Unicode 版本,请确保进入点被设置为 wWinMainCRTStartup,该设置在 Project Settings 对话框中 Link 字段的 Output 类别中。 有关其它信息,请参见 Microsoft Knowledge Base 中的下列文章:
125750 (http://support.microsoft.com/kb/125750/EN-US/) PRB: 错误 LNK2001: “_WinMain@16”: 不能解析的外部符号

6. 将以下代码行添加到 COM 接口、窗口过程和导出函数的每个成员函数的开头:
AFX_MANAGE_STATE(AfxGetAppModuleState());
有关 AFX_MANAGE_STATE 的详细信息,请查询 VC++ 联机文档。 有关将 MFC 支持添加到 ATL COM AppWizard 项目的详细信息,请参见下面的 Microsoft Knowledge Base 文章:
181505 (http://support.microsoft.com/kb/181505/EN-US/) PRB: ATL COM AppWizard 不提供对 .EXE 的 MFC 支持
将 MFC 支持添加到 ATL DLL 项目
执行上面的步骤 1 到步骤 3。

1.

将 AppWizard 生成的 DllMain 的 DLL_PROCESS_ATTACH 和 DLL_PROCESS_DETACH 中的代码移到 CMyApp 的 InitInstance 和 ExitInstance,并删除 DllMain,如下所示:


BOOL CMyApp::InitInstance()
      {
         _Module.Init(ObjectMap, m_hInstance);
         return CWinApp::InitInstance();
      }

int CMyApp::ExitInstance()
{
    // MFC's class factories registration is
    // automatically revoked by MFC itself.
    if (m_bRun)
        _Module.RevokeClassObjects();
}
2.将以下代码行添加到 COM 接口、窗口过程和导出函数的每个成员函数的开头:
AFX_MANAGE_STATE(AfxGetStaticModuleState());
有关其它信息,请参见 Microsoft Knowledge Base 中的下列文章:
140850 (http://support.microsoft.com/kb/140850/EN-US/) HOWTO: 转换 DLLTRACE 以使用共享库中的 MFC
备注:对于所有的发行版本,请确保 _ATL_MIN_CRT 预处理器定义已经被删除。 您可以在 Project Settings 对话框中 C/C++ 选项卡的 Preprocessor 类别中找到这些定义。

当使用 ClassWizard 将一个从 MFC 类衍生的类添加到 ATL EXE 项目或 ATL DLL 项目时,如果没有“MFC 支持”,编译器将发出 C2504 错误消息。

参考

<script type="text/javascript"> </script>
MFCATL 示例附带在 Visual C++ 5.0 中。

有关其它信息,请参见 Microsoft Knowledge Base 中的下列文章:
186212 (http://support.microsoft.com/kb/186212/EN-US/) HOWTO: 撤消 MFC 自动功能服务器的注册
(c) Microsoft Corporation 1997,保留所有权利。 作者:Chuck Bell,Microsoft Corporatio。
这篇文章中的信息适用于:
Microsoft ActiveX Template Library 2.0 当用于
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++, 32-bit Learning Edition 6.0
Microsoft ActiveX Template Library 2.1 当用于
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++, 32-bit Learning Edition 6.0
Microsoft ActiveX Template Library 3.0 当用于
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++, 32-bit Learning Edition 6.0

 

 

更多技术文章请参看施昌权的个人网站: http://www.joyvc.cn

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值