A Preliminary Study on COM technology (2)

Source: Internet
Author: User
Tags cmath

The excerpted article is not original. For the sake of clarity, no source code is provided. The example should be taken to the original article link below. :)

 

 

Http://dev.csdn.net/Develop/article/article/25/25203.shtm

 

3. Create a COM component manually

1. From Project Creation to registration

In this process, we will complete three steps: Create the DLL entry function, define the interface file, and implement the registration function.

1.1 create a Win32 DLL Project

Create a Win32 DLL project named mathcom.
In step 2 of the wizard, select the "A smiple DLL project" option. Of course, if you select an empty project, complete the definition of dllmain by yourself.

1.2 define interface file

Generate an interface file named mathcom. IDL. Add the file to the project you just created.

 

 

 

After the correct settings, if there is no compilation error, four

 

File Name Function
Mathcom. h Interface header file. If you want to declare or define an interface, use this file
Mathcom_ I .c Interfaces, class objects, and libraries are defined. This file is introduced only when guid-related things are used. This file can only be introduced once throughout the project, otherwise, a duplicate definition error occurs.
Mathcom_p.c Used for stubs and Proxies
Dlldata. c Unknown

1.3 added the registration function

COM must be registered and logged out.

1.3.1 Add a mathcom. Def File

The def file is a module definition file ). It allows the lead symbol to be suffixed with different import symbols.

// Mathcom. Def File
; Mathcom. Def: Declares the module parameters.


Conclusion 1.5

The following files should be included in our project:
 

File Name Function
Stdafx. h and stdafx. cpp Pre-compiled files
Mathcom. cpp DLL entry function and other important function definitions
Mathcom. Def Module definition file
Mathcom. IDL Interface definition file (four files should be compiled after 1.2)

Now, my so-called COM has implemented the registration and logout functions.

If the following "regsvr32" is executed under the command line or "run" menu
Register this COM component with the absolute path + mathcom. dll. After executing this command, check the hkey_classes_root/CLSID entry of the Registry.
Whether the 3bcfe27e-c88d-rjc-8c94-f5f7b97e7841 exists (God bless ).

For example, Run "regsvr32-u absolute path + mathcom. dll" in the same method as above, and then check the registry.
In fact, the generated DLL is not a COM component. Haha !!! Because it neither implements dllgetclassobject () nor implements either of the ismiplemath and iadvancedmath interfaces.
Let's move on !!!

2. Implement ismiplemath, iadvancedmath, and dllgetclassobject ()

2.1 implement ismiplemath and iadvancedmath Interfaces

Let's modify the original cmath class to implement the ismiplemath interface and iadvancedmath interface.
The modification is as follows:

1) math. h file
/* @ ** # --- 2003-10-29 21:33:44 (tulip )---#**@
# Include "interface. H "*/
# Include "mathcom. H" // newly added to replace the above

2.2 general process of calling COM components


  • 1) Use coinitialize Sequence Function (client) for com library Initialization)
  • 2) Activate COM (client)
  • 3) use the registry key to transfer the corresponding DLL to the com Library (COM library)
  • 4) Call the dllgetclassobject () function (COM component) in the COM component)
  • 5) it is not necessary to return the interface pointer (COM Library) through the class factory.

2.3 dllgetclassobject () Implementation

Add the following statements to mathcom. cpp,

#include "math.h"
#include "MathCOM_i.c"

Modify dllgetclassobject () in mathcom. cpp to the following:


2.4 Client

Next we will write a client program to test this COM.
Create an empty Win32 console project named testmathcom and add it to mathcom workspace.
Add a file named main. cpp to the testmathcom project. The content of this file is as follows:

// Main. cpp File
# Include <windows. h>
# Include "../mathcom. H" // note the path here
# Include "../mathcom_ I .c" // note the path here
# Include <iostream>
Using namespace STD;

Conclusion 2.5

Now we should have 2 projects and 8 files, as shown below:
 

Engineering File Function
Mathcom Stdafx. h and stdafx. cpp Pre-compiled files
  Mathcom. cpp DLL entry function and other important function definitions
  Mathcom. Def Module definition file
  Mathcom. IDL Interface definition file (four files should be compiled after 1.2)
  Math. h and math. cpp Ismiplemath, iadvancedmath interface implementation class
Testmathcom Main. cpp Mathcom client, used to test the mathcom component

In this section, we have completed a practical COM component that is close to the complete. We have completed the client of this COM component. If you have already created a com instance, you may find that the client in this section does not use cocreateinstance () to create a com instance, that's because we haven't implemented the iclassfactory interface in this COM component (this interface is implemented in the next part ).
Through this example, I hope you can understand the following points:

  • 1) For more information about the role of dllgetclassobject (), see the general procedure of calling COM components. Also, set the breakpoint to the dllgetclassobject () function, take a closer look at his implementation (without implementing the iclassfactory Interface) and his input parameters.
  • 2)
    Why is cogetclassobject () used to create COM instances instead of cocreateinstance () in this client program. You
    You can try to use cocreateinstance () to create cmath and see what is the first parameter of dllgetclassobject?
  • 3) implementing the iclassfactory interface is not necessary, but it should be said to be necessary (for how to implement it, see the next chapter)
  • 4) The implementation of dllregisterserver () and dllunregisterserver () should be mastered.
  • 5) The client needs the files when calling the COM component (as long as the two files generated by the IDL file)

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.