Com2 --- small example

Source: Internet
Author: User

In the small example of COM1, we probably know what a component class is and what an interface is. In this section, we will implement the process of implementing two interfaces by a component class.


Create a project:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKioL1PNDHLxIRIBAAH1HWMf00U151.jpg "Title =" 001.jpg" alt = "wkiol1pndhlxiribaah1hwmf00u151.jpg"/>


Our solution is named comdemocode and the project name is mathtoolkit. This indicates that the DLL automatically generated by our project is named mathtoolkit ).


It is necessary to list the inheritance relationships for you to see.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FC/wKiom1PNC8qBal8oAADI-_gMo1w822.jpg "Title =" 051.jpg" alt = "wKiom1PNC8qBal8oAADI-_gMo1w822.jpg"/>


Here, the iprimermath interface provides five basic calculation methods: +-*/%. The iadvancemath interface provides the Supply and Demand factorial method.


The preceding six methods are implemented by cmathtool.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FC/wKiom1PNDDXR4QhDAAF8SJbeppA427.jpg "Title =" 002.jpg" alt = "wkiom1pnddxr4qhdaaf8sjbeppa427.jpg"/>


Click Next,


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FC/wKiom1PNDFiT-1EzAAFpNvebYOQ171.jpg "Title =" 003.jpg" alt = "wKiom1PNDFiT-1EzAAFpNvebYOQ171.jpg"/>


The dynamic link library is selected by default. Click Finish.

After the project is created, we can see that there are two projects, the projects with the PS words are not available for now, so right-click it and select Remove.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNDZWhqEAkAAG7QSbZT_0844.jpg "style =" float: none; "Title =" 004.jpg" alt = "wkiol1pndzwhqeakaag7qsbzt_0844.jpg"/>


Then our project will look like this.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNDHuSAkFcAAFbqORdKBQ369.jpg "style =" float: none; "Title =" 005.jpg" alt = "wkiom1pndhusakfcaafbqordkbq0000.jpg"/>


Then we switch the video to the Class View.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNDP6gT9RAAADM0hEVy44406.jpg "style =" float: none; "Title =" 006.jpg" alt = "wkiom1pndp6gt9raaadm0hevy44406.jpg"/>


Right-click the project name in the Class View and choose add class.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNDP_BOVe5AAHUq13SWYg528.jpg "style =" float: none; "Title =" 007.jpg" alt = "wkiom1pndp_bove5aahuq13swyg528.jpg"/>


Select ATL simple object and click Add. The Wizard shown below is displayed.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNDP_Ay5gdAAF3x2rlGhc224.jpg "style =" float: none; "Title =" 008.jpg" alt = "wkiom1pndp_ay5gdaaf3x2rlghc224.jpg"/>


Then write mathtool in the short name, and then we will find that the content of other edit boxes has changed at the same time. See. The upper part is easy to understand, that is, we use the C ++ class method to implement a component class. Then the class name corresponding to C ++ is the name in the class, generally, a C ,. h file and. CPP file is the file name of the header files and source files required for this class. The coclass in the lower half is used to specify the name of our component class, called mathtool. We don't care about the two on the right for the moment. If you want to understand it, make a 10-time example of my example, every time I start again, it's easy to go back to the theoretical books such as the "com" Topic theory, and the things on the right will be clear. The interface is described below.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNDhmiHvdbAAGn-qwpxas504.jpg "style =" float: none; "Title =" 009.jpg" alt = "wKioL1PNDhmiHvdbAAGn-qwpxas504.jpg"/>


Interface is used to specify the interfaces that our components need to implement. As we have mentioned above, we need to implement iprimermath and iadvancemath, so we should first change the first interface to iprimermath.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNDhnALfmBAAGo36Fbpag977.jpg "style =" float: none; "Title =" 010.jpg" alt = "wkiol1pndhnalfmbaag36fbpag977.jpg"/>


Click Next.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PND2fwmyfzAAGv3FjISVE546.jpg "style =" float: none; "Title =" 011.jpg" alt = "wkiom1pnd2fwmyfzaagv3fjisve546.jpg"/>


You must select custom for the interface in this figure. Everything should start from a simple start.

Click Finish to view the Class View, as shown in figure


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNEE3REW-xAADfheM1-No477.jpg "Title =" 012.jpg" alt = "wKiom1PNEE3REW-xAADfheM1-No477.jpg"/>


In this way, we define an interface, but this interface does not define a method. Next we will add a method for this interface.

In the class view, right-click the iprimermath interface and choose add --> Add method.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNEILgI9E1AAFRbaMZ4nc171.jpg "style =" float: none; "Title =" 013.jpg" alt = "wkiol1pneilgi9e1aafrbamz4nc171.jpg"/>


