Click here to Skip to main content
15,867,906 members
Articles / Programming Languages / C#
Article

COM and component creation in C#

Rate me:
Please Sign up or sign in to vote.
2.29/5 (28 votes)
1 May 20027 min read 154.4K   41   11
A discussion on COM component creation in C#

Introduction

A 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 a component

Today’s applications are time consuming to develop, difficult and costly to maintain and we cannot remove a feature, upgrade or replace 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 and data manipulation.

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 Framework

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 a blueprint:

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 directly 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 separate 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

eg: 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Asia/Pacific Region Asia/Pacific Region
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Manish Choudhary .NET expert9-Nov-11 23:28
Manish Choudhary .NET expert9-Nov-11 23:28 
GeneralGood Article Pin
HimaBindu Vejella24-Mar-06 22:59
HimaBindu Vejella24-Mar-06 22:59 
GeneralCOM and component creation in C# Pin
Mohamad Rafi28-Sep-05 18:56
Mohamad Rafi28-Sep-05 18:56 
GeneralObtaining C++ variables' addresses througgh C# Pin
InesVT25-Feb-03 5:38
InesVT25-Feb-03 5:38 
GeneralRe: Obtaining C++ variables' addresses througgh C# Pin
Saikat Sen18-Aug-03 21:24
Saikat Sen18-Aug-03 21:24 
GeneralCOM+ Server Application (.NET) cannot return System.Data.DataSet Pin
Kyaw Kyaw Win19-Sep-02 22:17
Kyaw Kyaw Win19-Sep-02 22:17 
GeneralCom server Pin
vdaron26-Aug-02 4:51
vdaron26-Aug-02 4:51 
QuestionWhat are the advantages of using COM in .NET Pin
Michael P Butler2-May-02 11:06
Michael P Butler2-May-02 11:06 
AnswerRe: What are the advantages of using COM in .NET Pin
Andy Smith2-May-02 15:10
Andy Smith2-May-02 15:10 
GeneralO guru Pin
antoine@orchus-tech2-May-02 2:10
antoine@orchus-tech2-May-02 2:10 
GeneralRe: O guru Pin
Adrian-Bogdan Andreias14-Jun-02 3:50
Adrian-Bogdan Andreias14-Jun-02 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.