Home  >  Article  >  Web Front-end  >  What is the COM object of javascript

What is the COM object of javascript

青灯夜游
青灯夜游Original
2022-10-14 17:24:412207browse

COM object refers to "Component Object Model Object", which is a reusable software component that uses COM specifications; using COM specifications can ensure that COM objects work well and can be easily integrated into your in the application. COM objects are generally implemented using dynamic link libraries (DLLs); like ordinary DLLs, COM objects expose some methods that the user's application can call to complete any supported operations.

What is the COM object of javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is a COM object?

A Component Object Model (COM) object is a reusable software component that uses COM specifications. Using COM specifications ensures that COM objects work well and are easily integrated into your applications. In fact, COM is basically equivalent to a black box, which can do a lot of work for your application.

COM objects are generally implemented using dynamic link libraries (DLL). Like ordinary DLLs, COM objects expose methods that your application can call to complete any supported operation. The interaction between applications and COM objects is somewhat like the interaction between applications and C objects, but there are some significant differences between them.

  • COM objects implement precise encapsulation. You can't simply create an object and call its public methods arbitrarily. A COM object's public methods are put into one or more interface groups. For the use of methods, you must create a COM object and obtain an appropriate interface for the COM object from the COM object. For example: The IDirect3DCubeTexture8 interface contains a method that allows you to handle cube frame resources. Any methods that do not belong to this interface are inaccessible.

  • The creation of COM objects is different from the creation of C objects. There are several ways to create COM objects, but all involve COM-specific technologies. Microsoft's DirectX application programming interface (API) contains a variety of helper functions and methods for creating most DirectX objects.

  • You must use COM_specific techniques to control the lifetime of COM objects.

  • COM objects do not need to be explicitly loaded. COM objects are contained in a DLL. However, when you use this COM object, there is no need to explicitly load the DLL or explicitly load the static library. Each COM object has a unique registration ID used to create the object. COM will automatically load the correct DLL.

  • COM is a binary specification. COM objects can be written to and accessed from a variety of languages. You don't need to know anything about the COM object source code. For example: Microsoft Visual Bisice applications routinely use COM objects to write C.

##Objects and interfaces

It is important to understand the difference between objects and interfaces. Occasionally, the name of an object is sometimes referenced as the name of the primary interface. Strictly speaking, however, these two conditions are not interchangeable.

  • ▲A COM object can expose any number of interfaces. For example: all COM objects must expose the IUnknown interface, and they generally expose at least one additional interface, and possibly more. In order to use these special methods, you not only have to create the COM object, but also get the correct interface pointer.

  • ▲Many objects may expose the same interface. An interface is a set of methods that perform specified operations. The definition of an interface specifies the syntax of methods and their functionality. Any COM object that supports special operations exposes an appropriate interface. Some interfaces are highly specialized, and they are only exposed by a single object. In most other cases it is exposed by multiple objects. A very special case is that the IUnknown interface must be exposed by every COM object.

Note: If an object exposes an interface, it must define every supported method in the interface. In other words, any method you can call must be sure that it exists. However, the details of method implementation can change from one object to another. For example: different objects may use different algorithms to obtain final results. Sometimes, an object exposes an old interface and only needs to support a subset of methods. You can still call the remaining methods successfully, but they will return E_NOTIMPL.

The COM standard requires that once an interface is released, its definition cannot be changed. You cannot add new methods to an existing interface. You must recreate a new interface. A common practice is to include all methods of the old interface in the next generation interface before adding any new methods.

It is very common for an interface to have several generations. Often, their substance is the same, but their details are different. Typically, an object can expose each generation of interfaces. Doing so allows older programs to continue to use the object's older interface, and newer programs to take advantage of the new interface's features. Usually, interface families have the same name, followed by an integer indicating the generation. For example: If the original interface is named IMyInterface, then the next two generations of interfaces are named IMyInterface2 and IMyInterface3. Integers generally use the version number of directx.

GUIDS