Then, we can see that method name is the function name.

Parameter attributes: parameter attributes

In indicates the input parameter.

Output Parameter

Retval indicates the return parameters (Out And retval are not available by default. They can be enabled only when the parameter type is changed to a type of pointer, because only data can be carried out by pointers, in addition, after retval is selected, out is automatically selected ).


Add the first parameter, in long N1

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PND2ewbDVzAAFei5bnYMg421.jpg "style =" float: none; "Title =" 014.jpg" alt = "wkiom1pnd2ewbdvzaafei5bnymg421.jpg"/>

Add the second parameter in long N2

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNEIGDkx4oAAFrWxm-9ss917.jpg "style =" float: none; "Title =" 015.jpg" alt = "wKioL1PNEIGDkx4oAAFrWxm-9ss917.jpg"/>


Add the third parameter: retval & out long * nresult


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNEfqCHOx1AAF9g0e7f1Y876.jpg "Title =" 017.jpg" alt = "wkiom1pnefqchox1aaf9425e7f1y876.jpg"/>


Click Next.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNEzjhPljUAAEz6rlOO0I684.jpg "style =" float: none; "Title =" 018.jpg" alt = "wkiol1pnezjhzjuaaez6rloo0i684.jpg"/>


Click Finish.



Open the unique. IDL file in the project, or double-click the iprimermath interface in the Class View,


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNEh7BEK_rAAN5ggqLI70029.jpg "style =" float: none; "Title =" 019.jpg" alt = "wkiom1pne1_bek_raan5ggqli70029.jpg"/>


We can see a piece of statements, starting with the structure, we need to know that the selected part is a whole.

[] Is a part, and the following is another part. The lower part defines an add method. Therefore, if we want to continue adding methods, we can directly copy the data in this row and paste it. Just change the function name and parameters.




Well, this is actually an interface description. Then open mathtool. H. Let's take a look at the Declaration of our add function, such:


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNEh6DG0DKAAFuxOgGIRM283.jpg "style =" float: none; "Title =" 01_jpg" alt = "wkiom1pneh6dg0dkaafuxoggirm283.jpg"/>



Okay. Next we will add more interface functions for this interface, for example:


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNFKbAskddAADy-31h_R4380.jpg "Title =" 1.jpg" alt = "wKiom1PNFKbAskddAADy-31h_R4380.jpg"/>

Well, we added four more functions and declared the four newly added functions in our c ++ class.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNFP2jW5ZhAAJDHrVr8ZE056.jpg "Title =" 021.jpg" alt = "wkiom1pnfp2jw5zhaajdhrvr8ze056.jpg"/>


Well, the function has been declared. The following describes how to implement these five functions (of course, in the source file). For layout, I did not perform algorithm exception detection.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNFoHgIXjnAAM-f5CvO38113.jpg "Title =" 024.jpg" alt = "wKioL1PNFoHgIXjnAAM-f5CvO38113.jpg"/>


This will implement the five functions provided by the iprimermath interface ..


Next, let's recall our previous requirement that we should use a COM component class to implement multiple interfaces.


Therefore, the next step is to add multiple interfaces for this component class.


1. Interface Description

Copy the content of iprimermath in the idl file, paste it, and modify it, as shown in figure


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNFg7zgoX9AAUHiAx8XA8324.jpg "style =" float: none; "Title =" 026.jpg" alt = "wkiom1pnfg7zgox9aauhiax8xa8324.jpg"/>


We can see that I have modified the Interface Name, added annotations, and added, delete, and modify functions provided by the interface. More importantly, UUID () you can use the create guid tool under the Tools menu to obtain the value. For example, select "4", click "New guid", and then click "copy, paste the data into UUID (), and then make a slight modification.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNFyijrJKCAAE-LXwDGnI572.jpg "style =" float: none; "Title =" 025.jpg" alt = "wKioL1PNFyijrJKCAAE-LXwDGnI572.jpg"/>


Is that all right? The answer is no. At the very least, we have to let our c ++ class inherit the newly provided interface and declare and implement the methods provided by the new interface.


1. Before inheritance

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKioL1PNGETieSllAAJauQrlX08217.jpg "style =" float: none; "Title =" 027.jpg" alt = "wkiol1pngetiesllaajauqrlx08217.jpg"/>


2. After inheritance


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNFyvTFtwAAAJ3msIEOBE606.jpg "style =" float: none; "Title =" 028.jpg" alt = "wkiom1pnfyvtftwaaaj3msieobe606.jpg"/>


The following describes the function declaration and implementation.


Declaration part:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNGIbivrc8AAKvvuEcefU797.jpg "style =" float: none; "Title =" 029.jpg" alt = "wkiol1pngibivrc8aakvvuecefu797.jpg"/>


