Design and application of "reprint" COM component (vi)--write the first component in ATL

Source: Internet
Author: User

Original: http://vckbase.com/index.php/wv/1216.html

First, preface

1, and "COM Component design and application (v)" The content is basically consistent. But this is explained in Vc.net 2003 under the use of the method, even if you no longer use vc6.0, also please and the previous content, reference.

2, this first component, in addition to all the COM components must be IUnknown interface, we implement a self-defined interface Ifun, it has two functions: Add () to complete the addition of two numbers, Cat () Complete the connection of two strings.

3. Below ... Listen carefully! Started:-).

Second, building an ATL project

Step 2.1: Build a solution.

Step 2.2: In this solution, create a new VC + + ATL project. The example program is called Simple2, and select the DLL mode, see figure I, figure two.

Figure I, New ATL project

Figure Two, select the non-attributed DLL component type

attributed attributed programming is the direction of the future, but let's not choose it now.

The dynamic-link library (DLL) selects it.

executable (EXE) to speak later.

The Service (EXE) represents a program that establishes a system service component program that is loaded and executed after the system is started.

Allow merge proxy/stub code to select this option to merge the "proxy/stub" code into a component program, otherwise it needs to be compiled separately to register the proxy stub program separately. Agent/stub, what is this concept? Do you remember what we introduced in the last book? When the caller invokes out-of-process or remote component functionality, the proxy/stub is actually responsible for data exchange. About the agent/stub of the specific turn and operation, and then again ...

Support MFC Unless there is a special reason, we write the ATL program, it is best not to select the item. You might say, if there is no MFC support, what about the CString? Tell you a secret, the average person I do not tell him, I in the second half of my life by this secret alive:

1, you will STL it? Can be replaced with a string in STL;

2, I write a MyString class, hehe;

3, quietly, secretly, do not tell others (especially do not tell Microsoft), the MFC in the CString source to take over;

4, use CComBSTR class, at least can simplify our string operation;

5, directly with the API operation string, anyway, we all learn C language, are from here to do. (equal to not say, hehe)

COM + features that support transaction processing are supported by COM + 1.0 . COM + may introduce you in the 99th.

Iii. Adding an ATL object class

Step 3.1: Menu "project \ Add Class ..." (or right-click the popup menu in the project "add \ Add Class ...") and select the ATL simple object. See figure Three.

Figure III, choosing to build an ATL simple Object

In addition to simple objects (only the IUnknown interface is implemented), you can also select "ATL Control" (ActiveX, implementing more than 10 interfaces) ... There are many types of component objects that can be selected, but essentially, the wizard helps us to add some interfaces by default. In a future article, introduce it in succession.

Step 3.2: Increase the custom class Cfun (interface Ifun) as shown in Figure four.

Figure IV, fill in the name

In fact, we only need to enter the short name, other items will be automatically filled out. There's nothing to say, just take a look at the ProgID entry, the default ProgID is constructed as "project name. Short name".

Step 3.3: Fill in the interface properties options, as shown in Figure five.

Figure V, interface options

Threading Model The thread in COM, I think is the most annoying, the most complicated part. The concept of COM threads and Apartments is reserved for follow-up introductions. Now ... Everybody chose "unit" (Apartment), what does it represent? Simply put: These calls are queued when component functions are called in the thread. Therefore, in this mode, we can temporarily not consider the problem of synchronization. (Note 1)

