Interface Componenet Interoperability
INTERFACE
The fundamental concept behind both COM andDCOM is the interface. An interface is an agreement between a clientand an object about how they will communicate with each other. When wedefine interfaces in languages other than Visual Basic, we have to useMicrosoft?s Interface Definition Language (IDL), which we must compilewithin the Microsoft Interface Definition Language Compiler (MIDL).
GLOBALLY UNIQUE IDENTIFIER (GUID):
Each interface that we define in a COM objectwill include a Universally Unique Identifier (UUID), which theoperating system constructs exactly as it does a Globally UniqueIdentifier. When client programs access an interface the COM objectexposes, the programs will actually reference the interface?s UUID. Aninterface is an agreement between a client and an object about how theycommunicate. The interface becomes a communication channel between theclient and the object. Within our program code, an interface is acollection of related procedures and functions.
ROLE OF INTERFACES:
we create objects that we build around COMand clients to communicate with the COM objects, both the clients andthe objects must communicate exclusively through interfaces.An interface between two objects is actual program code. A logicalinterface is the model for the program code itself. An interface playsthe mediator?s role between the client and the object and is a contractthat specifies the work the object must do, but it dose nOt specify howthe object should accomplish the work.
C++ and Java programmers should always definethe interfaces and co classes with Interface Definition Language beforethey begin the actual program development for a COM-based project. Whenwe compile the COM-based project, the compiler feeds the InterfaceDefinition Language file to the Microsoft Interface Definition Language(MIDL) compiler, produces a binary description file called a typelibrary.
Unlike C++ and Java, Visual Basic does notrequire using Interface Definition Language or the MIDL compiler. TheVisual Basic Interactive Development environment (IDE) creates a typelibrary directly from our Visual Basic source code and builds the typelibrary information.
In C# includes native support for the COM andwindows based applications.and it allowing restriced use of native pointers.C# allows the users touse pointers as unsafe code blocks to manipulate your old code.IN c# the users no longer have to explicityly implement the unknown andother COM interfacers, those features are built in.
Components that implement certain genericinterfaces are often referred to by special names. For example,components that implement the IDispatch interface are called Automationcomponents. Information about interfaces and components can be storedin a type library. Development tools can use this information at designtime to determine what features a particular component exposes.
Com specification requires that object needsto keep track of the number of connections made to it. That is, acomponent needs to know the numbers of clients that are using thecomponent by defining instances of the component class. Thisinformation is required because the component needs to destroy itselfwhen all the clients release the reference to the object. Thisfunctionality needs to be provided by all the objects by using theIunknown interface.
Iunknown interface:
The Iunknown interface has the following three functions:
1.AddRef
It keeps track of the number of instances ofan object .It is called if we Set statement to initialize an object. Itincrements the usage count of the object.
2.Release
It Keeps track of the destruction of theinstances of an object .It is called when you assign the value nothingto an object variable. It decrements the usage count when a variablethat 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 standardautomation interface explicitly designed for exposing component methodsand 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, acomponent also needs to provide custom interface metods to access theproperties and methods of an object.A component that supports both the types of interfaces is termed asdual interface object. A dual interface allows com-complaint clientapplications the most efficient access to the methods and properties ofautomation 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 ComImportattribute marks the class as an externally implemented Com class. Sucha class declaration enables the use of a C# name to refer to a COMclass.
A class that is decorated with the ComImport attribute is subject to the following restrictions:
? It must also be decorated with the Guid attribute, whichspecifies the CLSID for the COM class being imported. A compile-timeerror occurs if a class declaration includes the ComImport attributebut 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 importedfrom COM that has a CLSID of ?0002E510-0000-0000-C000-000000000046?.Instantiating a Worksheet instance causes a corresponding COMinstantiation.
3. The ComRegisterFunction attribute:
The presence of the ComRegisterFunctionattribute on a method indicates that the method should be called duringthe COM regist
ration p
rocess.
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 ComUnregisterFunctionattribute on a method indicates that the method should be called whenthe 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 OLEAutomation DISPID. A DISPID is an integral value that identifies amember 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 thatspecifies name of the dll in which the imported method can be found.
9.The FieldOffset attribute:
The FieldOffset attribute is used to specifythe layout of fields for the struct.The FieldOffset attribute may notbe placed on a field declarations that is a member of a class.
10.The Guid attribute:
The Guid attribute is used to specify aglobally unique identifier (GUID) for a class or an interface. Thisinformation 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 providecustom marshalling information for parameters. All combinations ofthese marshalling attributes are permitted.
14.The IndexerName attribute:
Indexers are implemented in some systems usingindexed properties. If no IndexerName attribute is present for anindexer, then the name Item is used by default. The IndexerNameattribute enables a developer to override this default and specify adifferent 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 attributeindicates that the class or interface should derive from IUnknownrather than IDispatch when exported to COM.
18.The PreserveSig attribute:
The PreserveSig attribute is used to indicatethat the HRESULT/retval signature transformation that normally takesplace 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 interfaceis a communications protocol that defines a set of methods completewith names, arguments and return type.A COM object must implement atlest one interface, although the object is free to implement as manyinterfaces as it requires.












No comments yet... Be the first to leave a reply!