COM step-by-Step tutorial

Source: Internet
Author: User
Tags definition define function object model uuid

Introduced

For me, the understanding of COM (Component object model, component objects) is no less than a long journey. I believe that every programmer who wants to understand the basics of COM must write at least one simple COM object using normal C + +, that is, not relying on the support of any templates or macros provided by Mfc/atl. In this article, I'm going to go step-by-step through how to create a simple COM object from the basics. These components are available for VC/VB client programs.

As an exercise, we're going to try to design a COM component that will implement a hypothetical fast-add algorithm. It must pass in two long data type parameters and return another long parameter to the user, that is, the result of the addition algorithm. We are now starting to design the interface.

Interface

The interface of a COM object does not involve the actual implementation, but its approach marks the part of the COM object that is used to communicate with the outside world. We name our interface Iadd, and its declaration uses the interface definition language (Interface definition Language,idl). IDL is the language used to define function flags, which is independent of the various programming languages, allowing the RPC bottom to package, load, and unpack parameters between different computers. In our Iadd interface, we have the Setfirstnumber and Setsecondnumber methods, which are used to pass the parameters of addition. There is also a method, dotheaddition, that is used to complete the addition and pass the results back to the client.

1th Step:

To create a new Win32 DLL project (for example, addobj), we will create all of the next files in this folder. Create an empty file, and then type the following. Save it as a iadd.idl. An identifier for an interface can be generated using the tool Uuidgen.exe.

import "unknwn.idl";
[
object,
uuid (1221db62-f3d8-11d4-825d-00104b3646c0),
helpstring("interface IAdd is used for implementing a super-fast addition Algorithm")
]
interface IAdd : IUnknown
   {
   HRESULT   SetFirstNumber(long nX1);
   HRESULT   SetSecondNumber(long nX2);
   HRESULT   DoTheAddition([out,retval] long *pBuffer);
   };
[
uuid(3ff1aab8-f3d8-11d4-825d-00104b3646c0),
helpstring("Interfaces for Code Guru algorithm implementations .")
]
library CodeGuruMathLib
   {
   importlib("stdole32.tlb");
   importlib("stdole2.tlb");
   interface IAdd;
   }

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.