在VC++6.0中用MFC進行COM編程

發表于:2007-07-14來源:作者:點擊數: 標簽:
首先應當明確,MFC中是通過嵌套類而不是多重繼承來實現COM接口的,通過接口映射機制將接口和實現該接口的嵌套類關聯起來;MFC中提供一套簡明的宏來實現嵌套類的定義.其次,MFC通過 CC mdTarget類實現了IUnknown接口. 本文首先描述創建一個COM服務器的步驟和核心
首先應當明確,MFC中是通過嵌套類而不是多重繼承來實現COM接口的,通過接口映射機制將接口和實現該接口的嵌套類關聯起來;MFC中提供一套簡明的宏來實現嵌套類的定義.其次,MFC通過CCmdTarget類實現了IUnknown接口.

本文首先描述創建一個COM服務器的步驟和核心代碼.然后說明客戶程序關鍵代碼.

此COM服務器實現一個TimeLogServer組件,為簡明起見,此組件只有一個接口ITimeLog,通過ITimeLog的方法OutputLog可以將日志文本輸出至日志文件.

創建一個MFC DLL工程,選擇支持Automation(當然本程序不一定是自動化服務器,在這里這樣做好處在于自動實現了幾個必要的輸出函數如DllGetClassObject,DllRegisterServer等,否則要自己寫)

第一節 COM服務器

一. 聲明組件和接口

1.寫一個GUIDs.h,在GUIDs.h中聲明組件和接口的GUID

//聲明組件GUID {A433E701-E45E-11d3-97B5-52544CBA7F28}
//DEFINE_GUID(CLSID_TimeLogServer,
//0xa433e701, 0xe45e, 0x11d3, 0x97, 0xb5, 0x52, 0x54, 0x4c, 0xba, 0x7f, 0x28);
static const IID CLSID_TimeLogServer =
{0xa433e701, 0xe45e, 0x11d3, {0x97, 0xb5, 0x52, 0x54, 0x4c, 0xba, 0x7f, 0x28}};
// 聲明接口GUID{A433E702-E45E-11d3-97B5-52544CBA7F28}
//DEFINE_GUID(IID_ITimeLog,
//0xa433e702, 0xe45e, 0x11d3, 0x97, 0xb5, 0x52, 0x54, 0x4c, 0xba, 0x7f, 0x28);
static const IID IID_ITimeLog =
{0xa433e702, 0xe45e, 0x11d3, {0x97, 0xb5, 0x52, 0x54, 0x4c, 0xba, 0x7f, 0x28}};

2.寫一個ITimeLogServer.h,在ITimeLogServer.h文件中聲明組件和接口

//ITimeLogServer.h
#include "GUIDs.h"
//接口ITimeLog的聲明
DECLARE_INTERFACE_(ITimeLog,IUnknown)
{
   STDMETHOD(OutputLog)(BSTR* varLogText)PURE;
};

說明:1.宏DEFINE_GUID將組件和接口的progid與GUID相關聯.可以用guidgen.exe工具產生.

2.宏DECLARE_INTERFACE_聲明接口;該宏第一個參數為接口名,第二個參數為該接口的基類.聲明沒有基類的接口用DECLARE_INTERFACE宏.

3.宏STDMETHOD聲明接口中的方法.此方法的返回值為HRESULT.PURE被解釋為"=0",即此方法為純虛函數.當方法的返回值不是HRESULT時,用宏STDMETHOD_(返回類型,函數名)(參數)PURE;

二.聲明組件類CTimeLogServer和實現接口的嵌套類

在ClassWizard中添加新類CTimeLogServer,其基類選擇為CCmdTarget.修改其頭文件TimeLogServer1.h,加上#include "ITimeLogServer.h";同時在類聲明體中加上

//聲明實現ITimelog接口的嵌套類
BEGIN_INTERFACE_PART(TimeLog,ITimeLog)//自動聲明IUnknown接口的三個方法
  STDMETHOD(OutputLog)(BSTR* varLogText);
END_INTERFACE_PART(TimeLog)
//聲明接口映射
DECLARE_INTERFACE_MAP()
//聲明類廠
DECLARE_OLECREATE(CTimeLogServer)

三.實現類廠和接口映射

在CTimeLogServer的實現文件中寫入:

//實現類廠
IMPLEMENT_OLECREATE(CTimeLogServer,"TimeLogServer",
           0xa433e701, 0xe45e, 0x11d3, 0x97, 0xb5, 0x52, 0x54, 0x4c, 0xba, 0x7f, 0x28);
//映射接口到相應的嵌套類
BEGIN_INTERFACE_MAP(CTimeLogServer,CCmdTarget)
  INTERFACE_PART(CTimeLogServer,IID_ITimeLog,TimeLog)
END_INTERFACE_MAP()
四.在組件的構造和析構函數中對全局對象計數
CTimeLogServer::CTimeLogServer()
{
::AfxOleLockApp();
}

CTimeLogServer::~CTimeLogServer()
{
::AfxOleUnlockApp();
}
五.為嵌套類實現IUnknown接口

//為嵌套類而實現IUnknown接口
STDMETHODIMP_(ULONG)
CTimeLogServer::XTimeLog::AddRef()
{
METHOD_PROLOGUE(CTimeLogServer,TimeLog)
return pThis->ExternalAddRef();
}

