N342 ActiveX

powered by FreeFind
Modified: 
Microsoft has developed the ActiveX technology in part to provide Web designers with some programming ability to extend VBScript, JavaScript, PerlScript, etc. These can be used on either the client or server side, though use on the client side (the browser) requires: The server-side use is more positive since a designer can implement their own components, place them on their server, and use them to generate standard HTML for a client. The client would not need to know that an ActiveX component was involved in generating the HTML. Motivations for using ActiveX are driven primarily by a desire (need) to use weak scripting languages like VBScript or JavaScript in ASP that require considerable help to do anything useful in general. Examples of ActiveX use would be to write a TCP protocol handler in Visual Basic to provide a server-side scripting language component for communicating with another server.

The following simple example is intended to provide the nuts and bolts of implementing an ActiveX component in Visual Basic and using the component in a VBScript (or JavaScript or PerlScript would work the same) on a server.
 

ActiveX Class Definitions in Visual Basic
Visual Basic - HelloWorldClass

Option Explicit 'Global scripting context
Dim ASPsc As ScriptingContext

'Automatically called when object is instantiated

Public Sub OnStartPage(ASP_Scripting_Context As ScriptingContext)

   'Set global variable to reference 
   'scripting context in other subs
    Set ASPsc = ASP_Scripting_Context

End Sub

Public Sub Greeting(myName As String)

   Dim ASPResponse

   ' Create Response object from 
   ' global scripting context object.
   Set ASPResponse = ASPsc.Response

   ASPResponse.Write "<textarea>"

   ASPResponse.Write "Hello World " + myName

   ASPResponse.Write "</textarea>"

End Sub

HelloWorld.Asp

<H1>VB HelloWorldClass</H1>

<%

'  Create our HelloWorldClass object
   Set HW = Server.CreateObject("HelloWorldProject.HelloWorldClass")

'  Call Greetings subroutine
   HW.Greeting("Ray")

%>



Output

 

Exercise 1 - Creating the HelloWorldClass in Visual Basic

  1. VB.
    1. New VB ActiveX DLL project.
    2. Open menu and check: Project | References | Microsoft Active Server Pages Object Library
    3. Enter VB HelloWorldClass program
    4. Save as class: HelloWorldClass.cls and project HelloWorldProject. In the ASP script the object is referred as:

      Set HW = Server.CreateObject("HelloWorldProject.HelloWorldClass") 

      would be class HelloWorldClass and the project HelloWorldProject.

    5. File | Make - Save DLL as HelloWorldDLL.dll
  2. ASP: Copy the ASP script and save as HelloWorld.Asp.
  3. Browser: Enter the URL for HelloWorld.Asp