Implementation:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNF2ziTf94AAKen9wyuz8970.jpg "style =" float: none; "Title =" 030.jpg" alt = "wkiom1pnf2zitf94aaken9wyuz8970.jpg"/>


So is it OK if everything above is done? Not yet. Haha, there are two more steps to make the operation successful.

1. First, let's see how the description of our component class is described.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNF_6g3DGYAAHM3hhmEuM726.jpg "Title =" 033.jpg" alt = "wkiom1pnf_6g3dgyaahm3hhmeum%.jpg"/>

Obviously, we can see 47-50 rows, which describe our component class. Here there is only one iprimermath. In fact, our component class implements two interfaces, so we need to add another interface here.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKioL1PNGW7hsFrCAAHqkfjEwo4913.jpg "Title =" 034.jpg" alt = "wkiol1pngw7hsfrcaahqkfjewo4913.jpg"/>


Okay, OK. What else can I do? The last step is simpler. Let's talk about its function, which helps us to automatically complete interface queries, reference counts, and other troublesome matters. That is to say, we can use the iprimermath interface to obtain the iadvancemath interface through the query function. In turn, there will be a demo program later ..


How can this function be completed? In the header file of the component class, there is a ing macro.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNGQSSpxqUAAHdiI0znco726.jpg "style =" float: none; "Title =" 031.jpg" alt = "wkiom1pngqsspxquaahdii0zncow..jpg"/>


Then we need to add the ing of our iadvancemath interface to complete all the functions.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNGh7TdI4CAAH9xyto6gs776.jpg "style =" float: none; "Title =" 032.jpg" alt = "wkiol1pngh7tdi4caah9xyto6gs776.jpg"/>


Now, click Generate. Then you can find the program shown in.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNGoPAdDM0AAKTZ2eWiLg644.jpg "style =" float: none; "Title =" 035.jpg" alt = "wkiol1pngopaddm0aaktz2ewilg644.jpg"/>


Click the icon shown in the following figure.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNGWnwt2A1AAAiuGOvcWA882.jpg "style =" float: none; "Title =" 036.jpg" alt = "wkiom1pngwnwt2a1aaaiugovcwa882.jpg"/>


Open the DLL generated by our project and we can see that there is a component class mathtool, which has two interfaces: iprimermath and iadvancemath.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNGoOxYA2lAABl6sQCkUU858.jpg "style =" float: none; "Title =" 037.jpg" alt = "wkiol1pngooxya2laabl6sqcku858.jpg"/>


Expand the node and view more information. The idea is the same as that before the design.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNGWmDCYv6AAD2Xof7dgg575.jpg "style =" float: none; "Title =" 038.jpg" alt = "wkiom1pngwmdcyv6aad2xof7dgg575.jpg"/>


Next, we will add a new project for the current solution,


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNGjOjk7RHAAHp1bHJ8G8919.jpg "style =" float: none; "Title =" 039.jpg" alt = "wkiom1pngjojk7rhaahp1bhj8g8919.jpg"/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNG07RxWdPAAI4FTVi-aw358.jpg "style =" float: none; "Title =" 040.jpg" alt = "wKioL1PNG07RxWdPAAI4FTVi-aw358.jpg"/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNGjSSdm-yAAIqlydUkZQ985.jpg "style =" float: none; "Title =" 041.jpg" alt = "wKiom1PNGjSSdm-yAAIqlydUkZQ985.jpg"/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNG0-yqodkAAIDyu5DVtY639.jpg "style =" float: none; "Title =" 042.jpg" alt = "wKioL1PNG0-yqodkAAIDyu5DVtY639.jpg"/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKioL1PNG02AHfh0AAHUj4yx8oM010.jpg "style =" float: none; "Title =" 043.jpg" alt = "wkiol1png02ahfh0aahuj4yx8om010.jpg"/>


Select active accessibility to complete the initialization and final work of the COM library.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNGsOB_AxlAAGys1wozw4522.jpg "style =" float: none; "Title =" 044.jpg" alt = "wkiom1pngsob_axlaagys1wozw4522.jpg"/>


Then add a button for the dialog box and respond


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKiom1PNGsSy6KXoAACN6kjlC7w292.jpg "style =" float: none; "Title =" 045.jpg" alt = "wkiom1pngssy6kxoaacn6kjlc7w292.jpg"/>


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNG9_SkYBfAAVxNC1QKgA320.jpg "style =" float: none; "Title =" 046.jpg" alt = "wkiol1png9_skybfaavxnc1qkga320.jpg"/>


