What is the Component Object Model (COM)? Is it language-dependent?

后端 未结 4 1862
灰色年华
灰色年华 2021-01-30 18:45

I\'m new to COM, and I don\'t know what it is or why it exists.

Is this a programming methodology like OOP? Do programming languages have to support it? (with some speci

相关标签:
4条回答
  • 2021-01-30 19:21

    COM was first created as a mechanism to allow Microsoft Office applications to communicate with one another, then, in its second iteration it was modified and extended to become a specification for how binary code components could--if they were constructed according to the specification-- communicate with each other, and share data, no matter what language or OS they were built upon (as long as the binary file (the compiled .dll or .exe) conformed to the COM specification).

    The purpose was to allow "binary reuse" which means that a code component could be reused by multiple client code components, that the original code component knew nothing about, and which were not even in existence when the component was originally compiled. To quote from one of the original architects of COM, Don Box:

    [...] The design paradigm of COM was that component contracts are expressed as type definitions. This was a step forward from the world COM replaced, in which contracts were expressed only as simple functional entry points.In this respect, COM was a major advance because it brought the dynamic loading of code and the type system together in a fairly consistent manner.

    0 讨论(0)
  • 2021-01-30 19:25

    COM at its core is a way of providing a data-passing contract which is independent of any specific language. It is provably not language dependent, as there are many languages which support COM (there are C++, C, .NET, and Java implementations)

    In practice it is useful for a couple of different examples:

    1. Communication between different languages: Because COM is language independent, it is possible to use COM to pass data between components in different languages. For instance you can use COM to talk between C++, Java, and .NET code.
    2. Threading Semantics: COM allows you to define threading semantics for a particular component to ensure it is created in the appropriate thread context no matter where it is used.
    3. General componentization.
    0 讨论(0)
  • 2021-01-30 19:25

    After thinking a while, I suppose the best way to put it for you to grasp the idea would be:

    COM is a way to extend Windows API and publish other custom libraries in the system, so that you can discover and work with those new APIs in the same fashion without a need to recompile your application.

    COM objects are registered in the system (by adding some hooks into the Windows registry). After that is done, your application can query in run-time existence of those expected libraries, and based on their availability to decide how to proceed further (instead of crashing when a statically linked library not found).

    This mechanism is supposed to be language-independent, so any application written in any language should be capable of calling those interfaces and invoking operation of those libraries. In practice, however, some languages do not support some COM types so they get limited COM abilities.

    Answering your question in comments:

    To use COM no need to install anything. Everything is already there, in the form of WinAPI functions, which you can simply call in your application. DllGetClassObject and CoGetClassObject are used to instantiate COM objects. CoRegisterClassObject is used to register COM objects contained in your libraries in the system.

    In order to enable uniform creation and interaction with COM objects, their creation has been entrusted to class factories, these are sort of helper objects. You call CoGetClassObject and ask it to let you talk to the class factory of the object you need. Given the interface to that class library, you ask it to instantiate the object you need. Then you can manipulate the object through interfaces it exposes.

    Have a look at this brief overview on the Wikipedia: Component Object Model

    0 讨论(0)
  • 2021-01-30 19:31

    Component Object Model is a standard defined by Microsoft, for a language-independent binary object interface, i.e. it enables different OO languages to pass around objects and call methods on them.

    0 讨论(0)
提交回复
热议问题