在VC++6.0中用MFC进行COM编程(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(";# # # # # # # # # # # # # # # # #
";));
fprintf(m_logfile,_T(";开始于:%s";),(LPCTSTR)TimeStamp.Format(";%Y年%m月%d日%H:%M %S";));
fprintf(m_logfile,_T(";
";));
}
}
//然后在析构函数中关闭文件
CTimeLogServer::~CTimeLogServer()
{
::AfxOleUnlockApp();
if(m_logfile)
{
CTime TimeStamp = CTime::GetCurrentTime();
fprintf(m_logfile,_T(";
";));
fprintf(m_logfile,_T(";结束于:%s";),(LPCTSTR)TimeStamp.Format(";%Y年%m月%d日%H:%M %S";));
fprintf(m_logfile,_T(";
";));
fprintf(m_logfile,_T(";# # # # # # # # # # # # # # # # #
";));
fclose(m_logfile);
}
}