When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
Published: Wednesday, September 16, 1998

Using ActiveX Controls on your Web Page


Introduction:
You can add a tremendous amount of power to your web site by using ActiveX controls. ActiveX controls are similar to Java Applets, in the sense that they allow you to use a browser to run compiled code. For an ActiveX control, this compiled code can be written in a variety of languages. I have personally written ActiveX controls in only Visual C++ and VisualBASIC.

You may have seen a number of ActiveX controls already being used on the Internet. MSNBC.com, The Zone (http://www.zone.com), and other (mostly Microsoft-run sites) employ ActiveX technology. ActiveX controls are "packaged" in CAB (cabinet) files, which are downloaded from the web site you are visiting if you do not already have the ActiveX control installed. Of course no one wants an executable automatically downloaded and run on their machine, so a number of prompts can be displayed. If the control has been "digitally signed" by a third-party signer, then you will see a prompt informing you that the control in question is safe for executing. If the control has not been signed, you will see a prompt informing you that the control may not be safe to run. To ensure your users see a "Safe to Download" prompt, you must "licence" the ActiveX control. I will fully explain this process later on in the article.

As I said before, ActiveX controls are similar to Java Applets, but there are some distinct differences too. Java Applets are supported on more browsers than ActiveX controls. Java Applets also provide a more secure interface between the client and server. (For more information, read the Java Applet article.) Due to the increased security, however, Java Applets run noticably slower than ActiveX controls.

In this article, I intend to step you through the process of creating an ActiveX control using VisualBASIC 5.0. We will create a simple control and implement it on a web page. Lastly, we will used a pre-signed ActiveX control, and I will demonstrate the "licenced" prompt.


Creating an ActiveX Control:
We are now going to create a very simplistic ActiveX control using VisualBASIC 5.0 (or VB '97). To do this, we must first start VB! Once we have started VB, choose to create an ActiveX Control Project. You should see something similar to what is on the right. Once you click OK, you will be taken to an interface similar to when you are creating a Standard EXE Project. This interface differs slightly, though, from the Standard EXE one, in the sense that you have what appears to be a form without the title bar.

Now we will drag and drop out controls onto our canvas, and write code to handle the events that these controls fire. This is very similar to placing controls on a standard form. I have chosen to implement the following ActiveX control for this example: I will create a listbox which will contain a series of first names. When the user clicks on one of the names, a message (depending upon the name clicked) will be printing below the listbox. You can see what my control looks like below.

Now we need to write the code behind the events. The code for this example is quite simple. Here is is in full:

Option Explicit

Private Sub lstName_Click()
      Select Case lstName.ListIndex
           Case 0:
                lblYourName.Caption = "Hello Scott"
           Case 1:
                lblYourName.Caption = "Hello Justin"
           Case 2:
                lblYourName.Caption = "Hello James"
           Case 3:
                lblYourName.Caption = "Hello Scott 2"
           Case 4:
                lblYourName.Caption = "Who are you?"
      End Select
End Sub

Private Sub UserControl_Initialize()
      lstName.AddItem "Scott"
      lstName.AddItem "Justin"
      lstName.AddItem "James"
      lstName.AddItem "Scott 2"
      lstName.AddItem "None of the Above"
End Sub

To test the ActiveX control, we must first go to File / Make ActiveXControl.ocx... This will create the ActiveX control and record it in the registry. Next go to File / Add Project... Choose a Standard EXE Project and click OK. (Make sure this new project is set as the default project.) Next go to Project / Components... and choose to add the new control you made (it will be named the same as your ActiveX control project name). You should now see a new icon in the toolbar. It may be dimmed out; if it is, you need to first close the ActiveX control designer, and the icon should undim.

Now, in your new Form for your Standard EXE project, you can create an instance of the new control you just created in the same fashion you create instances of the other, standard controls. You have the same IntelliSense you have with the other controls as well. In a more detailed discussion of ActiveX controls, one could discuss property pages, or ActiveX features (such as aggregation or containment), which must be handled programmatically. However, since we are only creating a very simple control for a simple web page, discussing these topics in depth would be off-topic.

Now that you have your new control and .OCX file, let's put it in a web page. You only need to add the following HTML tag to your page to get this to work:

      <OBJECT ID="MyObject1" CLASSID="CLSID:6F83F556-4DAF-11D2-B06F-0020186313EF">
      </OBJECT>

The CLASSID specifies the controls class id. This value can be found in the registry. To find this value, open up the registry editor (go to Start / Run; type in "regedit"). Search for the name of your OCX (the project name of your control in VisualBASIC. If you are not presented with a lengthy class ID in the left pane, hit F3 to continue searching until you find the class ID. Then just copy that into your CLASSID tag as shown above.

If you haven't guessed, this won't work on someone's machine who does not have the control already there and registered (which is everyone else in the world right now, since you just created this control, and assigned it a brand, spanking new class ID). So, we will need to implement it so that if someone doesn't have the control, their browser will know how to get it. That is where the CODEBASE tag comes in. Say we upload the .OCX file to the same directory which the .htm file we ar e creating is in. Then this simple change:

      <OBJECT ID="MyObject1" CLASSID="CLSID:6F83F556-4DAF-11D2-B06F-0020186313EF"
           CODEBASE="MyOCXFileName.OCX">
      </OBJECT>

will allow a net surfer to download our control. Of course since our control is unsigned, they will get a dialog box which will say something to the effect of, "This site contains an ActiveX control which could be unsafe to run. Do you want to run the c ontrol anyway?" Regardless, though, if nothing else, you have written and implemented your own simple ActiveX control.

If you can not see by now, ActiveX controls hold an immense amount of potential in them. You can write very complex, compiled code, and have a visitor's browser execute that code and integrate it as if the functionality you provide is inherent in the web . In fact, we will next look at using a premade, professional ActiveX control, Microsoft's Chart Control 5.0. This control is somewhat bloated and slow, but delivers impressive-looking charts to a web site which can be created dynamically using JavaScri pt or Active Server Pages.


Using Microsoft's Chart Control 5.0:
You may have not been impressed with your ActiveX control you created. In my opinion, though, you should have been. Just imagine, you ran executable code on a web surfer's machine. Having this capability allows you to tap into a great wealth of possibi lities. We will now look at a very nice ActiveX control written by the folks at Microsoft. You can read about this control's properties and methods at http://www.microsoft.com/msdn. I will not take the time t o explain the methods and properties in detail. I will just show you some Active Server Pages code which utilizes the MSChart Control.

To view the code, please visit this link: MSChart Example. (Since we are not hosting this site on IIS / NT, we cannot run .ASP files. So you will have to imagine what, exactly this looks like. You can view this cont rol and its capabilities using VB. Just go to Projects / Components and add the Microsoft Chart Control 5.0 control.)


Licencing Your ActiveX Control:
To licence your ActiveX control, you must have your control "digitally signed" by a third party company, such as Verisign. Such a signature guarantees that the code wasn't modified since it was signed. From Microsoft:

When software (code) is associated with a publisher's unique signature, distributing software on the Internet is no longer an anonymous activity. Digital signatures ensure accountability, just as a manufacturer's brand name does on packaged software. If an organization or individual wants to use the Internet to distribute software, they should be willing to take responsibility for that software. This is based on the premise that accountability is a deterrent to the distribution of harmful code.
(Thank to alert 4Guys reader Spike for providing this quote and clarification on the signing process...)

Once you have this signed .OCX or .CAB file, you need to add a Licence file. You should add a licence file regardless, so let's add one to our listbox control example we did above. Slap the Visual Sudio '97 CD 1 into the ol' CD-ROM (or visit http://ww w.microsoft.com and search for LPKTool). Under the directory Vb5.0\Tools\Lpk_tool you will find a program called, not surprisingly, LPK_Tool.exe. Run this file. You should see something similar to the dialog on the right. Simply select the ActiveX con trol(s) you wish to create a licence file for (in this case choose the control you had just created), then click the Add button, and finally the Save and Exit button. You will be prompted to enter a file name.

Once you have this file (it will be about 900 bytes in length or so), you need to copy it to your web server and add these lines of HTML above your OBJECT tag where you instantiate your ActiveX control in your web page. (An example of this can be seen using the MSChart control.)

      <OBJECT CLASSID="CLSID:3d25aba1-caec-11cf-b34a-00aa00a28331">
           <PARAM NAME="LPKPath" VALUE="MyActiveXLPKFile.lpk">
      </OBJECT>

The class ID here must be the class ID specified above. Any other class ID there will cause this not to work. Again, right under these three lines of HTML code should be your OBJECT tag which instantiates your ActiveX control.


Conclusion:
ActiveX controls allow your web page to transform itself from a plain-jane, graphics and text site to one which can perform a wide variety of tasks, since executable, compiled code is being run. VisualBASIC is the ideal tool to create ActiveX controls in the sense of the time needed to create a professional looking control (although ActiveX controls created in VB are typically larger and slower than ones created using Visual C++). Many sites on the Internet use ActiveX controls, although many more use J ava Applets. Before you run out and go ActiveX crazy there are a few things you should keep in mind:

  • Although ActiveX controls run faster than corresponding Java Applets, they do present security issues. Some of your vistors might not feel comfortable using an ActiveX control that was written by an individual.
  • ActiveX controls are not supported by all browsers. IE 3.x and up as well as Netscape 3.x and up are suppose to support ActiveX controls, but my experience has led me to believe that ActiveX controls can loose some of their appeal if you're not using IE 4.x.
  • Many, many, many, many more sites use Java Applets instead of ActiveX controls. I think the count is something like 100 to 1.

If you would like to learn more about ActiveX controls, there are many great web sites out there. The best place to start it http://www.microsoft.com/search. Happy Programming!



ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article