COM component design and application 6 - Write the first component with ATL

xiaoxiao2021-03-18  232

Download Source Codes First, Foreword 1, and "COM component design and application (5)" basically consistent. But this time is in the use of the use of VC.NET 2003, even if you no longer use VC6.0, please refer to the previous content, refer to the comparison. 2, the first component, in addition to all the IUnknown interfaces that all COM components must have, we reach a defined interface IFUN, which has two functions: add () completed two values, Cat () completed two String connection. 3, below ... I listen! Start :-)

Second, establish an ATL Project Step 2.1: Establish a solution. Step 2.2: In this solution, create a VC ATL project. The sample program is simple2 and selects the DLL mode, see Figure 1, Figure 2. Figure 1. New ATL Project Figure 2, select non-personalized DLL component type property property property programming, is the direction of the future, but we don't want to choose it now. Dynamic Link Library (DLL) Select it. The executable file (exe) will be explained later. Service (EXE) represents the creation of a system service component program that will load and execute after the system starts. Allow the consolidation agent / stub (STUB) code Select this item to merge the "agent / stub" code into the component program, otherwise you need to be compiled separately, register the agent stub program separately. Agent / stub, what is this concept? Remember that we introduced it in the book? When the migrant calls the process or the remote component function, it is actually responsible for data exchange. Regarding the specific turn and operation of the agent / stub, it will be said later ... Support MFC unless there is special reason, we write the ATL program, it is best not to choose this. You may say, if there is no support for MFC, what should the cstring do? Tell you a secret, I don't tell him about the average person, I will live in this secret: 1, will you STL? You can use String in STL instead; 2, write a mystring class yourself, 嘿嘿; 3, quietly, secretly, don't tell others (especially don't tell Microsoft), take the CString source code in the MFC; 4, use CCOMBSTR class, at least simplify our string operation; 5, directly use the API operation string, anyway, when we learn C language, it is dry from here. (It is equal to not saying, huh, huh), supports the COM function supporting transaction processing. COM may introduce it in Chapter 99. Third, add the ATL object class step 3.1: Menu "Project / Add Category ..." (or right mouse button "Add / Add Category ..." and select the ATL simple object. See Figure 3. Figure 3, selecting an ATL simple object In addition to simple objects (IUNKNOWN interface), you can also select "ATL control" (ActiveX, more than 10 interfaces) ... You can choose a lot of components objects, But in essence, let the wizard help us with some interfaces. In the later articles, let's introduce it. Step 3.2: Increase the custom class CFUN (interface IFUN), see Figure 4. Figure 4. Fill in the name is actually, we only need to enter the referred to, and other projects will be filled out. Nothing to say, just please pay attention to the ProgID item, the default PROGID constructor is "project name. Referred to". Step 3.3: Fill in the interface attribute option, see Figure 5. Figure 5. Interface Options Thread Model CoM thread, I think it is the most annoying, most complex part. The concept of COM threads and apartments, leaving later introductions. Now ... Everyone chooses "Apartment", what does it represent? Simply put: When the component function is called in the thread, these calls will be queued. Therefore, in this mode, we can temporarily do not consider the problem of synchronization. (Note 1) Interface.

Double (Dual), this is very important, very common, but we don't talk today (Note 2). Remember! Remember! In our first COM program, you must choose "Custom"! ! ! ! (If you choose the wrong, please delete all the content, recall.) Aggregate the components we write, whether it is allowed to be used by others (Note 3). "I can only create it as a polymerization", it is a bit similar to the pure category in C , if you are a general engineer, if you are only responsible for design but if you don't write code, you choose it. Is iSupp PorterrorInfo supports rich information error handling interfaces. In the future. Whether the connection point supports the connection point interface (event, callback). In the future. Do IObjectWithSite supports IE call four, add interface function map 6. Tune the menu diagram of the increased interface method. Increase the interface function add, add the add () function, add a CAT () function. [in] means the parameter direction is input; [OUT] means the parameter direction is output; [OUT, RETVAL] indicates that the parameter direction is output, and can be used as the return value of the function operation result. In a function, there can be multiple [in], [out], but [RETVAL] can only be one, and the [OUT] is combined in the last position. (Note 4) Figure 8. During the completion of the interface function definition, we all know that if you want to change the class functions in C , you need to modify the two places: First, the function declaration of the header file (.h), the other The implementation of the function body (.CPP) file. And now use ATL write component programs, you have to modify a place, which is the interface definition (IDL) file. Don't worry about the next time you have next time. 5. Implementing the interface function mouse two-point Figure 8 CFUN / CMS and interface / add (...) You can start the input function is implemented: StdMethodimp CFUN :: Add (long N1, long n2, long * pval)

{

* pval = n1 n2;

Return S_OK;

} This is too simple, no longer wasting "mouth". Below we implements a string CAT () function:

STDMETHODIMP CFUN :: Cat (BSTR S1, BSTR S2, BSTR * PVAL)

{

INT Nlen1 = :: SYSSTRINGLEN (S1); // S1 character length

INT Nlen2 = :: SYSSTRINGLEN (S2); // s2 character length

* pval = :: sysallocstringlen (S1, Nlen1 Nlen2); // Construct a new BSTR while saving S1 first

IF (Nlen2)

{

:: Memcpy (* PVAL NLEN1, S2, NLEN2 * SIZEOF (Wchar)); // then connect S2 to

// WCSCAT (* PVAL, S2);

}

Return S_OK;

} Students: The above function is implemented, which is completely called the basic API mode.

Teacher: Yes, telling the truth, it is indeed cumbersome.

Student: We use memcpy () to complete the second string function, then why don't you use a function wcscat ()?

Teacher: In most cases, you need to know: Since the BSTR contains a string length, the actual BSTR string can store L '' '/ 0' ', and the function WCSCAT () is L' '.' / 0 '' As a replication end sign, there may be data. do you understand? Student: understand, understand. I have seen it

"COM component design and application (3) data types are understood. So teacher, is there a simple way?

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! Using CCOMBSTR, this is much simpler. CCOMBSTR :: Copy () and ccombstr :: DETACH () What is the difference?

Teacher: ccombstr :: Copy () will make a copy of a BSTR, and CCOMBSTR :: CopyTo () has similar functions. CCOMBSTR: :: DETACH () is to peel the object with the internal BSTR pointer, this function is slightly fast because there is no replication process. But pay attention to, after it is stripped, you can't use this object anymore.

Student: Teacher, you talk about too bad, I am in the hills, straight into the cloud ...

Teacher: STOP, STOP! Keep your homework ...

1. Write this component according to the content of today;

2, no matter what you don't understand, you must go to observe the IDL file, CPP file;

3, after compiling, what documents have you produced? If it is a file file, open it;

4. Download the sample program (VC.NET 2003 version) compiled and look at the effect. Then prepare the call method in the sample program;

Student: I know, let's go down, I have to go to the toilet, I can't do it ...

Teacher: Less lesson! Don't forget the top my post ...

Sixth, small knot

This paper introduces the establishment steps of the first ATL component program, and how to use this component, please pay attention to "COM component design and application (7)".

Note 1: Apartment, the system queues component calls by hidden window messages, so we can temporarily do not consider synchronization problems. Note that it is temporary.

Note 2: Dual interface is represented in an interface while supporting a custom interface and iDispatch interface. In the future, later, later. Because the double interface is very important, we will talk every day, night and night, often talk ------ referred to as "three talks" :)

Note 3: There are 2 reuse methods for components, aggregation, and inclusive.

Note 4: These are the concepts in the IDL file. What is used in the future, what is introduced?

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

New Post(0)