Programming °æ (¾«»ªÇø)

·¢ÐÅÈË: lofe ()¸Ð¼¤Éú»î(), ÐÅÇø: Programming
±ê  Ìâ: A Sample Code of COM, Written in VC6
·¢ÐÅÕ¾: ¹þ¹¤´ó×϶¡Ïã (2000Äê08ÔÂ25ÈÕ08:56:38 ÐÇÆÚÎå), Õ¾ÄÚÐżþ

±ê  Ìâ: A Sample Code of COM, Written in VC6
·¢ÐÅÕ¾: Î人°×Ôƻƺ×Õ¾ (Sat May 13 13:42:31 2000), Õ¾ÄÚÐżþ

    Ê×ÏÈÓ¦µ±Ã÷È·,MFCÖÐÊÇͨ¹ýǶÌ×Àà¶ø²»ÊǶàÖؼ̳ÐÀ´ÊµÏÖCOM½Ó¿ÚµÄ,ͨ¹ý½Ó¿ÚÓ³Éä
»úÖÆ ½«½Ó¿ÚºÍʵÏָýӿڵÄǶÌ×Àà¹ØÁªÆðÀ´;MFCÖÐÌṩһÌ×¼òÃ÷µÄºêÀ´ÊµÏÖǶÌ×ÀàµÄ¶¨
Òå.Æä ´Î,MFCͨ¹ýCCmdTargetÀàʵÏÖÁËIUnknown½Ó¿Ú.
   ±¾ÎÄÊ×ÏÈÃèÊö´´½¨Ò»¸öCOM·þÎñÆ÷µÄ²½ÖèºÍºËÐÄ´úÂë.È»ºó˵Ã÷¿Í»§³ÌÐò¹Ø¼ü´úÂë.
´ËCOM·þÎñÆ÷ʵÏÖÒ»¸öTimeLogServer×é¼þ,Ϊ¼òÃ÷Æð¼û,´Ë×é¼þÖ»ÓÐÒ»¸ö½Ó¿ÚITimeLog,ͨ
 ¹ýITimeLogµÄ·½·¨OutputLog¿ÉÒÔ½«ÈÕÖ¾Îı¾Êä³öÖÁÈÕÖ¾Îļþ.
´´½¨Ò»¸öMFC DLL¹¤³Ì,Ñ¡ÔñÖ§³ÖAutomation(µ±È»±¾³ÌÐò²»Ò»¶¨ÊÇ×Ô¶¯»¯·þÎñÆ÷,ÔÚÕâÀï
ÕâÑù×öºÃ´¦ÔÚÓÚ×Ô¶¯ÊµÏÖÁ˼¸¸ö±ØÒªµÄÊä³öº¯ÊýÈçDllGetClassObject,DllRegisterSer
verµÈ,·ñÔòÒª×Ô¼ºÐ´)

         µÚÒ»½Ú 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, 0
x7f, 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¿â,´´½¨×é¼þ¶ÔÏó²¢»ñÈ¡IUnkn
own½Ó¿ÚÖ¸Õë,²éѯ½Ó¿Ú²¢Ê¹ÓÃ,ÊÍ·Å×é¼þ.
    #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
**)&
ITimeLog);
                        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();
    ±¾Õ¾ËùÌṩËùÓÐ×ÊÔ´¶¼²»×÷ΪÉÌÒµÓÃ;£¬Èç¹ûÄãÎ¥·´±¾¹æ¶¨½«×ÔÐге£ËùÓÐÔðÈΣ¬
δ¾­±¾Õ¾ÔÊÐí²»µÃ¸´ÖƱ¾Õ¾À¸Ä¿ºÍÄÚÈÝ
VCHelpÍøÕ¾°æȨËùÓРÎÅâùÑóÖÆ×÷
--

--

¶À¹Â¾Å½£µÄ×î¸ß¾³½çÊDz»Äý£
±à³ÌµÄ×î¸ß¾³½çÊÇÎÞËùν¹¤¾ß

            ----- Sun Ëù´Í
-------------------------------------------

¡ù À´Ô´:¡¤¹þ¹¤´ó×϶¡Ïã bbs.hit.edu.cn¡¤[FROM: malacs.hit.edu.cn]
[°Ù±¦Ïä] [·µ»ØÊ×Ò³] [Éϼ¶Ä¿Â¼] [¸ùĿ¼] [·µ»Ø¶¥²¿] [Ë¢ÐÂ] [·µ»Ø]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º8.131ºÁÃë