Interacting with COM Components Using C#


This article was contributed by Anand Narayanaswamy.

In this article, we will analyze COM components and their application in C#. These components became popular among developers after Microsoft released Active Server Pages. However, the whole scenario changed when Microsoft released its Windows 2000 operating system and subsequent COM+ technology. In this article, we will examine the fundamentals of this exiting technology followed by its application in C#.

Introduction to COM

COM is a set of specification and services that facilitates a developer to create reusable objects and components for running various applications. COM components are components that are developed by using these specifications. These components can be implemented either as an executable (EXE) or as a Dynamic Link Library (DLL). They are basically developed using Visual Basic or Visual C++. They can either act as a server to a Visual Basic or C++ client application, or can be applied on the Web by using Active Server Pages (ASP).

In Visual Basic 6.0 (See Figure 1), you can refer a COM component in your application by adding a reference from the Project | References Menu. After that, you can call the respective methods of the added components in your client application. These components are introduced to reduce coding and to manage an application effectively.

Figure 1—Project | References Menu

In an Active Server Page application, you will write a code like what's shown below:

Set conn= Server.CreateObject("ADODB.Connection")

In the above code, ADODB is one of the components in which Connection is an object. Conn is a user-defined object, which should be referred while accessing the methods of the Connection object as shown below:

Conn.Open "Internet"

You cannot refer a built-in component directly in applications. As already examined, COM components can be either a Dynamic Link Library or Executable. Let's now discuss what these mean.

Dynamic Link Libraries are referred to as in-process objects because they run within the same address space as the client program that called the component. The main advantage in using these types of components is that a separate process is not created each time the component is accessed. Executables are considered to be out-of-process programs. They are executed in their own address space. Moreover, they consume more processing time when the program is called by an application.

In the case of DLLs, failure of an in-process component will ultimately bring down the application, whereas this may not happen in the case of EXEs because they are executed in their own address space.

With COM technology, you can:

  1. Create reusable components for your applications.
  2. Develop the component using one language and implement it in another language. For instance, you may develop a component using Visual C++ and implement it in a Visual Basic application.
  3. Make changes to the component without affecting the application.

Windows 2000 introduced a new technology called COM+, which is an extension to the existing COM. There are advanced technologies such as DCOM, which means Distributed COM. With DCOM, you can access components of other systems in a networked environment. However, a complete discussion to these technologies is beyond the scope of this article.

Creating a COM Component

In this session, we will demonstrate how to create a COM component by using Visual Basic 6.0, with the help of a series of steps. To create a COM component by using Visual Basic 6.0, you have to use either ActiveX DLL or ActiveX EXE projects. For our discussion, ActiveX DLL is used. The steps required for creating an ActiveX DLL are outlined below.

  1. Fire up Visual Basic and select the ActiveX DLL Icon from the New Project dialog box, as shown in Figure 2.
  2. Figure 2—Visual Basic 6.0 start up

  3. Change the class name to something meaningful like "Our_csharp".
  4. Now supply the code shown in Listing 1:
  5. Listing 1

    Public Function Show()
       MsgBox ("bye")
    End Function
    
  6. To create a function, you can use the Tools | Add procedure menu, as shown in Figure 3.
  7. Figure 3—Adding Procedure

  8. Save the project by supplying relevant class and project names of your choice.
  9. Change the Project name and Description by selecting the Project | Properties menu (see Figure 4).
  10. Figure 4—Properties Dialog

  11. Set the Binary Compatibility from the Components tab of the above menu because this action will not create a separate GUID upon each compilation.
  12. Finally, create your DLL by selecting File | Make Csharpcorner.dll. This is the name by which you save your VB project.

That's all. Your DLL is now complete. The next session will show you how to apply this DLL in your C# program.

Integrating the COM Component in C#

In this session, you will learn how to apply the DLL we discussed in last session in a C# application.

As you know with Visual Basic 6.0, it's possible to develop a COM server as shown in the previous session and implement it in a Visual Basic or Visual C++ client program. You may wonder about the idea of calling this DLL in a C# application. When we compile a C# program, an Intermediate Language (MSIL) is generated and it's called as Managed Code. A Visual Basic 6.0 DLL is Unmanaged, meaning it's not being generated by the Common Language Runtime. But, we can make this Visual Basic DLL interoperate with C# by converting the same into a .NET-compatible version.

It's not possible for a C# program to communicate with a Visual Basic DLL without converting the same into its .NET equivalent. To do so, the .NET SDK provides a tool called tlbimp. It stands for Type Library Import and converts your DLL to its equivalent .NET Assembly. For the above project, supply the following command after properly installing the SDK.

tlbimp Csharpcorner.dll /out:Csharp.dll

A new .NET-compatible file called Csharp.dll would be placed in the appropriate directory. The whole process is still not complete. You have to call this DLL in a C# program, as shown in Listing 2:

Listing 2

using Csharp;
using System;

public class Csharpapply     {

   public static void main()         {
      Our_csharp c = new Our_csharp();
      s.Show();
   }

}

Notice a function named Show() in the above session. It's called in the above listing. Notice the following declaration:

using Csharp;

Csharp is our .NET-compatible DLL file. Upon conversion, it will be changed to a .NET Assembly. In C#, all assemblies are referred with the keyword "using".

Upon execution of the above C# application, you can see the message box, as shown in Figure 5.

Figure 5—Sample Message Box

If you are using Visual Studio .NET, you can refer the DLL from Project | Add Reference | COM Tab. Select your DLL by using the browse button. This will add a reference to your project. After adding a reference, copy the above code and execute.

About the Author

Anand Narayanaswamy is a Microsoft MVP (Microsoft Most Valuable Professional) who works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.



About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in

Comments

  • How can I give a byte array as input to a filter graph in DirectShow?

    Posted by sidsid on 03/30/2007 04:45am

    Hi! I am getting an MPEG video stream which I am saving in a byte array. I have to decode this stream. After some research, I have decided to do this in DirectShow. I am working in C#. Now the problem: How do I provide this byte array stream to the filter graph ? Should I use the Source Filter? Or will I have to obtain that byte array as an object in COM? I am using DirectShow for the first time. I would be really grateful if I could get a reply soon as I am working on a project and time is short. Tell me if I am not clear in my description of my problem. I'll try to explain more. Thanks

    Reply
  • How can I give a byte array as input to a filter graph in DirectShow?

    Posted by sidsid on 03/30/2007 04:43am

    Hi! I am getting an MPEG video stream which I am saving in a byte array. I have to decode this stream. After some research, I have decided to do this in DirectShow. I am working in C#. Now the problem: How do I provide this byte array stream to the filter graph ? Should I use the Source Filter? Or will I have to obtain that byte array as an object in COM? I am using DirectShow for the first time. I would be really grateful if I could get a reply soon as I am working on a project and time is short. Tell me if I am not clear in my description of my problem. I'll try to explain more. thanks

    Reply
  • Error in code

    Posted by Legacy on 11/16/2002 12:00am

    Originally posted by: Brian Di Croce

    public static void main() {
    Our_csharp c = new Our_csharp();
    s.Show();
    }

    Little mistake here. It should be c.Show(); instead of s.Show();

    I really liked your tutorial. Keep on the good work!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds