您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > Visual C++开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

在VC++6.0中用MFC进行COM编程
2002-03-27· ·杨宁··

上一页  1 2 3  

  六.实现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();

上一页  1 2 3  

■ 相关内容
 公寓间的线程调度问题
 COM组件中使用用户自定义数据类型
 用ATL建立轻量级的COM对象(八)
 用托管C++开发Win表单的一般方法
 MFC文件浏览程序中改变文件读写对话框样式
 使用Microsoft Agent的COM接口编程
 COM中的可连接对象与连接点机制及其MFC程序实现
 MFC中自由使用自定义消息
 为MFC和ATL控件创建签署的CAB文件
 MFC编程特点
 应用MFC开发高级应用程序
 MFC中所提供的各种视类介绍
 MFC中常用类,宏,函数介绍
 利用MFC进行开发的通用方法介绍
 利用Visual C++/MFC开发Windows程序
 Visual C++/MFC入门教程
 MAP原理及其在MFC中的实现
 理解Visual C++.NET定制MFC应用程序
 MFC中多线程的应用
 MFC简单打印方法
 谈谈MFC中的消息映射
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!