Analysis of the eleven files of the components, stdafx.h
Code selection
...
#define strict
#ifndef _win32_winnt
#define _win32_winnt 0x0400
#ENDIF
#define _atl_apartment_threaded
#include
#include
#include
// You May Derive a class from ccommodule and use it ing you want to override
// Something, But Do Not Change The Name of_Module
EXTERN CCOMMODULE _MODULE;
#include
...
Code analysis
n contains ATL header files
#include
#include
N two statements
#define _atl_apartment_threaded
Defines the DLL default thread model that will become an Apartment model.
_Module is an object of ccommodule type, ccommodule implements basic functions of COM servers, implementing and providing services such as registration, instantiation, and services such as object mapping management life cycles. Note Because it is referenced by the ATL header file, the CCOMMODULE object must be globally and must be named _Module.
In addition, this class is also the center of ATL, as the main function is in the entry point (C / C ) of any program, the module object of this class handles several tasks, such as: server lock count, registering a local server class factory, according to registry Registration and reverse registration.
Second, STDAFX.CPP
...
#ifdef _atl_static_registry
#include
#include
#ENDIF
#include
Third, myProj.def output definition file
Code selection
MyProj.def: Declares The Module Parameters.
Library "MyProj.dll"
Exports
DllcanunloadNow @ 1 Private
DllgetClassObject @ 2 private
DllRegisterServer @ 3 private
DllunregisterServer @ 4 private
Code analysis
Also known as the module definition file, only the server DLL is generated in the process, it provides information on the linker (the name of the DLL file)
Fourth, myproj.tlb type library
After completing, use a #import mode when using the client.
V. myproj.idl interface definition language file
Code selection
...
Import "OAIDL.IDL"; // This file includes the definition of the idispatch interface, IMPORT is equivalent to #include
Import "OCIDL.IDL";
[
Object,
UUID (65460F9C-3BAB-4055-885A-8ED59F5FA9B0),
Dual,
Helpstring ("Imycom Interface"),
Pointer_DEFAULT (UNIQUE)
]
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);
}
[
UUID (FE651184-11DE-4D01-BD69-B07DDFA12D0C),
Version (1.0),
Helpstring ("MyProj 1.0 Type Library")
]
Library myprojlib
{
Importlib ("stdole32.tlb");
Importlib ("stdole2.tlb");
[
UUID (Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA),
Helpstring ("Mycom Class")
]
CoClass mycom
{
[default] interface iMycom;
}
}
Code analysis
The IDL file consists of three parts, each of which is composed of a pair of middle brackets and a pair of braces. Every part also contains a UUID to uniquely identify yourself
n Interface Part: COM interface definition
[
Object,
UUID (65460F9C-3BAB-4055-885A-8ED59F5FA9B0), // Interface Unique Identifier (GUID / IID)
Dual, // indicates that the client can access this interface in two ways, one is a non-script language that supports pointers, such as VC; one is a non-scripting language, such as VB, ASP
Helpstring ("Imycom Interface"), // Connect the prompt string with this interface
Pointer_DEFAULT (UNIQUE) / / Specifies the default feature of all pointers other than the properties listed in the parameter table, unique indicates that the pointer can be null, but does not support alias
]
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);
}; // method in the interface
N-type library part
[
UUID (FE651184-11DE-4D01-BD69-B07DDFA12D0C),
Version (1.0),
Helpstring ("MyProj 1.0 Type Library")
]
Library myprojlib
{
Importlib ("stdole32.tlb");
Importlib ("stdole2.tlb");
}
n Component Class Section (Component Class Nested in Type Library)
[
UUID (Feb7BDEF-FB6F-446B-BE31-DF0A3AD391BA), Helpstring ("Mycom Class")
]
CoClass mycom
{
[default] interface iMycom;
}
Add a method for the interface to add a way, which places?
Suppose the component class name is CMYCOM, the interface called Imycom, the method to join is Myf5.
First, find the definition of interface Imycom in the IDL file, where the following method is added:
[ID (5), Helpstring ("Method Myf5")] HRESULT MYF1 ([OUT, RETVAL] VARIANT_BOOL * RET);
Note: Numbers in the ID do not repeat with the ID already existing.
Second, add the following member function declaration in the CMYCOM class definition header file:
PUBLIC:
STDMETHOD (MYF5) (/ * [out, retval] * / variant_bool * ret);
Finally, the implementation of the function is added to the CMYCOM class's implementation of the CPP file:
STDMETHODIMP CMYCOM :: MYF5 (variant_bool * ret)
{
AFX_MANAGE_STATE (AFXGETSTATICModuleState ())
// Todo: Add Your Implementation Code Here
* Ret = varAint_true;
Return S_OK;
}
Author Blog:
http://blog.9cbs.net/callzjy/