Advanced Visual Basic - Project 10

An OLE Client for your ActiveX OLE Server

What’s an OLE Server worth without an OLE Client to use its functions?

Nothing . . . almost. In fact, many OLE Servers are also standalone programs (They have an EXE extension verses an OLE Server that is not standalone which has a DLL extension). Excel, Word, Access, and PowerPoint—just to name a few—are all OLE Server applications. In project 9 you created an ActiveX DLL file which is essentially an OLE Server application, which is not designed to standalone. By adding a Form Module, and using its Show method, and then making a few modifications to the project’s properties, you were able to run the project as an ActiveX EXE in a standalone mode for debugging purposes. But project 9 doesn’t need a Form module at all to do its job. However, without an OLE Client application to access its functions, it would be useless.

In this project, you will create an OLE Client for your ActiveX OLE Server.

Load Visual Basic and select New Project under the File menu. Make this a Standard project. Press the F4 key to view the Form’s properties—if they are not already visible.

Use the following illustration as a guide and place 8 Labels and 8 Command buttons where shown. Use the Properties list below the illustration to assign Name and Caption values.

 

Object Property Setting
Command Button Name cmdOS
  Caption Operating System
Command Button Name cmdBuild
  Caption OS Version
Command Button Name cmdCPU
  Caption CPU
Command Button Name cmdTotPhysMem
  Caption Total Physical Mem
Command Button Name cmdAvailPhysMem
  Caption Avail Physical Mem
Command Button Name cmdTotVirtMem
  Caption Total Virtual Mem
Command Button Name cmdAvailVirtMem
  Caption Avail Virtual Mem
Label Name lblObjectCount (Title at top)
Label Name lblOS
Label Name lblBuild
Label Name lblCPU
Label Name lblTotPhysMem
Label Name lblAvailPhysMem
Label Name lblTotVirtMem
Label Name lblAvailVirtMem

Now let’s get down to doing some coding. The first thing you’ll need to do is create 4 Object variables which will be used to reference your OLE Server’s Objects: one each for Computer, OS, CHIP, and MEMORY . Add the following declarations to the General Declarations section of frmClient:

Private objComputer As Object
Private objOS As Object
Private objCHIP As Object
Private objMEMORY As Object

Now go to the Form_Load event procedure and enter the following code:

Set objComputer = CreateObject("Sysinfo.Computer")
lblObjectCount.Caption = _
              "Total Objects in Computer Collection: " _
                            & objComputer.Parts.Count

The first line of code above creates an instance of your Sysinfo.Computer object. Recall that the hierarchy of objects in your ActiveX OLE Server application are called a Collection. Notice that this CreateObject function is looking for Sysinfo.Computer. This is because the name of your server application is Sysinfo (Sysinfo is the name you gave project 9 in the Project Properties dialog box), and Computer is the top-level object in the Object Hierarchy which has its Instancing property set to MultiUse.

The second line of code (above, broken onto multiple lines for readability), extracts the value of Count, which is a property of the Collection object, Parts. Count tells you how many objects there are in the Collection which is below the Computer object in the Object Hierarchy. This value is copied to lblObjectCount.Caption.

Important: Don’t try running this program until all of the following code is entered. Add this code to the cmdOS_Click event procedure:

Set objOS = objComputer.OS
lblOS.Caption = "OS: " & objOS.Name

Examine the above code. Notice how creating a reference to the OS object of objComputer, lets you access its Name property.  Of course we could also access the Name property of OS directly through the objComputer object like this:

lblOS.Caption = "OS: " & objComputer.OS.Name

This works because of the object hierarchy we created in Project 9; the OS, CHIP, and MEMORY objects are actually embedded in the Computer object.  But I wanted you to see how you can set a reference directly to each of the objects.  Doing so avoids unreadable code like this:

lblBytes.Caption = "Bytes: " & Computer.Board.Slot.Card.Memory.Size.Bytes

