The ActiveX EXE that we are about to create contains the following features.
- It has a single class Time (taken as it is from previous chapter).
- A form that is displayed only when ActiveX EXE is invoked as a standalone application. The form is hidden when ActiveX EXE is invoked from a client application (used as a server).
- The form of ActiveX EXE is used to display running digital clock using Time class of the ActiveX EXE
- Client application as usual uses an object of Time class to invoke methods such as GetTime, SetTime
- The startup object of ActiveX EXE is Sub Main, which is a procedure in code module. Main procedure determines whether form in server is to be displayed or not depending upon whether server is invoked as standalone application or invoked by client application.
Creating an ActiveX EXE
Let us start creating ActiveX EXE and its components like code module and form.
- Create a new project using File -> New Project and select ActiveX EXE as project type.
- Visual Basic creates a project with just one class module named Class1.
- Invoke Project Properties and click on right button.
- Select Add -> Class Module from popup menu.
- In Add Class Module dialog select Existing tab and select CLS.
- Click right button on Class1 and select Remove Class1 from popup menu. When you are prompted to save it, select No.
- At this stage, we have only one class module (TIME) in our project.
- Add a form using Project -> Add Form and select a simple form from Add Form dialog box.
- Add a code module using Project -> Add Module and click on Open in Add Module dialog box.
- At this stage, the project explorer should look like figure 26.1.
Figure 26.1: Project Explorer of ActiveX .EXE with one form, one code module and one class.
11. Change the following properties of the project.
Project name AEXETIMESERVER
Project Description Time class in Out-of-process server by P.Srikanth.
Startup object Sub Main
12. Create a procedure with the name Main in code module and write the following code.
Public Sub Main()
If App.StartMode = vbSModeStandalone Then
Listing 26.1: Code for Main procedure of code module.
App object is one of the system objects. If StartMode property contains vbSmodeStandalone, it means ActiveX EXE is run as standalone program. The other option is vbSmodeAutomation.
13. Invoke Form designer and place a label and a timer control on the form.
14. Change the following properties of the form and controls.
|Caption||ActiveX EXE Server|
Figure 26.2: Form in ActiveX Exe at design time.
15. Write the following code for frmTime form.
Dim t As New Time
Private Sub Form_Load()
Private Sub Timer1_Timer()
lblTime.Caption = t.GetTime
Listing 26.2: Code in Form.
16. Save the project and its component under the following names (if you want you may give different names).
Class Module aexetime.cls
Time class is taken from previous project. If we make any changes to time class and save them, the changes will be saved under Time.cls and this might effect the previous project (timerserer). So it is better we have a separate copy of Time class for the AEXETIMERSERER project.
To save time.cls under a different name:
- Select Time class in project explorer and click on right mouse button.
- Select Save Time.Cls As option from popup menu and enter AEXETIME as the name of the new file.
- Time class is now stored under a new name – AEXETIME.CLS. It doesn’t effect CLS and previous project continues to use TIME.CLS.
Creating .EXE file
- Select File menu and choose Make aexetimeserver.exe
- Visual Basic creates exe in the specified directory and also registers it in system registry.
Running ActiveX Server as Standalone application
Let us run aexetimerserver.exe as a standalone application. In this case it should display frmTime, which contains running digital clock.
To run ActiveX exe server as standalone application:
- Click on Start button on Taskbar
- Select Run option from the start menu
- Click on Browse button in Run window and select exe as shown in figure 26.3. (Your folder name may be different)
Figure 26.3: Running aexetimerserver.exe using Start->Run.
4. After selecting the file click on Ok to run the program.
5. A small window (frmTime) is displayed with running digital clock (figure 26.4).
Figure 26.4: Form at runtime. With running digital clock.
Running client application
We use the same client program that we used for ActiveX DLL. But client application should have a reference to ActiveX EXE. So open client application (timetest.vbp) and follow the steps given below.
To create a reference to ActiveX EXE:
- Select Project -> References
- Uncheck Time class by P.Srikanth
- Check Time class in Out-of-process server by P.Srikanth.
- Click on Ok
Run client application by pressing F5. You should see the same screen as you have seen in the previous chapter and it also functions in the same manner. But one important difference is that now, Time class is in a separate process. And it could also be running on a separate machine altogether. If it runs on a different machine then you are accessing an object that is on a different machine and access to such object is handled by DCOM (distributed Component Object Model).
Another important point to be noted here is, when you are invoking ActiveX EXE from client application, frmTime is not displayed. But the application is loaded and Time class is provided to client application. Remember, we have checked for the startup mode and displyed frmTime form only when startup mode is standalone.
That is all you need to do to create an ActiveX EXE. The basic advantatge with ActiveX EXE is- it can run as a separate process. That could as well be on a seperate machine. So think of ActiveX EXE if they are the matters of concern. If you are looking for a collection of classes and no need to run them separately then implement the collection of classes as an ActiveX DLL. ActiveX DLL (in-process server) is much faster than ActiveX EXE (out-of-process server).
Instancing Property of Classes
The value of the Instancing property determines whether your class is private — that is, for use only within your component — or available for other applications.
As its name suggests, the Instancing property also determines how other applications create instances of the class. The property values have the following meanings.
|Private||Means that other applications aren’t allowed access to type library information about the class, and cannot create instances of it. Private objects are only for use within your component.|
|PublicNotCreatable||Means that other applications can use objects of this class only if your component creates the objects first. Other applications cannot use the CreateObject function or the New operator to create objects from the class.|
|Allows other applications to create objects from the class. One instance of your component can provide any number of objects created in this fashion. An out-of-process component can supply multiple objects to multiple clients; an in-process component can supply multiple objects to the client and to any other components in its process.|
|GlobalMultiUse||Is like MultiUse, with one addition: properties and methods of the class can be invoked as if they were simply global functions. It’s not necessary to explicitly create an instance of the class first, because one will automatically be created.|
|SingleUse||Allows other applications to create objects from the class, but every object of this class that a client creates starts a new instance of your component. Not allowed in ActiveX DLL projects.|
|GlobalSingleUse||Is like SingleUse, except that properties and methods of the class can be invoked as if they were simply global functions. Not allowed in ActiveX DLL projects.|
Table 26.1: Options of Instancing property.
Class Modules and Project Types
The value of the Instancing property is restricted in certain project types. Allowed values are shown in the following table:
|Instancing Value||ActiveX EXE||ActiveX DLL|
Registration of ActiveX Components
Every ActiveX component is to be registered in Windows registry. Unless a component is registered, system has no way to get the required information about these components.
Visual Basic creates an entry in the registry for each component as and when component is compiled. But that is the case only with the system in which component is created. But as you can guess, you need to develop component only for once and then install it in other machines. When a component is loaded into another machine (different from development machine) you cannot access the components unless components are registered in windows system registry.
When you buy components, they come with an installation program, which takes care of installing and registering components in client’s machine.
Buf if you ever want to register an ActiveX component ,be it DLL or EXE or OCX, you can use REGSVR32 program available in windows\system folder.
To get help regarding REGSVR32.EXE, just enter REGSVR32 at system prompt after getting into WINDOWS\SYSTEM directory. see figure 26.5 .
Figure 26.5: Syntax of RegSvr32, displayed by RegSvr32 when run without any parameters.
To register TIMERSERVER.DLL, enter:
- Copy dll into required directory. Let us assume the name of the directory is servers, which is in root directory of C drive.
- Enter into windows\system directory. because exe resides in windows\system directory. and normally windows\system directory is not in the path. Otherwise you can invoke it from anywhere.
- Invoke regsvr32 to register dll as follows:
- If it is successful, you will see a window as shown in figure 26.6.
Figure 26.6: Message displayed by RegSvr32 after a successful registration.
To Unregister TimerServer.dll:
If you ever decide to unregister timeserver.dll from registry, you can use regsvr32.exe with /u option to unregister the component.
Enter the following to unregister timeserver.dll.