Archive for November, 2006

Calling .Net objects from classic ASP

Saturday, November 18th, 2006

Microsoft’s COM architecture is pretty amazing once you grasp how easy it is to integrate with COM objects. As far as I know, there is no analog in the Linux world, but if you see Java as a platform and not just a programming language Java just might come close, if someone invented a way to interact with Java classes from bash and other shell scripting languages. However, Java tends to fail because Windows is installed everywhere you want to be while Java is never installed on a PC fresh out of the box. Also, Java’s UI just has too much catch-up to come remotely close to the usability Windows has achieved. I could go on and on about how cool COM is, but that is for another article.

Because of the ease and pervasiveness of COM, you can easily call .Net components from classic ASP pages. To do so, expose the .Net class with a COM interface. The example below simply returns a string in the Hello() function, but you could have done some interesting web services or database connections. The point of this example is to show how easy it is to call .Net from your classic ASP applications.

First, create the .Net object. You will need attributes on the class to explain to the COM system how your class can be invoked. Make sure you strong name this assembly, because you are going to place it in the GAC once you have built it.

using System;
using System.Runtime.InteropServices;

namespace ASPIntegration
{
[ComVisible(true)]
[Guid("96FC5715-B375-43a9-A41B-4853ACC3DC01")]
[ProgId("ASPIntegration.NetObject")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class NetObject
{
public NetObject() { }
public string Hello() { return “Hi, what’s up?”; }

}
}

Second, register the class.

regasm ASPIntegration.dll

If you see “No types were registered.” you might be missing the ComVisible attribute on the class.
Now install the assembly in the GAC so COM can instantiate it.

gacutil /i ASPIntegration.dll

Incidentally, at this point, you can call your class from wscript, PowerShell, or any other program which can speak COM – which is almost everything on the Windows platform. Now write your classic ASP page to invoke the object.

<%
Response.Write “Calling .Net COM object…<br>”
Dim netobj
Set netobj = CreateObject(“ASPIntegration.NetObject”)
Response.Write “Calling NetObject.Hello()=” + netobj.Hello() + “<br>”
Response.Write “Done.<br>”
%>

Have fun integrating.