In the above example (don't type it!) we are dealing with a hypothetical object hierarchy that is 6 levels deep (ours is only 2 levels deep).  The above code would be more readable if a reference were set directly to the Size object in order to access it's Bytes property, like this:

Set objSize = Computer.Board.Slot.Card.Memory.Size
lblBytes.Caption = "OS: " & objBytes.Bytes

Let's continue now with our coding.  Add this code to the cmdBuild_Click event procedure:

Set objOS = objComputer.OS
lblBuild.Caption = "Build: " & objOS.Build

Just like the code in cmdOS_Click, this is how to access the OS object’s Build property.

Add this code to the cmdCPU_Click event procedure:

Set objCHIP = objComputer.CHIP
lblCPU.Caption = "CPU: " & objCHIP.CPU

Add this code to the cmdTotPhysMem_Click event procedure:

Set objMEMORY = objComputer.MEMORY
lblTotPhysMem.Caption = "Total Physical Memory: " & _
            objMEMORY.TotPhys

Add this code to the cmdAvailPhysMem_Click event procedure:

Set objMEMORY = objComputer.MEMORY
lblAvailPhysMem.Caption = "Available Physical Memory: " & _
            objMEMORY.AvailPhys

This is starting to get monotonous J. Add this code to the cmdTotVirtMem_Click event procedure:

Set objMEMORY = objComputer.MEMORY
lblTotVirtMem.Caption = "Total Virtual Memory: " & _
objMEMORY.TotVirt

Add this code to the cmdAvailVirtMem_Click event procedure:

Set objMEMORY = objComputer.MEMORY
lblAvailVirtMem.Caption = "Available Virtual Memory: " & _
objMEMORY.AvailVirt

Finally, add this code to the Form_Unload event procedure.  Note: Be sure to us Unload Me in the cmdExit_Click event procedure, so that the following code will be executed when the user exits with either the Exit button or the form's Close button:

Set objComputer = Nothing
Set objOS = Nothing
Set objCHIP = Nothing
Set objMEMORY = Nothing

Save the project. Before you can test your OLE Client application, you must load Project 9—the OLE Server application—and compile it as a DLL. Follow these instructions:

  1. Load your Project 9 (Sysinfo.vbp).
  2. View the Code of the modSysinfo module, and remove or comment out the frmTest.Show command from the Sub Main procedure.
  3. Select Sysinfo Properties from the Project dropdown menu, and change the Project Type option to ActiveX DLL. Now click the OK button.
  4. When you click the OK button you may see a message box that says. Project ‘Start Mode’ property has been changed. This means that the Start Mode has been changed from Standalone to ActiveX Component automatically. Remember, a DLL file cannot be a standalone application. Click the OK button to close this message box.
  5. Select Make Sysinfo.dll from the File dropdown menu. Be sure you remember in which folder the DLL file is being created, and specify Sysinfo.dll as the file name. Then click the OK button to make the DLL file.

If all went well you should now have a Sysinfo.dll file. Now exit from Visual Basic for the next part of this exercise.

The OLE Registry

How will the CreateObject function know that Sysinfo.Computer refers to your Sysinfo.dll file? In the Windows folder is a hidden system file called System.dat (in windows 95/98. In windows NT 4+ this information resides elsewhere). This is the main System Registry file (there is also a User.dat registry file that stores specific User information). When an OLE Server application is executed (or compiled to a DLL or EXE file) for the first time, an entry is permanently placed in the System.dat file. Whenever an OLE Client application uses the CreateObject function to access an OLE Server application, it searches the System.dat file (in a special place just for OLE Server entries) for an entry matching the parameter passed to the CreateObject command.

By using the Registry Editor to access the Registry, you can see how the entry for your OLE Server application appears (it should look something like this):

There are at least 4 ways that entries for OLE Server applications can be registered in the System.dat file:

  1. The OLE Server is run for the first time (as an EXE file). Actually the Registry entry is also made when you compile the project as an EXE or DLL file in Visual Basic.
  2. The OLE Server is run with the /REGSERVER command-line switch (ActiveX EXE’s only).
  3. The OLE Server is selected in the References dialog box (via the project menu) in another Visual Basic project.
  4. Use regsvr32.exe   <DLL File Name>.  

The Registry Editor

The Registry Editor is an application provided with Windows 95/98/NT/2000 for viewing and editing the Registry—namely the System.dat file. Let’s use the Registry Editor to view the entry for your OLE Server application in the System.dat file (that entry should have been made when you compiled and created the Sysinfo.dll file above).

Your first view of the Registry Editor should look something like this:

The Registry is vast and unfathomable, with thousands and thousands of entries. Doing a manual search for your OLE Server’s entry Sysinfo.Computer, could take forever. To make the impossible possible, select Find from Registry Editor’s Edit dropdown menu.

Type Sysinfo.Computer in the Find What textbox. Make sure that only the Data option is checked in the Look at groupbox. Click the Find Next button. After the search stops, you should see something similar to this (Windows NT 4.0 users—press the F3 key once to search once more, and you should see a screen similar to this one):

This shows you that the name for the ProgID (Program ID) registry entry is Sysinfo.Computer, which is the reference used in the CreateObject function of your OLE Client application.

Click once on the LocalServer folder (Windows NT 4.0 users, click on the InprocServer32 folder) to see where the ActiveX OLE Server’s DLL file is physically located. The OLE Client application uses this information to find the OLE Server application.

You may now exit from the Registry Editor. Note: The Registry Editor is a very dangerous application: You can use the Registry Editor to take a peek at what’s in the Registry (like we just did). You can also change values in the registry with the Registry Editor, and any changes that you make to the registry are saved automatically—be careful! You can thoroughly screw up your system.

Now that you’ve seen that your OLE Server application is registered, you are ready to test the OLE Client application:

  1. Load Visual Basic again, load your Client.vbp project, and run it.
  2. If all goes well, you should see the hard drive light come on as your Sysinfo.dll file is being accessed. This happens when this line of code from the Form_Load event procedure of your program is executed:

Set objComputer = CreateObject("Sysinfo.Computer")

  1. Eventually, you should see the main screen of you OLE Client application appear. Give it a test spin, and try the different buttons. That finishes this project. There are no required enhancements.