Home > Articles > Programming > Visual Basic

  • Print
  • + Share This
Like this article? We recommend

Step by Step

Begin creating the sample Web UI by following these steps:

  1. Start VB and create a new ActiveX DLL project.

  2. From the Project menu, choose References and check "Microsoft Active Server Pages Object Library." This object library is installed on your PC when you install IIS or PWS. Note that it is possible to use this technique without this ASP object library, but it uses late-binding, which is not desirable.

  3. Give your component a nicer name than Project1 by clicking Properties under the Project menu. Type PublishersWebUI for the Project Name and Project Description.

  4. Change the name of the default class (Class1) to clsWebApp.

  5. Add a new (code) module to the project, and change its name to basGlobal.

  6. In basGlobal, make the following declarations:

  7. Public gAspAppl    As Application
    Public gAspReq     As Request
    Public gAspResp    As Response
    Public gAspServer  As Server
    Public gAspSession As Session
  8. In clsWebApp, type the code for the OnStartPage events as follows:
  9. Public Sub OnStartPage(ByRef rScriptingcontext As ScriptingContext)
        Set gAspAppl = rScriptingcontext.Application
        Set gAspReq = rScriptingcontext.Request
        Set gAspResp = rScriptingcontext.Response
        Set gAspServer = rScriptingcontext.Server
        Set gAspSession = rScriptingcontext.Session
    End Sub

    If you expose this OnStartPage method, it is called automatically when your class is instantiated from ASP code. Think of it as being similar to a Form_Load event. I stumbled on this quite by accident one day while surfing the net. I don't know where it came from, but it likely has something to do with WebClasses. In any event, it allows our code to grab a reference to the intrinsic ASP objects. As mentioned earlier, there are other ways to get these references, but I like this one best.

  10. Code a public sub procedure in clsWebApp to act as a method that can be called from your ASP code:

  11. Public Sub WebMain()
        Call gAspResp.Write("Hello World.")
    End Sub
  12. And now, run your project. When you run an ActiveX DLL from inside the VB IDE, VB temporarily registers the component and its public classes, and points the registry back to the VB IDE. When another application creates an instance of a class that your application exposes, the source code from the class gets interpreted and run inside the VB IDE. When you stop the application, VB automatically unregisters your component. This behavior is great because it enables you to use all of VB's interactive debugging tools, just as you do when developing Windows apps.

  13. At this point, your component is running and waiting for something to invoke it. To pass control from IIS (or PWS) to your component, you'll need to create one ASP file that creates an instance of the clsWebApp class and calls its WebMain method. (I know I said that this technique is scriptless, and now I'm telling you to create a script file, but this file is so tiny that I don't think it really counts.) Use Notepad to create a file named PubWebUI.asp, and type its contents as follows:

  14. <%
        Dim mWebUI
        Set mWebUI = Server.CreateObject("PublishersWebUI.clsWebApp")
        Call mWebUI.WebMain
    Or, if you're feeling particularly clever, use this one-line version:
    <% Call Server.CreateObject("PublishersWebUI.clsWebApp").WebMain %>
    sure that you save this script file to somewhere that is reachable by your Web browser, such as: c:\inetpub\wwwroot\PubWebUI.asp.
  15. Finally, run your Web browser, and type the URL for the PubWebUI.asp script file (for example, http://localhost/pubwebui.asp). If everything works as planned, you should see the "Hello World." text displayed in your browser window.

The sequence of the previous processing steps goes something like this: When you type the URL of your script, the Web browser sends a request for this file to the Web server running on your machine. The Web server reads the script file and, because it has an ASP file type, passes the request to the ASP and script-parsing components. When your script's CreateObject command is executed, the registry is searched for a component named PublishersWebUI (which we know points back to the VB IDE). Next, an instance of clsWebApp is created, and because we exposed a method named OnStartPage, it gets automatically called and is passed a reference to the scripting context. Our code then grabs a reference to the intrinsic ASP objects and saves these references in the global variables for later use. Finally, the class's WebMain method is called, which then uses the ASP Response object's Write method to send some simple text back to the browser.

Because we grabbed a reference to all of the intrinsic ASP objects, the VB code is free to use any property or method of these objects, just as VBScript code in an ASP page might. For example, our code can get the contents of form controls, obtain query string parameters, and use session variables.

  • + Share This
  • 🔖 Save To Your Account