Globally Unique Identifiers (GUIDS) are a key part of the COM program model. At its most basic principles, GUIDS is a 128-bit structure. However, GUIDS are created in a way that ensures that no two GUIDS are identical. COM extensively uses GUIDS for two main purposes:

1. To uniquely identify a specific COM object.
2. To uniquely identify a specific COM interface.

Return value (HRESULT)

All COM methods return a 32-bit value named HRESULT. For most methods, the HRESULT is actually a structure containing two pieces of information.

1. Whether this method succeeds or fails.
2. Output detailed information about the operations supported by the method.

The HRESULT value returned by some methods is defined in Winerror.h. The HRESULT value returned by the method can also be some user-specific information. These values ​​are usually verified in the method's reference page.

The fact that method calls on COM objects can return a variety of codes indicating success or failure of the call means that you must be very careful to test its return value. For example: If the return value of a method call is
S_OK, this indicates that the method call was successful. If the return value is S_FAIL, it indicates that the call failed. Of course, the method call may also return other codes indicating the success or failure of the call. The following code snippet illustrates that it is unsafe to perform such a simple test on the code. The value of hr in the code is the return value of the method call:

    if( hr == E_FAIL )
     {
        //Handle the failure
     }
     else
     {
        //Handle the success
     }

If this code only returns E_FAIL, this code snippet can work well. However, this is not the case. This method call may also return other values, such as: E_NOTIMPL, E_INVALIDARG. If the code returns these values, this code segment does not process them, and by default it will indicate that the program is executing normally, but the actual program does not Incorrect.

If you want to know more detailed information about a method call, you must test each relevant return value. However, you may just want to know whether the method call was successful. A good way for you to test whether a method call is successful is to pass the method's return value to the following two macros, which are defined in the winerror.h file.

1. The macro SUCCEEDED returns TRUE to indicate that the call is successful, and returns FALSE to indicate that the call failed.
2. The macro FAILED returns TRUE to indicate that the call failed, and returns FALSE to indicate that the call was successful.

You can use the FAILED macro to modify the previous code segment.

    if( FAILED(hr) )
    { 
       //Handle the failure.
    }
    else
    {
       //Handle the success.
    }

The above code snippet handles E_NOTIMPL and E_INVALIDARG errors.

Although the return value HRESULT of most methods is a structure value, the return value HRESULT of a few methods is a simple integer value. This implies that methods that return integer values ​​can always be called successfully. If you pass a return value of this type to the SUCCESS macro, the macro will always return TRUE. A common example is the IUnkoown::Release method. The function of this method is to release (decrement) the usage count of an object and return the object's current usage count. The usage count is used to identify the lifetime of an object.

The address of the pointer

If you have seen some reference pages for COM methods, you may have encountered some like the following Situation:

   HRESULT CreateDevice(
      . 
      .
      .
      IDirect3DDevice8 **ppReturnedDeviceInterface
      )

Generally, any C or C developer is familiar with pointers, and COM usually uses an additional indirection standard. The standard is: two asterisks (**) followed by a type specification and the variable name with the typical "pp" prefix. In the previous example, the ppReturnedDeviceInterface parameter is the address of a pointer to the IDirect3DDevice8 interface.

Unlike c, you cannot directly access the methods of a COM object. Instead, you must obtain a pointer to an interface that exposes the methods. The syntax for calling methods of an object interface is the same as the syntax for calling C methods using pointers. For example: to call the IMyTnterface::DoSomething method, you only need to use the following syntax.

   IMyInterfance *pMyTface;
   .
   .
   .
   pMyIface->DoSomething(...) ;

For pointers to COM object interfaces, you cannot directly create a pointer to the interface. You must call one or more other methods to obtain this pointer. For example: the CreateDevice method mentioned earlier.

To get the interface pointer through this method, you need to declare a variable to point to the interface you requested. Pass the address of this variable to the CreateDevice method. In other words, you must pass the address of a variable to this method. When this method returns, this variable will point to the interface you requested, and you can access the methods of this interface through this pointer.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of What is the COM object of javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn