你是不是用atl写阿,如果是的话,最简单的方法就是你新建一个工程,在向导那里选择支持mfc就不用这么麻烦了!

解决方案 »

  1.   

    是用ATL写的,只有宿主文件类型为dll才有选择支持mfc,而且这COM已经定型了,再改写的话太麻烦了。请各位指导指导!
      

  2.   

    来源:msdnAdding MFC Support to an ATL EXE Project
    Add the following #include directives to StdAfx.h prior to including Atlbase.h:      #include <afxwin.h>   // MFC core and standard components
          #include <afxext.h>   // MFC extensions
          #include <afxdisp.h>  // MFC Automation extensions 
    Change project settings to use MFC. From the Project Settings dialog box, click the General tab, and change the setting in the "Microsoft Foundation Classes" list box to MFC.Add a CWinApp derived class and declare a global variable of that type as follows:class CMyApp : public CWinApp
          {
          public:
             virtual BOOL InitInstance();
             virtual int ExitInstance();
          protected:
          BOOL m_bRun;
    }; 
    Replace the _tWinMain function with the following InitInstance and ExitInstance code: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;

    For Unicode builds, make sure the entry point is set to wWinMainCRTStartup in the Output category of the Link field in the Project Settings dialog box. For additional information, please see the following article in the Microsoft Knowledge Base:
    Q125750 PRB: Error LNK2001: '_WinMain@16': Unresolved External Symbol 
    Add the following line of code to the beginning of every member function of a COM interface, window procedure, and exported function: AFX_MANAGE_STATE(AfxGetAppModuleState()); 
    For more information on AFX_MANAGE_STATE, consult the VC++ online documentation. 
    For more information about adding MFC support to an ATL COM AppWizard project, please see the following article in the Microsoft Knowledge Base:Q181505 PRB: ATL COM AppWizard Doesn't Offer MFC Support for .EXE 
    Adding MFC Support to an ATL DLL Project
    Follow steps 1 to 3 from above. 
    Move the code in the AppWizard-generated DllMain's DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH to the CMyApp's InitInstance and ExitInstance and remove the DllMain as follows:      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(); 
    Add the following line of code to the beginning of every member function of a COM interface, window procedure and exported function: AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
    For additional information, please see the following article in the Microsoft Knowledge Base:
    When adding a class derived from a MFC class using the ClassWizard to an ATL EXE project or to an ATL DLL project without "MFC Support," the compiler will issue a C2504 error.
      

  3.   

    请问在ATL EXE项目的什么文件的什么地方做上面提到的这一步:
    Add a CWinApp derived class and declare a global variable of that type as follows:class CMyApp : public CWinApp
          {
          public:
             virtual BOOL InitInstance();
             virtual int ExitInstance();
          protected:
          BOOL m_bRun;
    }; 
      

  4.   

    我按照如上提示,StdAfx.h修改好,在project.cpp中加入了class CMyApp类定义,并把_tWinMain函数替换成CMyApp::InitInstance()和CMyApp::ExitInstance(),编译连接仍然出错,错误提示和出错行如下,请大家help!!!
    \project.cpp(436) : error C2660: 'Init' : function does not take 2 parameters
    \project.cpp(459) : error C2065: 'IDR_ServerS2B' : undeclared identifier
    \project.cpp(460) : error C2660: 'UnregisterServer' : function does not take 1 parameters
    \project.cpp(468) : error C2660: 'RegisterServer' : function does not take 1 parameters
    \project.cpp(479) : error C2039: 'StartMonitor' : is not a member of 'CServiceModule'
            d:\myproject\stdafx.h(26) : see declaration of 'CServiceModule'
    \project.cpp(499) : error C2065: 'dwPause' : undeclared identifierBOOL CMyApp::InitInstance()
    {
    ...
    /*(436)*/ _Module.Init(ObjectMap, m_hInstance);
    ...
    /*(459)*/ VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, FALSE)));
    /*(460)*/ VERIFY(SUCCEEDED(_Module.UnregisterServer(TRUE)));
    ...
    /*(468)*/ VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE)));
    /*(479)*/ _Module.StartMonitor();
    }int CMyApp::ExitInstance()
    {
    /*(499)*/   Sleep(dwPause); //wait for any threads to finish
    }
      

  5.   

    你把几个.h文件包含到.cpp的前面来。
      

  6.   

    总之:
    COM宿主文件类型为Service(EXE),在COM接口函数中读取文本文件内容并送出,使用什么方法对文件操作?CFile,CString等类不支持。
      

  7.   

    要根据自己的项目来改
    我这里编译就没错误
    可以使用MFC类