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.
September 16th, 2011 at 6:48 am
TKS A LOT! Worked perfectly!
August 19th, 2011 at 7:18 am
I tried this example using Visual Studio 2010. It all works except for the last step:
U:\My Documents\Visual Studio 2010\Projects\ComTest\ComTest\bin\Debug>regasm Com
Test.dll /tlb:COMTest.tlb /codebase COMTest
Microsoft (R) .NET Framework Assembly Registration Utility 4.0.30319.1
Copyright (C) Microsoft Corporation 1998-2004. All rights reserved.
RegAsm : error RA0000 : Could not load file or assembly ‘file:///U:\My Documents
\Visual Studio 2010\Projects\ComTest\ComTest\bin\Debug\ComTest.dll’ or one of it
s dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
August 19th, 2011 at 12:55 am
superb
August 11th, 2011 at 5:25 am
To not have warning for [assembly: …] delete this line and set the snk file here:
right click on project -> property -> tab sign -> check “Sign assembly” -> in combobox set file name.
I use VS 2010, all work.
July 22nd, 2011 at 2:40 am
Thanks Steve.
I would advise anybody trying this to first read the whole text by Stever and then follow the instructions by “Scriptor”.
September 22nd, 2010 at 11:32 pm
Nice article.
Easy way of explaining….
Keep it up..
You can also give an example which can be downlodable….
September 7th, 2010 at 1:11 pm
So great !! It worked.
June 21st, 2010 at 2:06 pm
About your step #11 for invoking regasm.exe – understand it will only register the components for use on 32-bit processes. For it to be accessible from 64-bit processes, you need to use:
June 16th, 2010 at 2:45 am
nice work! thank you all Steve and Scriptor…
January 18th, 2010 at 4:34 am
nice one :)