Three methods for using the com library in vc6

Source: Internet
Author: User

The framework used in this article is the mfc exe project in the dialog box mode. Before programming, you must first determine whether the code component to be operated has been registered in the system. If the code component is not registered, you can use the regsvr32. exe program under the Windows "system directory to register it.

1. Use the com library function 1. Include the com header file and the. c file defined by the interface.

The header file contains the C ++ definition of the interface. the C file describes the symbolic constants of the Interface id iid and class ID clsid. For example, if a com library name is "simpletest", it must contain the following files:

# Include "simpletest. H"

# Include "simpletest_ I .c"

2. Add the com initial and termination code.

Add the following code to the initialization instance function initinstance () of the application class:

Coinitialize (null );

......

Couninitialize ();

The preceding statement runs in the MFC framework or non-MFC framework. However, because the program in this article uses the MFC framework, you can also use the afxoleinit () function to initialize it.

3. Create a component object.

Hresult hr;

Isimpleinterface * pintf = NULL;

HR = cocreateinstance (clsid_simpleinterface, null, clsctx_server,

Iid_isimpleinterface, (void **) & pintf );

If (succeeded (HR ))

{

Pintf-> welcome ();

Pintf-> release ();

}

Ii. Use the Class Wizard to import the Type Library

You can use the Class Wizard to directly read the component type library and generate classes for each interface in the packaging type library. The member functions of these classes can access the methods and properties of the component interface, the method is similar to that of ActiveX control.

1. Add code to initialize the COM component.

Add the. TLB library file of the component through the from a type library in the Class Wizard and introduce its interface class. The Type Library File introduced in this example only contains an iaccount of the component packaging class derived from the coledispatchdriver. Through the members of the packaging class, you can understand what services the component interfaces can provide and access the methods and attributes of the component interfaces through them.

In the initialization dialog box, use the createdispatch () member function of the coledispatchdriver class to create an account component object:

Iaccount m_account;

......

M_account.createdispatch ("atlsample. account.1 "));

The progid value "atlsample. Account. 1" can be found through the OLE View tool in Microsoft Visual Studio Tools 6.0, provided that the component has been successfully registered.

You can also use the releasedispatch () function of the coledispatch-Driver Class to release the account component object.

2. Call Method

The post method used in the com library function method can be called using the following methods:

Cstring STR = m_account. Post (100 );

It can be seen that this method implements the same function, but it is easier to implement than the previous method, and the requirements for understanding com are not high.

For the example of the first point, you can call this method:

Coinitialize (null );

Isimpleinterface simple;

Simple. createdispatch ("simpletest. simpleinterface.1 ");

Simple. Welcome ();

Simple. releasedispatch ();

Couninitialize ();

Iii. Use # import command 1. Brief Introduction

This command is very suitable for type library files, because the path is fixed for type library files, whether it is the debug version or the release version. # The import command extracts two files from the type library to be introduced during execution: One. tlh file and. TLI file, the latter is only the function implementation of the packaging class, and the former contains a lot of important information. Smart interface pointers are also defined here:

_ Com_smartptr_typedef (iaccount ,__ uuidof (iaccount ));

During actual compilation, the compiler expands it into the following code and defines a smart pointer iaccountptr for the iaccount interface through the _ com_ptr_t template class. It is a smart pointer because it automatically processes cocreate-instance and all iunknow methods when replacing iaccount, which is very convenient to use:

Typedef _ com_ptr_t <_ com_iiid <iaccount ,__ uuidof (iaccount)> iaccountptr;

With smart pointers, we can call the createinstance () function of the _ com_ptr_t template class to create interface pointers:

Iaccountptr m_account = NULL;

M_account.createinstance (_ uuidof (account ));

Because. the tlh file contains the structure declaration and declspec (UUID ("") Declaration. Therefore, you can easily use _ uuidof (account) to obtain the guid of the interface. The declspec (UUID ("") Statement associates the guid with the class and each interface, allowing developers to use the uuidof operator to obtain the guid of the class and interface.

Note: To prevent name conflicts between the original code and the newly introduced code, the compiler defines a namespace identified by the Type Library name, and add an identifier to any name declared in it. To avoid specifying the namespace identifier, you can add using namespace after the # Import Statement, and use rename_namespace to change the namespace. For example, you can perform the following operations in this example:

# Import "account. TLB" rename_namespace ("accountdriver ")

Using namespace accountdriver;

In this way, you only need to define the Smart interface pointer iaccountptr:

Iaccountptr m_account;

The call to functions and attributes in the Code component is the same as that in the first two methods, and the access is completed through m_account. Because of the introduction of the _ com_ptr_t template class and smart pointer, the # import command method is the simplest method to use the COM component among the three methods.

2. Usage

Add the following sentence at the end of the stdafx. h file:

# Import "simpletest. TLB" no_namespace

Isimpleinterfaceptr * m_account = new isimpleinterfaceptr;

M_account-> createinstance (_ uuidof (simpleinterface ));

M_account-> interface. Welcome ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.