COM Guide (Step By Step CoM Tutorial) - Next (2)

zhaozj2021-02-16  61

Chapter 10 DllRegisterServer and UnregisterServer

The location information of the COM object will enter in the registry. This job can be done outside through a .reg file, or use an output function DllRegisterServer. To remove the content of the registry, we will use another output function DllunregisterServer. The implementation of these two functions is in the file registry.cpp. A simple tool REGSRV32.EXE can also be used to load a specified DLL and perform DllRegisterServer / DllunregisterServer.

To make the connector output these two functions, we will create a module definition file (exports.def)

;

Contains the list of functions That Are Being Exported from this DLL

;

Description "Simple COM Object"

Exports

DllgetClassObject Private

DllcanunloadNow Private

DllregisterServer Private

DllunregisterServer Private

First step

Let's give finally completed our adoBJ Win32 DLL project. Insert the file Iadd.idl into the engineering workspace.

Set the custom compilation option for this file. (As shown below)

In order to execute Regsrv32.exe after each compilation, insert a command string in the "POST-BULID STEP" dialog box:

Compiled this DLL. Insert the IDL file into the project is to reduce each file after editing, you must perform external compilation. Every time we successfully compile our project, this COM object will be registered.

Step 12 In Visual Basic, use COM just created objects

To use the Addobj COM object from Visual Basic, create a simple EXE project to add the following code. Determine a reference to an object of the Iadd.TLB template library has been added.

DIM Iadd as codegurumathlib.iadd

Set Iadd = CreateObject ("CodeGuru.fastAddition")

Iadd.setfirstnumber 100

Iadd.setsecondNumber 200

Msgbox "total =" & iadd.dotheaddition ()

Step 13 Analyze all the documents we just created

The following is the document we have used

Iadd.idl

Interface definition file content

Addobj.h

Contains CADDOBJ declaration

AddobjFactory.h

Contains CADDFactory declaration

Addobj.cpp

Contains CADDOBJ implementation

Addobj.cpp

Contains CADDFactory implementation

Exports.cpp

Realization of DllgetClassObject, DllcanunloadNow & DllMain

Registry.cpp

Contains R dllregisterServer, DLLunRegisterServer implementation

Addobjguid.h

GUID value containing our AddObj COM object

Step 14 in ActiveX DLL embedded type library

Together with Addobj.dll, the type template library can also be released. For a simple process, the type template library Iadd.tlb can be embedded in the addobj.dll file as a binary resource. In the future, just release this DLL file addobj.dll to the customer.

The fifteenth step from the Visual C client uses the COM object you just created

Through any of the following ways, Visual C customers can use this COM interface 1, # import "add.tlb"

2, Iadd.h header file. In this case, this DLL provider must release Iadd.h with the DLL file.

3, use some tools to creating C code (for example, MFC Wizard)

In the first case, the compiler creates some intermediate files (.tlh ,.tli), which contains an extended interface declaration. In addition, the compiler can also declare the Bulit Around The Raw Interfaces smart pointer class. For COM programmers, the intelligent pointer class makes the survival period easier (control) by appropriate management of the SM object.

In the example below, # import to enter the addobj.dll file instead of Iadd.tlb because we put the TLB file in the DLL (the translator's note, the type library mentioned earlier as a binary resource in DLL resources) . In addition, # import should also use the TLB file.

In Visual C , create a console EXE project. Type (or copy) the following content and compile.

//

///Client.cpp

//

// demo of client using the com object from addobj.dll

//

#include

#include

#import "addobj.dll"

//

// Here We do a #import on the dll, You Can Also Do a #import on the .tlb

// The #import directive generates two files (.tlh / .tli) in The Output Folders.

//

void main ()

{

Long N1 = 100, N2 = 200;

Long noutput = 0;

Coinitialize (NULL);

CodeGurumathlib :: Iaddptr pfastaddalgorithm;

//

// Iaddptr is not the actual interface Iadd, But a template C Class (_COM_PTR_T)

// That Contains an Embedded Instance of the Raw Iadd Pointer

// While Destructing, The Destructor Makes Sure To Invoke Release () on the intence

// Raw Interface Pointer. Further, The Operator -> HAS Been Overloaded to Direct All

// Method Invocations to the Internal Raw Interface Pointer.

//

Pfastaddalgorithm.createInstance ("CodeGuru.fastAddition");

Pfastaddalgorithm-> setfirstnumber (n1); // "->" overloading in action

Pfastaddalgorithm-> setsecondnumber (N2);

NOUTPUT = Pfastaddalgorithm-> Dotheaddition ();

Printf ("/ NOUTPUT AFTER ADDING% D &% D IS% D / N", N1, N2, NOUTPUT;

}

转载请注明原文地址:https://www.9cbs.com/read-27742.html

New Post(0)