Some basic concepts of COM

Source: Internet
Author: User
Tags what interface

?

  1. Windows lets share code at the binary level using DLLs. After all, that's how Windows apps function-reusing Kernel32.dll, User32.dll, etc. But since the DLLs is written to a C interface, they can is only is used by C or languages that understand the C calling con Vention. This puts the burden of sharing on the programming language implementer, instead of the DLL itself.

    MFC introduced another binary sharing mechanism with MFC extension DLLs. But these is even more restrictive-you can only use them from the an MFC app.

    COM solves all these problems by defining a? binarystandard, meaning this COM specifies that the binary modules (the DLLs and EXEs) must is compiled to match A specific structure. The standard also specifies exactly how COM objects must is organized in memory. The binaries must also not depend on any feature of any programming language (such as name decoration in C + +). Once that's done, the modules can is accessed easily from any programming language. A binary Standard puts the burden of compatibility in the compiler that produces the binaries, which makes it much easier For the folks, come along later and need to use those binaries.

    ?

  2. A? coclass? (short for?) component? object? class) is contained in a DLL or EXE, and contains the code behind one or more interfaces. The coclass is said to? implementthose interfaces. A? COM object? is an instance of a coclass in memory. Note that a COM "class" was not the same as a C + + "class", although it is often the case that the implementation of a COM C Lass is a C + + class.
  3. A? class ID, or? CLSID, is a GUID of that names a coclass. An? interface ID, or? IID, is a GUID, this names an interface.
  4. An? HRESULTintegral type used by COM to return error and success codes. Isn't It a "handle" to anything, despite? H. prefix. I'll have more than say about HRESULTs and what to test them later on.
  5. The? COM Library? is the part of the OS so you interact with when doing com-related stuff. Often, the COM library is referred to as just "com,"
  6. every language have its own the Dealing with objects. For example, in C + + you create them on the stack, or use new ?to dynamically allocate them. Since com must is language-neutral, the COM library provides its own object-management routines. A Comparison of COM and C + + object management is listed below:

    Creating a new object

  • in C + +, use? operator?new? or create an object on the stack.
  • In COM, a call to the API in the COM library.

    Deleting objects

  • in C + +, use? operator?delete? or let a stack object go out of scope.
  • In COM, all objects keep their own reference counts. The caller must tell the object when the caller was done using the object. COM objects free themselves from memory when the reference count reaches 0.

    Now, in between those, stages of creating and destroying the object, are you actually having to? use? it. When you create a COM object, you tell the COM library what interface you need. If The object is created successfully, the COM library returns a pointer to the requested interface. You can then call methods through that pointer, just as if it were a pointer to a regular C + + object.

Handling HRESULTs

I ' ve already shown some simple error handling, using the? succeeded? FAILED? macros. Now I'll give some more details on what does with the? HRESULTs returned from COM methods.

An ?HRESULT?is a 32-bit signed integer, with nonnegative values indicating Success, and negative values indicating failure. An? hresult ?has three fields:the severity bit (to indicate success or failure), the facility code, and the status Co De. The "facility" indicates what's component or program the? hresult ?is coming from. Microsoft assigns facility codes to the various components, for example COM have one, the Task Scheduler have one, and so on . The "code" is a 16-bit field this has no intrinsic meaning; The codes is just an arbitrary association between a number and a meaning, just like the values returned by? getlasterror ()

If You look up error codes in the? winerror.hfile, you'll see a lot of? HRESULTs listed, with the naming convention [Facility]_[severity]_[description]. Generic? HRESULTs that can is returned by anycomponent (likee_outofmemory) There are no facility in their name. Examples:

  • Regdb_e_readregdb : Facility = REGDB, for "registry database"; E = error; Readregdb is a description of the error (couldn ' t read the database).
  • S_OK: Facility = generic; S = success; OK is a description of the status (Everything ' s OK).

    Fortunately, there is easier ways to determine the meaning of an?HRESULT? than looking through?Winerror.h hresult s for built-in facilities can is looked up with the Error Lookup tool. For example, say-forgot to-call CoInitialize () ?before? cocreateinstance () cocreateinstance () ?will return a value of 0x800401f0. You can enter this value into Error Lookup and you'll see the description: "CoInitialize have not been called."

    can also look up?hresult ?descriptions in the debugger. If you have a? hresult ?variable called? hres , you can view the description in the Watch window by entering" HRES,HR "as the value to Watch. The ", HR" tells VC to display the value as an? hresult ?description.

Some basic concepts of COM

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.