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

xiaoxiao2021-03-06  37

Visual C calls Visual Basic Components

Write the steps:

n Use Visual C to create a dialog-based application, the engine name is VCTestVBComproj, just like the above VBTestVBComproj project, placed two text boxes and a certaintable button on the dialog.

N Import the type library of the server in the client. The type library of importing the server will allow you to use server methods and properties in our customers.

Now, in order to access the server, its type library should be imported into the customer's workspace. This is equivalent to setting component references from accessing COM components in the Visual Basic environment. In MFC or C , this is done using a #import statement.

#import keyword creates a package with a package with extension TLH (Type library header file) and implements it in another file with extension TLI (Type Library implementation file). These files are displayed in the customer's workspace output directory, usually under the debug directory of the current workspace.

The package class is a class created by the #import statement, which encapsulates server information. It uses to access the client and method defined in the server. It shows a zone in the TLH file in front with keyword Namespace. The name of the packaging class is the server space name inserted into the keyword lib. If your server component name is Server, its name is ServerLib. Note: Here is a component created for Visual C , not the components created by Visual Basic.

2-1 Add code in stdafx.h file:

// stdafx.h: include File for Standard System Include Files,

// or project specific include files That Are used frequently, but

// area Changed Infrequently

//

#if! defined (AFX_STDAFX_H__A07C8507_082B_4B60_9880_AEB5ADB6D68A__INCLUDED_)

#define AFX_STDAFX_H__A07C8507_082B_4B60_9880_AEB5ADB6D68A__INCLUDED_

#iF _MSC_VER> 1000

#pragma overce

#ENDIF / / 100 m _ _ _

#define vc_extralean // Exclude Rarely-usest Stuff from Windows Headers

#include // MFC Core and Standard Components

#include // mfc extensions

#include // MFC Automation Classes

#include // MFC Support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include // MFC Support for Windows Common Controls

#ENDIF / / _AFX_NO_AFXCMN_SUPPORT

#import "../vbcom/validatecardserver.dll"// If your server component is created with Visual C , then you should import type library files with a .tlb extension instead .dll file. Specific example see developing COM Components Using VC-ATL (1) // {{AFX_INSERT_LOCATION}}

// Microsoft Visual C Will Insert Additional Declarations Immediate Line.

#ndif //! defined (AFX_STDAFX_H__A07C8507_082B_4B60_9880_AEB5ADB6D68A__INCLUDED_)

2-2 Compile STDAFX.CPP, when the CIMPORT statement structure is completed, the package generates the packaging class. This package class will encapsulate the functionality of the server components, which can be accessed by the customer. This information will be created in the directory in the customer engineering space: Type Liber File (TLH), and Type Library implementation file (TLI). The name of the packaging class will be the default server name. So, in this example, its name will be ValidateCardServer.

If the server component is created with Visual C , then the name of the package class will be the upper-written server name to keep up with lib. If the server component created by Visual C has a name ValidateCardServer, the name of the packaging class will be ValidateCardServerLib.

2-3 Now you have to access the functionality of the server components from the dialog. To do this, add the following statement in the vctestvbcomprojdlg.cpp file:

// vctestvbcomprojdlg.cpp: Implementation File

//

#include "stdafx.h"

#include "vctestvbcomproj.h"

#include "vctestvbcomprojdlg.h"

#ifdef _Debug

#define new debug_new

#undef this_file

Static char this_file [] = __file__;

#ENDIF

Using Namespace ValidateCardServer;

/

// Caboutdlg Dialog Used for App About

Class Caboutdlg: Public CDIALOG

