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. [“sn -k key.snk” at the command prompt]
- 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. [“regasm COMTest.dll /tlb:COMTest.tlb /codebase COMTest” at the command prompt, while in the directory that contains COMTest.dll]
- 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:
set o = createobject( "COMTest.COMObject" )
- 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.