Author: Saurabh Dasgupta
Article Source: http://www.codeguru.com/activex/comstepbystep.html
If the translator's words must first be declared, this takes an article from www.codegurbu.com, I just translated it. To tell you, if you are already a COM master, please don't waste time to read it, if you want to give me this novice, I am not grateful. If you are entither in learning COM, I think he is still very helpful, at least, you can do it by manually adding code instead of using the Visual C wizard, let you know a simple COM object, Need to do work. Because I have written a lot of shallow text, because I have not written a content introduction, I was discounted. So I wrote these texts, I hope that others will quickly understand the content of the article, read in not reading, and choose. We are willing to share with all beginners.
table of Contents:
Introduction
interface
Step 1: Create an IDL file
Step 2: Generate Type Library
Step 3: Inheriting from IADD
Step 4: Implementation of IADD
Step 5: Realize IUNKOWN
Step 6: Class Factory
Step 7: Implementation of ICLASSFACTORY
Step 8: Realize DllgetClassObject
Step Ninth: Realize DLLCanunloadNow
Step 10: DllregisterServer and UnregisterServer
Step 11: Insert the IDL file into the workspace
Step 12: Use COM just created in Visual Basic
Step 13: Analyze all the documents we just created
Step 14: Embed Type Library in ActiveX DLL
Step 15: From the Visual C client, use the COM object created.
Introduction
For me, understanding COM (Component Object Model) is at least once more than one adventure. I believe that each programmer who wants to understand the basic theory behind COM must have written at least one simple COM object with the C language with a template and macro help without MFC / ATL. In this article, I will introduce the steps to create a simple COM object according to the basic principle of COM. This created component can be used in VC / VB clients.
As an exercise, we will try to design a COM object to implement an imaginary super fast additive algorithm. This component has two long integer (long type) parameters, and its return value is also a long integer, which is the result of our addition algorithm. Let's start designing this interface.
interface
The interface of the COM object, we will not talk about the actual implementation, but to talk about the use of this COM object and communicate with the method of Method Signatures. We will name our interface for IADD. The declaration of this interface is done using the interface definition language. The interface definition language is used to define function signatures, which uses a format independently of the programming language, which helps RPC Organize parameters from a computer to another packaging, distribution, and unpacking. In our interface IADD, we have two methods setFirstnumber and setsecondNumber, which are used to deliver the parameters used by the addition. Then, there is another method DotHeadDition, which is used to calculate the actual results and return the results to the customer. Step 1: Create an IDL file
Create a new Win32 DLL project (named addObj). The next file will be created to this folder. Create an empty file and type the following, save it as Iadd.idl. Interface identifier is generated using uuidgen.exe (uuidgen.exe is a DOS file, you can run it directly on the command line)
Import "unknwn.idl";
[
Object,
UUID (1221DB62-F3D8-11D4-825D-00104B
3646C
0),
Helpstring ("Interface Iadd Is Used for Implementing A Super-Fast Addition Algorithm")
]
Interface Iadd: IUNKNOWN
{
HRESULT SETFIRSTNUMBER (long NX1);
HRESULT SETSECONDNUMBER (long NX2);
HRESULT DOTHEADDITION ([OUT, RETVAL] Long * PBuffer;
}
[
UUID (3FF1AAB8-F3D8-11D4-825D-00104B
3646C
0),
Helpstring ("Interfaces for Code Guru Algorithm Implementations")
]
Library CodeGurumathlib
{
Importlib ("stdole32.tlb");
Importlib ("stdole2.tlb");
Interface Iadd;
}
Chapter 2 Generate Type Library
Use the IDL compiler MIDL.EXE to compile the interface definition file Iadd.idl. After the compilation is completed, the following files will be generated:
Iadd.h
Interface declaration containing C format
DLLDATA.C
Contains code for the agent DLL. Use when the object is called on a different processor / computer
Iadd.tlb
binary file. Use a defined format to fully describe our IADD interface and all of it. This file and COM component are distributed to all customers.
Iadd_p.c
Contains column code for the agent DLL. Use when the object is called on a different processor / computer
Iadd_i.c
Contains interface ID (IID)
(Translator Note: The compilation method is that if your machine has installed the Visual Studio environment, then you can write directly from the command line to the following statement, midl.exe ../ addobj / ipd.idl)
The third step is inherited from IADD
We will create a COM object in this step. Create a new file (addObj.h), declare a C class, name this class as Caddobj, inherited from the interface IADD (in the file Iadd.h). Remember, IADD inherits from IunkNown, IUNKNOWN is an abstract base class. Therefore, we have to declare all methods as an abstract base class Iadd like IUNKNOWN. ///
//
//Addobj.h
// Contains the C Class Declarations for Implementing The Iadd
// Interfaces
//
#include "ipd.h"
EXTERN Long g_ncomobjsinuse;
Class CADDOBJ:
Public Iadd
{
PUBLIC:
// iUnknown interface
HRESULT __STDCALL QueryInterface
Refiid RIID,
void ** ppobj);
Ulong __stdcall addref ();
Ulong __stdcall release ();
// Iadd Interface
HRESULT __STDCALL SETFIRSTNUMBER (long NX1);
HRESULT __STDCALL SetSecondNumber (long NX2);
HRESULT __STDCALL DOTHEADDITION (long * pbuffer);
Private:
LONG M_NX1, M_NX2; // Operands for Addition
Long m_nrefcount; // for managing the reference country
}
///
COM Guide - Step By Step Com Tutorial - Next (Step By Step CoM Tutorial) - Under (2)