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

xiaoxiao2021-03-18  229

Download source code 1, foreword 1, if you are using VC5.0 and previous versions, please upgrade to vc6.0 or vc.net 2003; 2, if you are using VC6.0 (ATL 3.0) Please read this back Content; Implement a defined interface ifun, it has two functions: add () completes two values, CAT () completes two strings. 5, below ... I have listened! Start :-)

2. Establish an ATL Project Step 2.1: Create a work area (Workspace). Step 2.2: In the workspace, establish an ATL project (Project). The sample program is Simple1 and selects a DLL method. See Figure I. Figure 1. Establish an ATL DLL project Dynamic Link Library (DLL) represents a component program that creates a DLL. Executable (exe) represents a component program that creates an EXE. Service (exe) represents a program that creates a service, and then loads and executes the program. Allow Merging Of Proxy / Stub Code Select this item to merge the "Agent / Store" code into the component program, otherwise you need to be compiled separately, register the agent stub. 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. About the specific turn and operation of the agent / stub, later said ... Support MFC unless there is a 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. (Is not said, huh, huh) Support MTS supports transaction processing, that is, whether COM function is supported. COM may introduce it in Chapter 99. Third, add ATL object class steps 3.1: Menu INSERT / New ATL Object ... (or right click in the ClassView card pop up menu) and select the Object classification, select the Simple Object project. See Figure II. Figure 2, select the establishment of a simple COM object Category Object normal component. Among them, there are many components object types, but in essence, let the wizard help us with some interfaces. For example, we select "Simple Object", the wizard is given to our components plus the iUnknown interface; we choose "Internet Explorer Object", the wizard adds an IOBJECTWITHSITE interface to IE to add IE. Of course, we can do any interface manually. Category Controls ActiveX control. Among them, there are many ActiveX types. We discussed in ActiveX programming in subsequent specialization. Category Miscellaneous assisted miscellaneous components. Categroy Data Access database class (I hate the database programming, so I will not). Step 3.2: Increase the custom class CFUN (interface ifun), see Figure 3. Figure 3. The name in the input class is actually, we only need to enter a short name, and other items will be filled out.

Nothing to say, just please pay attention to the Progid item, the default PROGID constructor is "engineering name. Short name". Step 3.3: Fill in the interface properties, see Figure 4. Figure 4, Interface Properties Threading Model Selecting the Thread Model Supported by Components. Threads in COM, I think it is the most annoying, most complicated 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) The basic type of the interface interface. Dual expressed support for dual interface (Note 2), this is very important, very common, but we don't speak today. Custom represents a custom excuse. Remember! Remember! In our first COM program, you must choose it! ! ! ! (If you choose the wrong, please delete all the content, recall.) AGGREGATION We write the components, whether it is allowed to be used by others (Note 3). ONLY indicates that it must be aggregated to use, a bit similar to the pure virtual 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. Does Support Isupp PortrorInfo supports rich information error handling interfaces. In the future. Does Support Connection Point supports a connection point interface (event, callback). In the future. Free threaded marshaler will not talk, even if you kill you, I don't say it! (Note 4) Fourth, the interface function is added, the menu map of the increased interface method is called, increase the interface function add Figure 7. Increase the interface function CAT, increase the add () function, increase the add () function; due to Figure 7 Increase the parameters of the CAT () function, I don't have the appropriate input space, please pay attention to yourself. [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 5) Figure 8. During the completion of the interface function, we all know that if you want to change the class functions in C , you need to modify two places: First, the function declaration of the header file (.h), the second is 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. Due to the BUG of VC6.0, it may not be a pattern that everyone can perform in the figure (Fig. 8) after increasing the interface and interface functions. Solution:

1 Turn off the project, then reopen this method often effectively 2 Close IDE, then re-run 3 Open the IDL file, check if the interface function is correct, please modify 4 Open the IDL file, please modify (add a space, then remove this Space), then save this method often effectively 5 Open the H / CPP file, check if the function exists or correct, there is no change, no, not, this idiom psychology 6 delete interface functions in IDL / H / CPP , Then re-establish the project again, reinstall the VC, reinstall Windows, smash your computer! 5. Implementing the interface function mouse double point Figure 8 CFUN / IFUN / Add (...) can start the input function is implemented:

STDMETHODIMP CFUN :: Add (long N1, long n2, long * pval) {* pval = n1 n2; returnid,} This is too simple, no longer wasting "mouth bar". Below we implements a string CAT () function:

STDMETHODIMP CFUN :: CAT (BSTR S1, BSTR S2, BSTR * PVAL) {INT NLEN1 = :: Systringlen (S1); // S1 Character length INT NLEN2 = :: Systringlen (S2); // S2 Character length * PVAL = :: sysallocstringlen (S1, NLEN1 NLEN2); // Constructing new BSTR while saving S1 first in the IF (Nlen2) {:: Memcpy (* PVAL NLEN1, S2, NLEN2 * Sizeof (Wchar)); / / Then connect S2 to the // WCSCAT (* PVAL, S2);} Return S_OK;} Student: 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 ""

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 (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 (VC6.0 version) of this article to compile and see 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: The name of the name is very nice, but Microsoft has not been implemented.

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