ARTICLE

Com and Component creation in C#

Posted by G Gnana Arun Ganesh Articles | COM Interop June 09, 2001
Component is nothing but the reusable piece of software in binary form that can be plugged into other components from other vendors.
Reader Level:

COMPONENT

Component is nothing but the reusable piece of software in binary form that can be plugged into other components from other vendors. Reusable software programs that share a common interface allowing easy integration into applications that is it is a Binary standard that allows any two components to communicate that component may be written in any language.

NEED FOR COMPONENT

Today's applications are time consuming to develop, difficult and costly to maintain and we cannot remove a feature, upgraded or replaced with alternatives. The solution for this problem is reusable software components. Each function has a distinct functionality.

    Microsoft word   Word Processing software.
    Microsoft Excel   Spread sheet-analyze & manipulates data.

This is a situation where we find ourselves requiring features that are not a part of the application we are using.

SOLUTION

The solution to this problem is to isolate the functionality that is required by many applications and creates a new application from it and makes into a common functionality. Each time the client requires the functionality then it should be called and uses its features as though they were its own.

In our project we create the Components from the five applications so that the users can easily use it in many languages and upgrade it easily.

COM FRAME WORK

To create application that can interact with any other application requires that there be a standard manner in which the two applications will interact. So, for that we need a framework that defines the rules that will enable to interact with each other irrespective of who has created the application.

COM AS BLUE PRINT

If all the application follows the same rules for interaction then application created by different people can interact with each other and we can exploit any application to obtain the feature we need.

Microsoft has defined a model that sets standard that will enable application to interact with each other. This model is referred to as the COM. It defines the rules which when implemented will enable applications to interact with other application in a uniform manner. Both calling application and the called application need to follow the rules specified by the COM.

Calling application search the hard disk will be a time consuming affair. Thus COM states all applications that can be provide services to each other application must be registered at a central location. But COM doesn't define what this location is to be or the information that is to be registered.Thus COM acts as blue print.So Com just defines the theory but not how they are to be implemented.

MICROSOFT'S COMPONENT OBJECT MODEL (COM)

When we register a COM or DCOM object within the window registry, we must reference that object's Globally Unique Identifier (GUID), which is a 128-bit (16-byte) number that uniquely identifies the object. Whenever a client program accesses a COM or DCOM object, the client program will use the GUID to reference the object within the windows Registry.

Another powerful COM feature is language independence. COM clients and COM objects have the same layout and behavior at run time, regardless of which language we use to produce the component. COM's independence from any specific language lets build systems with many different components that other programmers create in different language, whether Visual Basic,

C++, or Java. The language we use to create the component simply does not matter in COM. First, it helps in split up large systems into manageable subsystems early in the design phase. Second, it assists to implement each subsystem with a component that we create with any Com-capable tool.

Third, it lets individual teams working on each subsystem has complete. The two most notable of these technologies are OLE (Object Linking and Embedding) and ActiveX. These both use COM to facilitate all the interaction between objects. OLE uses COM to communicate between applications, allowing users to link or embed parts of one application's data and display into another application.

INTERFACES OF A COMPONENT

MICROSOFT'S INTERFACE DEFINITION LANGUAGE (IDL)

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.

FUNCTIONS OF INTERFACES

When 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 nit specify how the object should accomplish the work. In addition, 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.The object creates an array of function pointers, called a V table (the "V" stands for Virtual), and passes a V Table pointer to the client. The client sees the V Table as the interface and uses the V Table pointer it received from the client to locate a particular function pointer.when the client finds the function pointer, the client invokes the method directly.

In other words, when programs access the COM object, they will always access the V Table and receive a vptr from the V Table's.

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.

If we want to see the Interface Definition Language for a particular COM component, we can use the ole view, utility to "reverse engineer" a type library into Interface Definition Language text. The type library lets development tools, such as the Visual Basic compiler and the Visual J++ compiler, build the Table binding at compile time.

COMPONENT CREATION IN C#

INTEROPERABILITY

  1. C# includes native support for the COM and windows based applications.
  2. Allowing restriced use of native pointers.
  3. Users no longer have to explicityly implement the unknown and other COM interfacers, those features are built in.
  4. C# allows the users to use pointers as unsafe code blocks to manipulate your old code.
  5. Components from VB NET and other managed code languages and directlyt be used in C#.

COMPILING  THE COMPONENT

The creation of the component is  a simply writing a class program with in a normal application but in compilation we have to create a library instead of an application.
After creating  a seprate library file that can be used in a client application.

The compilation process should be as follows
csc  /r:  REFRENCE DLL /t  library /out DLLNAME.DLL Program name.cs

The "/t library" in compilation code says the C# compiler to create a library and also tells
to not to look for a 'static Main() method. In  "/r" we have to add the required namespace DLL.After the compilation we have the required library which is ready to use in the client application.To use a component in client application we have to compile like this

csc \r:components library DLL name   program name.cs

ex:  csc /r: arun.dll component.cs

The code is  simpler, and there are only two files (client and server) to deal with. For a sufficiently large application the amount of C code is going to be comparable to the amount of C# code.

CONCLUSION

According to Microsoft's marketing, component development is vastly simplified in .Net framework. Creating and managing dll files is takes a lot of windows developer resources.

The main advantage of C# comes from the reduced number of files and the absence of complex linker options and header files.

COMMENT USING
Employers - Post Free Jobs