interface . Double (Dual), this is very very important, very very common, but we do not speak today (note 2). Remember! Remember! In this first COM program of ours, be sure to select "Custom"!!!! (If you've chosen the wrong one, please delete the entire content and try again.) )

Aggregate The components we write and whether they will be allowed to be aggregated (note 3) in the future. "Can only be created as aggregations", a bit like a pure virtual class in C + +, if you are the chief Engineer, only responsible for design but do not write the code yourself, select it.

ISupportErrorInfo whether to support rich information error handling interface. Talk about it later.

Connection Whether the point supports connection point interfaces (events, callbacks). Talk about it later.

Whether the IObjectWithSite supports IE calls

Iv. Adding interface functions

Figure VI, bring up the menu to increase the interface method

Figure VII, adding an interface function add

Add the Add () function as shown, adding the cat () function. [In] indicates that the parameter direction is input; [out] indicates that the parameter direction is output; [Out,retval] indicates that the parameter direction is output and can be used as the return value of the result of the function operation. In a function, there can be multiple [in] and [out], but [retval] can have only one and be combined with [out] at the last position. (Note 4)

Figure VIII, the diagram after the completion of the interface function definition

As we all know, to change the class function in C + +, we need to modify two places: one is the function declaration of the class in the header file (. h), and the second is the implementation of the function body (. cpp) file. While we are now using ATL to write component programs, we also need to modify one place, which is the interface definition (IDL) file. Don't worry. IDL will be discussed next time.

V. Implement interface functions

Mouse double dot figure eight cfun\ base and Interface \add (...) You can begin the input function implementation:

STDMETHODIMP Cfun::add (Long N1, long N2, long *pval) {*pval = n1 + N2;return S_OK;}

  

This is too simple to waste "tongue". Below we implement the cat () function of the string connection:

STDMETHODIMP Cfun::cat (BSTR S1, BSTR S2, BSTR *pval) {int nLen1 =:: Sysstringlen (S1);//s1 character length int nLen2 =:: Sysstringle  n (S2);//s2 character Length *pval =:: SysAllocStringLen (S1, nLen1 + nLen2);//Construct a new BSTR and save the S1 first if (nLen2) {:: memcpy (*pval + NLEN1, S2, nLen2 * sizeof (WCHAR));//Then connect S2 to//wcscat (*pval, S2);} return S_OK;}

  

Student: The above function implementation is done entirely by invoking the basic API method.

Teacher: Yes, to tell the truth, it is quite cumbersome indeed.

Student: We are using memcpy () to complete the second string function, so why not use the function wcscat ()?

Teacher: In most cases, but you need to know: Because a BSTR contains a string length, the actual BSTR string content can be stored in the "L", and the function Wcscat () is the "L" as the end of the copy flag, so the data may be lost. Do you understand?

Student: Understand, understand. I've seen the data types of COM component design and application (c). So, teacher, is there a simple way to do that?

Teacher: Yes, you see ...

STDMETHODIMP Cfun::cat (BSTR S1, BSTR S2, BSTR *pval) {CComBSTR sresult (S1); Sresult.appendbstr (s2); *pval = Sresult.copy ( );//*pval = Sresult.detach (); return S_OK;}

  

Student: Haha, good! With CComBSTR, this is a lot easier. What is the difference between ccombstr::copy () and CComBSTR::D Etach ()?

Teacher: Ccombstr::copy () Creates a copy of a BSTR, and Ccombstr::copyto () has a similar function. and CComBSTR::D Etach () is to peel the object with the internal BSTR pointer, which is a little bit faster because there is no copy process. Be aware, however, that once stripped, you can no longer use the object.

Student: Teacher, you speak too cow, I admire you like the towering Taishan, straight into the sky ...

Teacher: stop,stop! Leave homework ...

1. Write this component first according to the content of today's speech;

2, whether you understand or do not understand, must go to observe IDL files, CPP files;

3, after compiling, see what kind of files are produced? If it is a text file, open it to look;

4. Download the sample program (vc.net 2003 version) of this article to compile and run to see the effect. Then preview the calling method in the sample program;

Student: I know, I want to go to the bathroom, I have to pee.

Teacher: Class dismissed! Don't forget to top my post ...

Vi. Summary

This article describes the steps to establish the first ATL component program, and how to use it, so stay tuned for COM component design and application (vii).

Note 1:apartment, the System queues component calls through hidden window messages, so we can temporarily leave out synchronization issues. Note that it is temporary ha.

NOTE 2: The dual interface means that both the custom interface and the IDispatch interface are supported in one interface. Later, later, will speak. Because the dual interface is very important, we will speak everyday, every night, often speaking------abbreviated as "three talk":)

Note 3: There are 2 ways to reuse a component, aggregation and containment.

Note 4: These are the concepts in the IDL file, and what will be introduced later.

Design and application of "reprint" COM component (vi)--write the first component in ATL

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.