[COM/ATL] COM component programming-preliminary understanding

Source: Internet
Author: User
What is a COM component?

Com is the abbreviation of Component Object Model. The COM component is an executable code that is published in the Win32 dynamic link library (DLL) or executable file (exe ).

What is guid

Globally Unique Identifier (guid -- globally unique identifier) is the identifier assigned to the COM object ). It is generated using a complex algorithm that ensures that all COM objects have unique IDs without name conflicts.

COM Component Programming

Programming Mode

  • Structured Programming: represents the C language, top-down programming method, based on the software process or function modules, complete programming.
  • Object-Oriented Programming: it represents the C ++ language, introduces the concept of classes, and proposes features such as encapsulation, inheritance, and polymorphism.
  • Component-Oriented Programming: maintainability and reusability are similar to building blocks.

Component: The component is actually some executable binary code in the form of DLL or EXE.

Features: 1. binary code, which can be executed; 2. flexible and convenient use; 3. unrelated to programming languages

Standard of components: COM provides the standard for component writing. This standard must be followed when writing components in any language.

Component Interface: Provides components to users.

COM interface

Interface: C language, the interface is a function, the C ++ language, the interface is a tired member function, the COM interface, a set of pure virtual functions.

Implementation of the C ++ interface: 1. define pure virtual functions (interfaces) 2. derived implementation Class 3 based on the interface. create an object for the Implementation class (createinstance () function) 4. use the interface to return the created object

DLL Interface implementation: the Code does not need to be re-written or re-compiled

Instance
// Create an MFC interface project and delete the automatically generated files (except stdafx. H and stdafx. CPP), and set properties-> linker-> system-> subsystem Console (/subsystem: Console) // interface. CPP: defines the class behavior of an application. # Include "stdafx. H "# include <objbase. h> // -------- function Provider Interface cmath // interface, equivalent to pure virtual class {public: Virtual int add (INT nadd1, int nadd2) = 0; virtual int sub (INT nsub1, int nsub2) = 0 ;}; class cimpmath: Public cmath {public: Virtual int add (INT nadd1, int nadd2 ); virtual int sub (INT nsub1, int nsub2) ;}; int cimpmath: add (INT nadd1, int nadd2) {return (nadd1 + nadd2);} int cimpmath :: sub (INT nsub1, int nsub2) {return (nSub1-nSub2);} class cimpmath2: Public cmath {public: Virtual int add (INT nadd1, int nadd2 ); virtual int sub (INT nsub1, int nsub2) ;}; int cimpmath2: add (INT nadd1, int nadd2) {return (nadd1 + nadd2 + 100);} int cimpmath2 :: sub (INT nsub1, int nsub2) {return (nSub1-nSub2-100);} cmath * creatinstance () {return New cimpmath2;} // user void main () of the Function () {int nsum [2] = {0}; // common call method: cimpmath2 math; nsum [0] = math. add (100,100); // call method using the interface, depending on the Interface rather than the specific implementation cmath * pmath = creatinstance (); nsum [1] = pmath-> Add (100,100); printf ("nsum [0] = % d; nsum [1] = % d", nsum [0], nsum [1]); // nsum [0] = 300; nsum [1] = 300}

 

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.