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#.
- 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.
- Open a console window and navigate to the project’s folder.
- 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
- Under Project->COMTest Properties, set the Output Type to Class Library. Output Type is found within General, which is found under Common Properties.
- Add a class called “COMObject” to the COMTest project, which will create COMObject.cs.
-
Add an assembly attribute to COMObject.cs.
[assembly:System.Reflection.AssemblyKeyFileAttribute( @"..\..\key.snk" )]
- Generate a globally unique identifier for use with COMObject.
- Start guidgen.exe (this should be easily done when opening a console window and entering “guidgen” at the command prompt).
- Select the Registry Format option in the guidgen utility, generate a new guid, and copy the GUID.
- 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.
- Create a public member function for COMObject that is named “COMObjectFunction” with the following code:
public string COMObjectFunction() { return "Hello, COM!"; }
- Build the solution.
- 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
- The assembly is now accessible using COM.
Test procedures for the COM object that was created above:
- Create a file called “test.vbs.”
- Place the following into test.vbs:
dim o set o = createobject( "COMTest.COMObject" ) Wscript.Echo o.COMObjectFunction
- 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.
March 4th, 2015 at 12:05 am
.tlb has been generated in the COM components instead of .dll.
June 29th, 2013 at 12:33 pm
Great tutorial.. I have one question.. component created using procedure mention above.. will it get the advantages of Garbage Collector?
Thanks Steve
May 7th, 2013 at 12:22 pm
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).
April 25th, 2013 at 5:05 am
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.
August 16th, 2012 at 11:52 pm
Great work.. Understood the basics of creating COM with your simple and elegant article. thanks STEVE !
June 15th, 2012 at 7:40 am
Did it in VS C# Express – here are the things that are a little different:
drop the assembly attribute, instead select “Signing” in project properties, check “Sign the assembly” and browse to your key.snk
I had to add the path to sn.exe and guidgen.exe to my path environment variable before using cmd
I had to run cmd as administrator to get regasm to work
I tested it in classic asp like so:
JScript:
VBScript:
Great article – just what I was looking for and I have barely ever used C# and VS so had to google how to do a couple of things that your instructions assumed I knew.
March 14th, 2012 at 1:04 pm
thanks. It’s working.
I had one problem…..
This was the error.
RegAsm : error RA0000 : Error occurred while registering exported type library…………..
I tried running command prompt under administrator, and it worked.. :-)
February 29th, 2012 at 4:22 am
Thanks this is working with the change “class COMObject” -> “public class COMObject”
February 29th, 2012 at 4:22 am
thanks this is working
November 22nd, 2011 at 6:00 am
I tried this example using Visual Studio 2010. But getting error
“ActiveX component can’t create object: COMTest.COMObject”