Developing COM Components Using VC-ATL (3-3)

xiaoxiao2021-03-06  39

N Write Visual C Components

Determine the most effective tools for creating server components

When you start create a COM component, you can choose different tools with Visual Basic, Delphi, MFC, and ATL. Let us compare each tool to expose their respective advantages and disadvantages.

When you write a COM class, you can have several options, you can choose to meet COM with C . This option provides the greatest flexibility but is very boring. In addition, you can choose two main frames provided by Visual C to write COM: MFC and ATL.

MFC uses a document view architecture to design a well-developed Windows-based application. Use the MFC design based on Windows applications to request runtime library MFC42.dll. When used to develop an identified application, the MFC encounters the requirements for large run time library overhead. The MFC is helpful in the development of a number of GUI client applications, ActiveX controls, and OLE documents, like Windows-based single-layer applications. Support for COM is later increasing and relying on its document view architecture. Although the Visual C wizard makes it easy to create components with MFC, it is not a flexible approach to the congenital design of the Frame structure. Moreover, the assembly ends in volume and requires expansion DLL.

Instead, ATL is designed for COM. In addition, ATL provides full support of synthesis techniques like polymerization and tolerance. ATL uses multi-inheritance techniques of C templates to implement COM classes. ATL allows COM components, automation servers, and ActiveX controls to create. COM created with ATL is smaller and faster. ATL offers local support for many COM interfaces and new thread models supported by Windows NT Servers. Since ATL mainly uses the concept of templates, the created component is small, and the runtime DLL is small and dynamically loaded.

So, you create a COM using ATL.

Component Function: Same as above (written Visual Basic Components).

Components will contain the following functions: Validatecreditcard, Prefixcheck, Lengthcheck, Luhncheck. Only validatecreditcard can be called to encapsulate components work.

To create a server component for verifying a credit card, you need to perform the following steps:

1. Create an application framework to launch VC 6.0, select the New menu item from the File menu option, select the Projects card in the New dialog box, select the ATL COM AppWizard from the displayed engineering type. Specify the project's work space name, that is, enter the project name ValidatecreditCardServer in the Project Name Edit box to distinguish between ValidateCardServer in Visual Basic Components. After selecting the appropriate location, press the confirmation button to enter the next dialog: ATL COM AppWizard - Step 1 of 1, select Dynamic Link Library [DLL] in Server Type, the server components in the process. Check the Support MFC option. After pressing the finish and ok buttons, the framework of a component has been established. Let us now look at the list of generated files.

n stdafx.h

n stdafx.cpp

n validatecreditcardServer.idl

n validatecreditcardserver.rc

n resource.h

n validatecreditcardserver.def

n validatecreditcardserverps.defn validatecreditcardServer.h

n validatecreditcardServer.cpp

n validatecreditcardServer.dsp

n validatecreditcardServer.dsw

n validatecreditcardServer.ncb

n validatecreditcardserverps.mk

n validatecreditcardServer.clw

ValidatecreditcardServer.cpp list

// ValidateCreditCardServer.cpp: Implementation of DLL Exports.

// Note: Proxy / Stub Information

// TO Build A Separate Proxy / Stub DLL,

// Run nmake -f validatecreditcardserverps.mk in the project directory.

#include "stdafx.h"

#include "resource.h"

#include

#include "validatecreditcardserver.h"

#include "validatecreditcardser_i.c"

#include "validatecard.h"

CCOMMODULE _MODULE;

Begin_Object_map (ObjectMap)

Object_entry (CLSID_VALIDATECARD, CVALIDATECARD)

END_OBJECT_MAP ()

Class CvalidatecreditcardSerVardSerVardSerVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERAPP: Public CWINAPP

{

PUBLIC:

// Overrides

// ClassWizard Generated Virtual Function Overrides

// {{AFX_VIRTUAL (CVALIDATECREDITCARDSERVERVERAP)

PUBLIC:

Virtual Bool InitInstance ();

Virtual int exitInstance ();

//}} AFX_VIRTUAL

// {{AFX_MSG (CVALIDATECREDITCARDSERVERVERAPP)

// 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 (CVALIDATECREDITCARDSERVERVERVERAPPPPPPP, CWINAPP)

// {{AFX_MSG_MAP (CVALIDATECREDITCARDSERVERVERAP)

// 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 ()

CVALIDATECREDITCARDSERVERVERVERVERVAPP THEAPP;

Bool CvalidatecreditcardSerVardSerVERVERVERVERVERVERVERVERVERVERAPP :: InitInstance () {

_Module.init (Objectmap, M_HINSTANCE, & LIBID_VALIDATECREDITSERVERVERLIB);

Return CWINAPP :: InitInstance ();

}

Int CvalidatecreditcardSerVERVERVERVERVERVERVERVERVERVERVERVERVERVERVERAPP :: 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);

}

2. Creating the components in the framework Select the New ATL Object ... menu item from the Insert menu, the ATL Object Wizard dialog box appears.

In ATL Object Wizard, select Objects in the Category on the left, select the Simple Object item in the Objects on the right. Press the Next button.

In the SHORT NAME in the top of the eight editing boxes in the ATL Object Wizard property dialog box, enter the short name ValidateCard, and the contents of the other seven edit boxes are automatically generated. Here we also analyze the content of the other seven edit boxes of this dialog:

C block

Short name: validatecard // User Enter

Class: CVALIDATECARD / / Class name of the implementation class of the component class

.H file: validatecard.h // Generate the header file of the implementation class of the component class

.Cpp file: validatecard.cpp // Generate implementation files for implementation classes

COM block

Coclass: ValidateCard // The component class is generated, distinguishes the implementation class of the component class

Interface: When you create a component class, create a user interface for this component class.

TYPE: ValidateCard class // Component class PROGID: ValidatecreditCardServer.validateCard // ClsidFromProgID Call, with it to get CLSID

Then press the confirmation button to exit. This will generate as above and do one login in the same manner, especially the IDL file. Here we compare the two versions of the two versions of the component before and after.

Create the validatecreditcardserver.idl file before the component, which creates the IDL file after the application framework.

// validatecreditcardser.idl: idl source for validatecreditcardServer.dll

//

// this file will be processed by the Mid Tool To

// produted the Type Library (ValidatecreditcardServer.tlb) and Marshalling Code.

Import "OAIDL.IDL";

Import "OCIDL.IDL";

[

UUID (B3404054-C33E-4931-B737-5783CEEFECDB),

Version (1.0),

Helpstring ("ValidatecreditcardServer 1.0 Type Library")

]

Library ValidatecreditcardServerLib

{

Importlib ("stdole32.tlb");

Importlib ("stdole2.tlb");

}

(Space limit, endless, to be continued)

Author Blog:

http://blog.9cbs.net/callzjy/

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

New Post(0)