Sixth, mycom.h
Code selection
// mycom.h: Declaration of the cmycom
#ifndef __mycom_h_
#define __mycom_h_
#include "resource.h" // main symbols
// CMYCOM
Class ATL_NO_VTABLE CMYCOM:
Public CComobjectrootex
Public ccomcoclass
Public IDispatchImpl
{
PUBLIC:
CMYCOM () {}
Declare_registry_resourceId (idR_mycom)
Declare_protect_final_construct ()
Begin_COM_MAP (CMYCOM)
Com_interface_entry (iMycom)
COM_ITERFACE_ENTRY (Idispatch)
END_COM_MAP ()
// Imycom
PUBLIC:
STDMETHOD (MYF4) (/ * [in] * / int x, / * [out, retval] * / int * val);
STDMETHOD (/ * [in] * / bstr str, / * [out, retval] * / bstr * RetStr);
STDMETHOD (MYF2) (/ * [in] * / bstr str, / * [out, retval] * / int * val);
STDMETHOD (Myf1) ();
}
#ENDIF / / / __ mycom_h_
Code analysis
n ccomobjectrootex
It is one of the classes that must inherit each COM component, and this type of management component is quoted. Since the reference count is important, all ATL COM components must inherit from CComObjectroTex.
N ccomcoclass
If a class is inherited from ccomcoClass, ATL ensures that this class must be the default class object. ATL supports a default factory implementation, and there is a function that makes it possible to recover object CLSID and set an error message.
n begin_com_map (cmycom)
Com_interface_entry (iMycom)
COM_ITERFACE_ENTRY (Idispatch)
END_COM_MAP ()
BEGIN_COM_MAP and END_COM_MAP These two macros define the interface mapping of COM. The interface column in the map is the interface of the interface pointer that QueryInterface can return.
n In addition, the CComobject class implements a method that belongs to the IUnknown interface. This class has always been the largest derived class that provides the choice of aggregation and lock models. However, the call to Queryinterface, AddRef, Release is delegated to CComobjectroTex
N, we have seen a statement of four functions that are similar to interface definition language files.
STDMETHOD (MYF4) (/ * [in] * / int x, / * [out, retval] * / int * val);
STDMETHOD (/ * [in] * / bstr str, / * [out, retval] * / bstr * RetStr);
STDMETHOD (MYF2) (/ * [in] * / bstr str, / * [out, retval] * / int * val);
STDMETHOD (Myf1) ();
So what is the meaning of what they have?
There is an interface function (method) in the interface definition language file (method)
Interface Imycom: idispatch
{
[ID (1), Helpstring ("Method Myf1")] HRESULT MYF1 ();
[ID (2), Helpstring ("Method Myf2")] HRESULT MYF2 ([In] BSTR STR, [OUT, RETVAL] INT * VAL);
[ID (3), Helpstring ("Method Myf3")] HRESULT MYF3 ([In] BSTR STR, [OUT, RETVAL] BSTR * RETSTR);
[ID (4), Helpstring ("Method Myf4")] HRESULT MYF4 ([IN] INT X, [OUT, RETVAL] INT * VAL);
}
This is all for interfaces, and the interface only contains the collection of these functions, which does not implement these functions. The function is implemented by the CMYCOM Class. Generate a component object when component class is instantiated.
Seven, mycom.cpp
Code selection
// mycom.cpp: Implementation of CMYCOM
#include "stdafx.h"
#include "myproj.h"
#include "mycom.h"
// CMYCOM
STDMETHODIMP CMYCOM :: MYF1 ()
{
AFX_MANAGE_STATE (AFXGETSTATICModuleState ())
// Todo: Add Your Implementation Code Here
AfxMessageBox ("Welcome to My Components");
Return S_OK;
}
...
Code analysis
The component class is implemented in the specific implementation of the interface function.
Eight, myproj.h
Code selection
slightly
Code analysis
Internal definition of virtual functions
Nine, myproj.cpp
Code selection
// myproj.cpp: Implement OF DLL Exports.
// Note: Proxy / Stub Information
// TO Build A Separate Proxy / Stub DLL,
// Run nmake -f myprojps.mk in The Project Directory.
#include "stdafx.h"
#include "resource.h"
#include
#include "myproj.h"
#include "myproj_i.c"
#include "mycom.h"
CCOMMODULE _MODULE; //
Begin_Object_map (ObjectMap)
Object_entry (CLSID_MYCOM, CMYCOM) // Object Map, there is an entry for each server
END_OBJECT_MAP ()
Class CMYPROJAPP: PUBLIC CWINAPP
{
PUBLIC:
// Overrides
// ClassWizard Generated Virtual Function Overrides
// {{AFX_VIRTUAL (CMYPROJAP)
PUBLIC:
Virtual Bool InitInstance ();
Virtual int exitInstance ();
//}} AFX_VIRTUAL
// {{AFX_MSG (CMYPROJAPP)
// Note - The ClassWizard Will Add and Remove Member functions here.
// Do Not Edit What You See in these Blocks of generated code!
//}} AFX_MSG
Declare_message_map ()
}
Begin_MESSAGE_MAP (CMYPROJAPP, CWINAPP)
// {{AFX_MSG_MAP (CMYPROJAPP)
// Note - The Classwizard Will Add and Remove Mapping Macros Here.
// Do Not Edit What You See in these Blocks of generated code!
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
CMYPROJAPP THEAPP;
Bool cmyprojapp :: initInstance ()
{
_Module.init (ObjectMap, M_HINSTANCE, & LIBID_MYPROJLIB);
Return CWINAPP :: InitInstance ();
}
Int cmyprojapp :: exitInstance ()
{
_Module.Term ();
Return CWINAPP :: EXITINSTANCE ();
}
// use to determine WHETER THE DLL CAN BE Unloaded by OLE
StDAPI DLLCanunloadNow (Void)
{
AFX_MANAGE_STATE (AFXGETSTATICModuleState ());
Return (AFXDLLCanunloadNow () == S_OK && _Module.GetLockCount () == 0)? S_OK: S_FALSE;
}
// Returns a class factory to create an Object of the Requested Type
StDAPI DLLGETCLASSOBJECT (Refclsid Rclsid, Refiid Riid, LPVOID * PPV)
{
Return_Module.getClassObject (RCLSID, RIID, PPV);
}
// DLLREGISTERSERVER - Adds Entries to the system registry
StDAPI DLLREGISTERSERVER (VOID)
{
// Registers Object, Typelib and All Interfaces in Typelib
Return_Module.RegisterServer (TRUE);
}
// DllunregisterServer - Removes Entries from the system registry
StDAPI DllunregisterServer (Void)
{
Return_Module.unregisterServer (TRUE);
}
Author Blog:
http://blog.9cbs.net/callzjy/