{

PUBLIC:

Caboutdlg ();

...

n Initialize the COM library. The application must initialize the COM library before calling the COM function.

Because the interaction between customers and components is through the COM library, the COM library must be initialized. In order to initialize the COM library, you can call the Coinitialize function. The return value of the Coinitialize function is the HRESULT data type, which suggests that the operation is successful or failed. The return value can be S_OK or S_FALSE.

grammar:

HRESULT COINIALIZE (NULL);

n Get the CLSID of the server. The client application can only instantiate the server after the CLSID of the COM server component is obtained.

In order to create an instance of the server, the server's CLSID must be obtained. Because COM's CLSID is hard to remember, you must use readable ProgID to get CLSID. This is done by calling the CLSIDFROMPROGID function. This function comes with two parameters: input parameter progid and output parameter CLSID.

grammar:

HRESULT CLSIDFROGID (LPCOLESTR LPSZPROGID, LPCLSID PCLSID);

n An instance of Creating a COM server component. Create an instance of a COM server component and return a pointer to the desired interface. Once the component's CLSID is obtained, it can be used to create an instance of the COM server. Use the CoCreateInstance function to create an instance of the server component. COM will not provide a pointer to the object, COM only provides a pointer to the interface.

grammar:

HRESULT COCREATEINSTANCE

Refclsid rclsid, // To create a CLSID of the component

LPUNKNOWN PUNKOUTER, // Used when the object supports aggregation, otherwise null

DWORD DWCLSCONTEXT, // Object Guanyuan Environment (within the process, outside the process or remote)

Refiid riid, // To get the ID of the interface

LPVOID * PPV // [OUT] Pointer

);

1. The first parameter of the COCREATEINSTANCE function is the CLSID of the instantiation component, which is obtained through the CLSIDFROMPROGID function. In order to find the components you need, the CoCreateInstance function uses it in the HKEY_CLASSES_ROOT / CLSID section in the registry. If no match is found, CocreateInstance failed.

2. The second parameter of the COCREATEINSTANCE function is used to indicate whether you create an object as part of the aggregate object. Since the program does not use aggregation, use NULL.

3. The third parameter of the COCREATEINSTANCE function is used to indicate the environment where components are running. In COM, you can create three types of components: internal, processes and remote. The table below shows different environment variables.

Class environment

Description

CLSCTX_INPROC_SERVER

Indicate the server.

CLSCTX_INPROC_HANDLER

Specifically specify the specific type of the server within the process, running in the customer process, but the instance of the class is remotely accessed.

CLSCTX_LOCAL_SERVER

Indicates local (processes) servers.

CLSCTX_REMOTE_SERVER

Indicates the remote server and uses the CoCreateInstanceex function.

CLSCTX_SERVER

Indicates if it is not a specific component type. This value retrieves the process within the process, the process, the first variable in the remote.

4. The fourth parameter of the COCREATEINSTANCE function indicates the interface identifier IID that needs to interface. The interface ID is returned by the __UUIDOF function. The interface pointer returns in the fifth parameter.

5. Output parameters to the specified interface pointer.

n Use a COM object. After getting the interface pointer, the client application can call the method and properties of the COM server object.

The COCREATEINSTANCE function returns an interface pointer for a component. Use this pointer to access the properties and methods of the server.

n Terminate the COM library. After release the COM object, the client application should indicate that the COM library that is no longer needed.

After the instance of the Com object is released, the initialized COM library is no longer needed. In order to release the initialized COM library, the function Couninitialize can be invigured.

grammar:

Void Couninitialize ();

N with complete code

Void cvctestvbcomprojdlg :: onbutton1 ()

{

// Todo: Add Your Control Notification Handler Code Here

HRESULT HR = Coinitialize (NULL); // Step 3

CLSID CLSID;

HR = CLSIDFROMPROGID (Olestr ("ValidateCardServer.iValidateCard"), & clsid); // Step 4

_IValidatecard * cardptr; hr = cocreateInstance (CLSID, NULL, CLSCTX_INPROC_SERVER, __ uuidof (_iValidatecard), (lpvoid *) & cardptr);

IF (Failed (HR))

{

AfxMessageBox ("Server Creation Failed");

Return;

} // Step 5

Char ccardnum [20];

Getdlgitemtext (IDC_EDit1, ccardnum, 20);

Char ccardtype [10];

Getdlgitemtext (IDC_EDit2, ccardtype, 10);

_BSTR_T BSTCARDNUM (CCardNum), BstcardType (ccardtype);

Long Lstatus;

Lstatus = cardptr-> fnvalidatecreditcard (bstcardnum, bstcardtype);

IF (LSTATUS == - 1)

AfxMessageBox ("Valid Card");

Else

AFXMessageBox ("Invalid Card"); // STEP 6

Couninitialize (); // step 7

}

Author Blog:

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

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

New Post(0)