Accessing ASP objects from VB COM

Posted on 2006-03-23
Last Modified: 2010-03-17
I have created VB COM DLL & instancing it from ASP application.
Now I want to access ASP objects (Session, Application, Server) or site URL from VB COM function.
Is ther any way out for doing this
Question by:aurionpro
    1 Comment
    LVL 23

    Accepted Solution


    How do I access the built-in ASP objects from VB?

    When using IIS3 the only way to get a handle on the built-in ASP objects was via the ScriptingContext. When you use Server.CreateObject, ASP would call the OnStartPage method of your object and pass in a ScriptingContext object. Using that object you could then get a reference to the built-in ASP objects.

    Private objScript As ScriptingContext
    Sub OnStartPage(obj As ScriptingContext)
        Set objScript = obj
    End Sub
    Sub DoSomething()
    Dim objResponse As ASPTypeLibrary.Response

        Set objResponse = objScript.Response
        objResponse.Write "<p>Hello World</p>"
    End Sub

    IIS4 and IIS5 provide this same functionality for backward compatibility but you shouldn't use it. Instead you should get a handle on the built-in objects using the object's ObjectContext. Objects created in ASP are given their own context and you can choose to hook into that context if you want to. In your VB Object Add a reference to COM+ Services Type Library if you are using Win2k , or to MTS if you are using Windows NT, and a reference to Microsoft Active Server Pages Object Library. Although we are adding a reference to MTS/COM+ you don't actually need to register the DLL with MTS, or register it as a configured component under Win2k. The reason being is that, under the covers, your scripts are running in the MTS environment anyway, we're just choosing to utilise it. If you do want to add your component to MTS/COM+ then that is fine too. The thing to note is that you can add the object to MTS or you can leave it as a normal DLL, using this technique does not restrict your deployment choices.

    In our VB code the built-in ASP objects are held in the Items collection of the ObjectContext. Getting a handle on them is a simple matter of;

    Dim objResponse As ASPTypeLibrary.Response
    Set objResponse = GetObjectContext.Item("Response")

    We can now use objResponse just as we would the Response object in ASP. Below is a small demo

    ContextTest1.asp Code:

    <%@ Language=VBScript %>

    if len(trim(Session("FullName"))) > 0 then
          Response.Write "<p>I remember your name as " & Session("FullName") & "</p>"
    end if

    <FORM action="ContextTest2.asp" method=POST>
    Surname: <input type="text" name="txtSurname"><br>
    Forename: <input type="text" name="txtForename"><br>
    <input type="submit" value="Submit">

    ContextTest2.asp Code:

    <%@ Language=VBScript %>
    set obj = Server.CreateObject("ASPObject.ASPClass")
    set obj = nothing

    VB Code :

    Create an ActiveX DLL project called ASPObject and add a class called ASPClass. Add a reference to MTS (if using Win NT) or COM+ Services Type Library (if using Win2k) and a reference to ASP (Microsoft Active Server Pages Object Library). Inside ASPClass put the following code.

    Option Explicit

    Public Sub DoStuff()
    Dim objResponse As ASPTypeLibrary.Response
    Dim objRequest As ASPTypeLibrary.Request
    Dim objSession As ASPTypeLibrary.Session
    Dim sSurname As String
    Dim sForename As String
    Dim sFullName As String
        Set objResponse = GetObjectContext.Item("Response")
        Set objRequest = GetObjectContext.Item("Request")
        Set objSession = GetObjectContext.Item("Session")
        objResponse.Write "<HTML>" & vbCrLf

        objResponse.Write "<HEAD>" & vbCrLf
        objResponse.Write "</HEAD>" & vbCrLf
        objResponse.Write "<BODY>" & vbCrLf

        sSurname = Trim(objRequest("txtSurname"))
        sForename = Trim(objRequest("txtForename"))

        If Len(sSurname) < 2 Then
            sSurname = UCase(sSurname)
            sSurname = UCase(Left(sSurname, 1)) & Mid(sSurname, 2)
        End If
        If Len(sForename) < 2 Then
            sForename = UCase(sForename)
            sForename = UCase(Left(sForename, 1)) & Mid(sForename, 2)
        End If
        sFullName = sForename & " " & sSurname
        objSession("FullName") = sFullName
        objResponse.Write "<p>Hello " & sFullName & "</p>" & vbCrLf
        If Len(Trim(objRequest.ServerVariables("HTTP_REFERER"))) > 0 Then
            objResponse.Write "<p><a href=""" & objRequest.ServerVariables("HTTP_REFERER") & """>Back</a>" & vbCrLf
        End If
        objResponse.Write "</BODY>" & vbCrLf
        objResponse.Write "</HTML>"

        Set objResponse = Nothing
        Set objRequest = Nothing
        Set objSession = Nothing

    End Sub
    i hope it may help you


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Convert websphere application server default chained Certificates from 1024 to 2048 keysize or higher size and also you can change signatureAlgorithm . Please make sure Websphere Application Server fixpack or Above. The following steps a…
    Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now