Creating COM Components using Visual C#.NET

Developers are sometimes asked to support older software systems that utilize obsolete technologies. This may be difficult when the development tools used to implement the older software system are not available and have been replaced by newer tools that do not seem to support the former tools’ technologies. Faced with the need to replace a COM component that is used by VBScript in an ASP application, a developer may need to create the replacement using Visual Studio .NET. This guide is motivated at helping developers create COM components using Visual Studio.NET and C#.

Creating the COM Object
  1. Launch Microsoft Visual Studio .NET and create a new Visual C# Project with the Empty Project template. In our example we will create the project and name it “COMTest.” Save the project.
  2. Open a console window and navigate to the project’s folder.
  3. Create a key pair that will be used to sign the .NET assembly with a strong name. At the command prompt, enter:
    sn -k key.snk
  4. Under Project->COMTest Properties, set the Output Type to Class Library. Output Type is found within General, which is found under Common Properties.
  5. Add a class called “COMObject” to the COMTest project, which will create COMObject.cs.
  6. Add an assembly attribute to COMObject.cs.

    [assembly:System.Reflection.AssemblyKeyFileAttribute( @"..\..\key.snk" )]
  7. Generate a globally unique identifier for use with COMObject.
    1. Start guidgen.exe (this should be easily done when opening a console window and entering “guidgen” at the command prompt).
    2. Select the Registry Format option in the guidgen utility, generate a new guid, and copy the GUID.
  8. Use System.Runtime.InteropServices.GuidAttribute to generate an attribute for class COMObject. Pass the GUID as a string to GUIDAttribute, but remove the curly braces that surround the GUID that was copied onto the clipboard.
  9. Create a public member function for COMObject that is named “COMObjectFunction” with the following code:
    public string COMObjectFunction()
    {
    	return "Hello, COM!";
    }
    
  10. Build the solution.
  11. Register the Assembly. While in the directory that contains COMTest.dll, enter the following at the command prompt:
    regasm COMTest.dll /tlb:COMTest.tlb /codebase COMTest
  12. The assembly is now accessible using COM.
Testing the COM Object

Test procedures for the COM object that was created above:

  1. Create a file called “test.vbs.”
  2. Place the following into test.vbs:
    dim o
    set o = createobject( "COMTest.COMObject" )
    Wscript.Echo o.COMObjectFunction
    
  3. Execute test.vbs.

Running test.vbs should cause a MessageBox, which contains the string that was returned by COMObject’s COMObjectFunction, to be displayed.

To be truly a COM component, the developer may need to utilize the DispId and other attributes for the member functions and properties. This mini-howto was written to meet the immediate need of replacing a COM component used by VBScript ASP applications. The components created through this mini-howto may not work in environments that do not have the .NET framework installed, or they may not work with applications that use the IUnknown and IDispatch methods.

Questions, comments, and responses are welcomed and appreciated.

46 Responses to “Creating COM Components using Visual C#.NET”

  1. Madhuri Says:

    .tlb has been generated in the COM components instead of .dll.

  2. Mohit Says:

    Great tutorial.. I have one question.. component created using procedure mention above.. will it get the advantages of Garbage Collector?
    Thanks Steve

  3. Pablo Mauricio Says:

    I was scared to dead after my boss asked to develop a COM component to be called from .Net and an old version of InstallShield which uses vb script. I had no idea where to start. Afar reading this article I now know exactly what to do, than you Steve.
    I would think that the .Net app can continue to call compiled dll (just to use new technology) and the vb script can call the COM component created. Since the source is the same maintenance should not be a problem.

    STEVE!! …MY QUESTION IS, can I develop this in framework 4.5 and assume that the regasm registration will translate it to COM correctly? The reason why I am asking is because we cannot assume that the server where the COM component will be deployed in the customer has framework 4.5 (we do know that they have 2.0).

  4. Rmt Says:

    I developed the class library as explained. but while importing this library from Delphi 5, I am getting list of errors in mscorlib_TLB. Any suggestion would be highly appreciated.

  5. tirumudi Says:

    Great work.. Understood the basics of creating COM with your simple and elegant article. thanks STEVE !

Leave a Reply