Reader Level:
ARTICLE

Interface Components Interoperability

Posted by G Gnana Arun Ganesh Articles | C# Language August 27, 2001
The fundamental concept behind both COM and DCOM is the interface. An interface is an agreement between a client and an object about how they will communicate with each other.
  • 0
  • 0
  • 10933

INTERFACE:

The fundamental concept behind both COM and DCOM is the interface. An interface is an agreement between a client and an object about how they will communicate with each other. When we define interfaces in languages other than the visual basic, we have to use Microsoft's Interface Definition Language (IDL), which we must compile within the Microsoft Interface Definition Language Compiler (MIDL).

GLOBALLY UNIQUE IDENTIFIER (GUID)

Each interface that we define in a COM object will include a Universally Unique Identifier (UUID), which the operating system constructs exactly as it does a Globally Unique Identifier. When client programs access an interface the COM object exposes, the programs will actually reference the interface's UUID. An interface is an agreement between a client and an object about how they communicate. The interface becomes a communication channel between the client and the object. Within our program code, an interface is a collection of related procedures and functions.

ROLE OF INTERFACES:

we create objects that we build around COM and clients to communicate with the COM objects, both the clients and the objects must communicate exclusively through interfaces.An interface between two objects is actual program code. A logical interface is the model for the program code itself. An interface plays the mediator's role between the client and the object and is a contract that specifies the work the object must do, but it dose nOt specify how the object should accomplish the work.

C++ and Java programmers should always define the interfaces and co classes with Interface Definition Language before they begin the actual program development for a COM-based project. When we compile the COM-based project, the compiler feeds the Interface Definition Language file to the Microsoft Interface Definition Language (MIDL) compiler, produces a binary description file called a type library.

Unlike C++ and Java, Visual Basic does not require using Interface Definition Language or the MIDL compiler. The Visual Basic Interactive Development environment (IDE) creates a type library directly from our Visual Basic source code and builds the type library information.

In C# includes native support for the COM and windows based applications.and it allowing restriced use of native pointers.C# allows the users to use pointers as unsafe code blocks to manipulate your old code.

IN c# the users no longer have to explicityly implement the unknown and other COM interfacers, those features are built in.

Components that implement certain generic interfaces are often referred to by special names. For example, components that implement the IDispatch interface are called Automation components. Information about interfaces and components can be stored in a type library. Development tools can use this information at design time to determine what features a particular component exposes.

Com specification requires that object needs to keep track of the number of connections made to it. That is, a component needs to know the numbers of clients that are using the component by defining instances of the component class. This information is required because the component needs to destroy itself when all the clients release the reference to the object. This functionality needs to be provided by all the objects by using the Iunknown interface.

Iunknown interface:

The Iunknown interface has the following three functions:

  1. AddRef
    It keeps track of the number of instances of an object .It is called if we Set statement to initialize an object. It increments the usage count of the object.

  2. Release
    It Keeps track of the destruction of the instances of an object .It is called when you assign the value nothing to an object variable. It decrements the usage count when a variable that references the object goes out of scope.

  3. Query Interface
    It is used to request additional interfaces provided interfaces provided by the object.

Standard Automation interface (IDispatch):

The Idispatch interface is a standard automation interface explicitly designed for exposing component methods and properties to a client application. This interface contains four functions:

  1. GetTypeInfoCount
    It Retrieves the number of type information object interfaces that an object provides.

  2. GetTypeInfo
    It Retrieves the type information object, which can be used to get for an interface.

  3. GetIDsOfNames
    It returns the dispID value of the property or method passed as argument from the objects type library.

  4. Invoke
    It calls the method whose dispID is passed as a parameter.

Custom interface methods:

Other than the Idispatch interface, a component also needs to provide custom interface metods to access the properties and methods of an object. A component that supports both the types of interfaces is termed as dual interface object. A dual interface allows com-complaint client applications the most efficient access to the methods and properties of automation objects.

Interoperability with COM programs:

The attributes described below are used for creating programs that interoperate with COM programs.

1. The ComAliasName attribute

2. The ComImport attribute

When placed on a class, the ComImport attribute marks the class as an externally implemented Com class. Such a class declaration enables the use of a C# name to refer to a COM class.
A class that is decorated with the ComImport attribute is subject to the following restrictions:

  • It must also be decorated with the Guid attribute, which specifies the CLSID for the COM class being imported. A compile-time error occurs if a class declaration includes the ComImport attribute but fails to include the Guid attribute.
  • It must not have any members. (A public constructor with no parameters is automatically provided.)
  • It must not derive from a class other than object.

The example

using System.Runtime.InteropServices;
[ComImport, Guid("0002E510-0000-0000-C000-000000000046")]
class Worksheet {}
class Test
{
static void Main()
{
Worksheet w =
new Worksheet(); // Creates an Excel worksheet
}
}

declares a class Worksheet as a class imported from COM that has a CLSID of "0002E510-0000-0000-C000-000000000046". Instantiating a Worksheet instance causes a corresponding COM instantiation.

3. The ComRegisterFunction attribute:
The presence of the ComRegisterFunction attribute on a method indicates that the method should be called during the COM registration process.

4. The ComSourceInterfaces attribute:
The ComSourceInterfaces attribute is used to list the source interfaces on the imported coclass.

5. The ComUnregisterFunction attribute:
The presence of the ComUnregisterFunction attribute on a method indicates that the method should be called when the assembly is unregistered for use in COM.

6. The ComVisible attribute:
The ComVisible attribute is used to specify whether or not a class or interface is visible in COM.

7. The DispId attribute:
The DispId attribute is used to specify an OLE Automation DISPID. A DISPID is an integral value that identifies a member in a dispinterface.

8. The DllImport attribute:
The DllImport attribute is used to specify the dll location that contains the implementation of an extern method.

Specifically, the DllImport attribute has the following behaviors:

  • It can only be placed on method declarations.
  • It has a single positional parameter: a dllName parameter that specifies name of the dll in which the imported method can be found.


9. The FieldOffset attribute:
The FieldOffset attribute is used to specify the layout of fields for the struct.The FieldOffset attribute may not be placed on a field declarations that is a member of a class.

10. The Guid attribute:
The Guid attribute is used to specify a globally unique identifier (GUID) for a class or an interface. This information is primarily useful for interoperability with COM.

11. The HasDefaultInterface attribute:
If present, the HasDefaultInterface attribute indicates that a class has a default interface.

12.The ImportedFromTypeLib attribute:
The ImportedFromTypeLib attribute is used to specify that an assembly was imported from a COM type library.

13.The In and Out attributes:
The In and Out attributes are used to provide custom marshalling information for parameters. All combinations of these marshalling attributes are permitted.

14.The IndexerName attribute:
Indexers are implemented in some systems using indexed properties. If no IndexerName attribute is present for an indexer, then the name Item is used by default. The IndexerName attribute enables a developer to override this default and specify a different name.

15.The InterfaceType attribute:
When placed on an interface, the InterfaceType attribute specifies the manner in which the interface is treated in COM.

16. The MarshalAs attribute:
The MarshalAs attribute is used to describe the marshalling format for a field, method, or parameter.

17.The NoIDispatch attribute:
The presence of the NoIDispatch attribute indicates that the class or interface should derive from IUnknown rather than IDispatch when exported to COM.

18.The PreserveSig attribute:
The PreserveSig attribute is used to indicate that the HRESULT/retval signature transformation that normally takes place during interoperability calls should be suppressed.

19.The StructLayout attribute:
The StructLayout attribute is used to specify the layout of fields for the struct.

20.The TypeLibFunc attribute:
The TypeLibFunc attribute is used to specify typelib flags, for interoperability with COM.

21.The TypeLibType attribute:
The TypeLibType attribute is used to specify typelib flags, for interoperability with COM.

22.The TypeLibVar attribute:
The TypeLibVar attribute is used to specify typelib flags, for interoperability with COM.

23.The ComSourceInterfaces attribute:
The ComSourceInterfaces attribute is used to list the source interfaces on the imported coclass.

CONCLUSION:

An interface defines a contract.An interface is a communications protocol that defines a set of methods complete with names, arguments and return type.A COM object must implement at lest one interface, although the object is free to implement as many interfaces as it requires

COMMENT USING

Trending up