Three methods for compiling com client programs under VC ++ 6.0

Source: Internet
Author: User
Tags uuid

This article introduces three methods for compiling com client programs in VC ++ 6.0. Although each method can achieve the purpose of using code components, however, a detailed understanding of and understanding of all the methods will provide more room for selecting appropriate methods based on the actual situation.

Com library functions
---------------------------------------------------
Using COM library functions to use code components is the most troublesome and difficult method to implement among the three methods described. It requires developers to have a deep understanding of the principles of COM. The steps for implementing this method are as follows:

1. First 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.

2. Use the # include statement to include the reference to the component header file and create the component object. The header file contains the C ++ definition of the interface and the symbolic constant that describes the interface id iid and the class ID clsid. Create a job in the initialization dialog box function:

Iaccount paccount = NULL;

......

Cocreateinstance (clsid_account,

Null,

Clsctx_inproc_server,

Iid_iaccount,

Reinterpret_cast (& paccount ));

3. Finally, release the component object. This work should be completed before the program exits, for example, in the response function of message wm_close:

If (paccount! = NULL)

Paccount-> reallocate ();

To call other functions in the Code component, you can use the interface pointer paccount of the component object:

......

BSTR bstrresult;

Paccount-> post (100, bstrresult );

Sysfreestring (bstrresult );

......

Because the com Support class is defined in comdef. H, you must include a reference to this header file to make the program run normally.

---------------------------------------------------
Class Wizard
---------------------------------------------------
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.

First, add the code to initialize the COM component. We can 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.

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.

---------------------------------------------------
# Import command
---------------------------------------------------

# The import command method is very simple. 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.

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.