When a solution has only one project, when we click run, it will automatically call our project program, but when there are multiple projects, at this time, vs does not know which one to call. We need to specify one for it. Through this, we can see that a project font is bold, this means that the program will be started when we click debug or run. We can right-click the project to change the startup project. It is not difficult to touch it by yourself.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNGsTznahlAABbe_VgQwc502.jpg "style =" float: none; "Title =" 047.jpg" alt = "wkiom1pngstznahlaabbe_vgqwc502.jpg"/>

After modification, such.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FD/wKioL1PNG9_znVOyAABbj-itraw339.jpg "style =" float: none; "Title =" 048.jpg" alt = "wKioL1PNG9_znVOyAABbj-itraw339.jpg"/>


The program running effect is as follows:


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNGsWSWWzHAABT0g-ab-k322.jpg "style =" float: none; "Title =" 049.jpg" alt = "wKiom1PNGsWSWWzHAABT0g-ab-k322.jpg"/>


Click the button to bring up the dialog box. We can see that all of them are correct.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNG93yR7UMAAC6Yhp1RKc390.jpg "style =" float: none; "Title =" 050.jpg" alt = "wkiol1png93yr7umaac6yhp1rkc390.jpg"/>



This proves that our components are okay.


If you use C #, you can also use C # to call it.


Then, let's take a look at this inheritance diagram to see if it is easy to understand.


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKioL1PNHUzQTD-6AADI-_gMo1w056.jpg "Title =" 051.jpg" alt = "wKioL1PNHUzQTD-6AADI-_gMo1w056.jpg"/>


Here I want to explain our code. Otherwise, we still don't understand it.


Paste the code again.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKioL1PNG9_SkYBfAAVxNC1QKgA320.jpg "style =" float: none; "Title =" 046.jpg" alt = "wkiol1png9_skybfaavxnc1qkga320.jpg"/>



Well, the methods provided by the component class cannot be called directly through the component class, and must be called using the interface. That is to say, if we call the add method, we must first obtain the iprimermath interface, so how can we get these interfaces?

Creates a component class instance. The 98-row function is used to create a component class instance. The first parameter is the unique identifier (CLSID) of our component class ), it is quite regular. CLSID _ component class name. The second and third parameters need to be understood in the principles of COM. The fourth parameter is to specify the interface ID, finally, it is used to receive interface pointers. Then we can call the method of this interface. Then, this interface can also obtain other interface pointers provided by the same COM component class through the 106-line function, to call the methods provided by other interfaces. Finally, we need to release the obtained interfaces. The release () method of the interfaces is called. The Code in line 116 refers, if all the interfaces provided by an instance in a component class are released, the DLL is detached from the address space of the process.


For the definition of the Interface ID and component class ID, we can view


With this example, you can practice it, and it is not difficult to read books such as the principles of COM.


Let's talk about it later.


Many people send some source code to others, which is very large, dozens of MB or even hundreds of MB. In fact, many files here are useless.


I will first send the directory hierarchy of our solution.


This is our top-level directory.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FE/wKioL1PNILDAziECAAASoaKdZV8494.jpg "style =" float: none; "Title =" 054.jpg" alt = "wkiol1pnildaziecaaasoakdzv8494.jpg"/>


This is the second-level directory.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/FE/wKioL1PNILDADxqPAACJQnuadgk763.jpg "style =" float: none; "Title =" 055.jpg" alt = "wkiol1pnildadxqpaacjqnuadgk763.jpg"/>


This is the mathtoolkit directory.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/FD/wKiom1PNH5aC79fqAALYl1Tdc14521.jpg "style =" float: none; "Title =" 056.jpg" alt = "wkiom1pnh5ac79fqaalyl1tdc14521.jpg"/>


This is the testmathtoolkit directory.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FD/wKiom1PNH5biFU6AAAG_raAPXsM828.jpg "style =" float: none; "Title =" 057.jpg" alt = "wkiom1pnh5bifu6aaag_raapxsm828.jpg"/>




Many files are related to users or save some information. You do not need to issue these files when sending the source code, for example:


All files and directories under all the debug directories, all files under all the release directories, and this directory, *. APs, *. NCB,

The files of the vs2010 icon shown here can be placed over them with a prompt message, which is generally related to the user and can be deleted. If you are not at ease, you can perform experiments on your own, that is, compress all the files, back up them, and try them one by one.


In fact, this small project, after compression is completed, is only a few dozen K, such


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/FE/wKioL1PNIeri8Ya5AABMcJFm7W4917.jpg "Title =" 059.jpg" alt = "wkiol1pnieri8ya5aabmcjfm7w4917.jpg"/>


Okay. End.


Code we uploaded.









This article from the "Qian song" blog, please be sure to keep this source http://qianqianquege.blog.51cto.com/8004200/1441079

Com2 --- small example

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.