STDMETHODIMP_(ULONG)
CTimeLogServer::XTimeLog::Release()
{
METHOD_PROLOGUE(CTimeLogServer,TimeLog)
return pThis->ExternalRelease();
}

STDMETHODIMP
CTimeLogServer::XTimeLog::QueryInterface(REFIID riid,void**ppvObj)
{
METHOD_PROLOGUE(CTimeLogServer,TimeLog)
return pThis->ExternalQueryInterface(&riid,ppvObj);
}
說明:雖然CCmdTarget類已經實現了IUnknown接口,但是還必須通過上述代碼來將嵌套類的IUnknown映射到CCmdTarget支持的IUnknown接口.METHOD_PROLOGUEH宏的兩個參數分別是實現組件對象的類和實現接口的嵌套類.

六.實現ItimeLog接口的方法OutputLog

注意本組件的功能是往日志文件中輸入日志.

1. 在組件類中添加一個文件指針:

  // Attributes
  public:
  protected:
    FILE* m_logfile;

2. 初始化和退出

首先在CTimeLogServer的構造函數中進行一些初始化:

CTimeLogServer::CTimeLogServer()
{
::AfxOleLockApp();
CTime TimeStamp = CTime::GetCurrentTime();
CString FileName;
FileName.Format(_T("%s.log"),TimeStamp.Format("%Y%m%d"));
m_logfile = fopen(FileName,_T("a"));
if(m_logfile)
{
fprintf(m_logfile,_T("# # # # # # # # # # # # # # # # # \n"));
fprintf(m_logfile,_T("開始于:%s"),(LPCTSTR)TimeStamp.Format("%Y年%m月%d日%H:%M %S"));
fprintf(m_logfile,_T("\n"));
}
}
//然后在析構函數中關閉文件
CTimeLogServer::~CTimeLogServer()
{
::AfxOleUnlockApp();
if(m_logfile)
{
CTime TimeStamp = CTime::GetCurrentTime();
fprintf(m_logfile,_T("\n"));
fprintf(m_logfile,_T("結束于:%s"),(LPCTSTR)TimeStamp.Format("%Y年%m月%d日%H:%M %S"));
        fprintf(m_logfile,_T("\n"));
fprintf(m_logfile,_T("# # # # # # # # # # # # # # # # #\n"));
fclose(m_logfile);
}
}
3. 實現接口ITimeLog方法

//實現接口ITimeLog方法
STDMETHODIMP
CTimeLogServer::XTimeLog::OutputLog(BSTR* varLogText)
{
METHOD_PROLOGUE(CTimeLogServer,TimeLog)
if(pThis->m_logfile)
{
CTime TimeStamp = CTime::GetCurrentTime();
CString NowTime = TimeStamp.Format("%Y年%m月%d日%H:%M:%S");
CString LogText((LPCWSTR)*varLogText);
fprintf(pThis->m_logfile,"\n%s\n%s\n%",NowTime,LogText);
return NOERROR;
}
else
{
AfxMessageBox("沒有日志文件!");
return S_FALSE;
}
}
七.完善組件服務器

在應用對象CTimeLogServerApp的 實現文件中,處理Instance()和ExitInstance()

BOOL CTimeLogServerApp::InitInstance()
{
::AfxOleLockApp();
// Register all OLE server (factories) as running.  This enables the
//  OLE libraries to create objects from other applications.
COleObjectFactory::RegisterAll();

return TRUE;
}
int CTimeLogServerApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
    ::AfxOleUnlockApp();
return CWinApp::ExitInstance();
}
第二節 客戶程序

使用COM組件服務器的客戶程序關鍵步驟是:初始化COM庫,創建組件對象并獲取IUnknown接口指針,查詢接口并使用,釋放組件.

    #include "ITimeLogServer.h"
    //初始化COM庫,對組件實例化
HRESULT hResult;
    IUnknown* pIUnknown;
hResult = ::CoInitialize(NULL);
if(FAILED(hResult))
{
::AfxMessageBox("不能初始化COM庫!");
return FALSE;
}

    //創建組件實例
    pIUnknown = NULL;
hResult = ::CoCreateInstance(CLSID_TimeLogServer,NULL,
CLSCTX_INPROC_SERVER,IID_IUnknown,(void**)&pIUnknown);
if(FAILED(hResult))
{
pIUnknown = NULL;
::AfxMessageBox("不能創建TimeLog對象!");
return FALSE;
}
    //查詢接口并使用
    if(pIUnknown!=NULL)
{
ITimeLog* pITimeLog;
HResult=pIUnknown->QueryInterface(IID_ITimeLog,(void**)&pITimeLog);
if(FAILED(hResult))
{
::AfxMessageBox("不能獲取接口ITimeLog!");
pIUnknown->Release();
return;
}
BSTR bstrLogText;
bstrLogText = m_logtext.AllocSysString();
CString text((LPCWSTR)bstrLogText);
::AfxMessageBox(text);

if(FAILED(pITimeLog->OutputLog(&bstrLogText)))
{
::AfxMessageBox("日志輸出出錯!");
pITimeLog->Release();
return;
}
pITimeLog->Release();
::AfxMessageBox("日志已經寫入!");
}
        //釋放組件
        pIUnknown->Release();
        pIUnknown = NULL;
    ::CoUninitialize();

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97