ActiveX EXE

How to register and unregister ActiveX components using REGSVR32.EXE?
In this chapter let us create an ActiveX EXE, which is also called as out-of-process server. One of the major advantages of the out-of-process server is, it can be run on a different machine from client machine and thus enabling you to develop Application Server in 3-tier client/server architecture. ActiveX Exe could also be run as a separate program. It is suitable for cases where you need to create an application that may be invoked as a standalone application or as a server. When it is invoked as a standalone application then users can interact with the application.
To get a feel of out-of-process server and how to use it in client application, let us create an out-of-process server that uses the Time class (created in the previous chapter), and testing application.
 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 etc.
¨         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.

1.      Create a new project using File -> New Project and select ActiveX  EXE as project type.
2.      Visual Basic creates a project with just one class module named Class1.
3.      Invoke Project Properties and click on right button.
4.      Select Add -> Class Module from popup menu.
5.      In Add Class Module dialog select Existing tab and select TIME.CLS.
6.      Click right button on Class1 and select Remove Class1 from popup menu. When you are prompted to save it, select No.
7.      At this stage, we have only one class module (TIME) in our project.
8.      Add a form using Project -> Add Form and select a simple form from Add Form dialog box.
9.      Add a code module using Project -> Add Module and click on Open in Add Module dialog box.
10.  At this stage, the project explorer
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
         frmTime.Show
   End If
End Sub
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.

Object
Property
Value
Form
Name
FrmTime

Caption
ActiveX  EXE Server
Label1
Name
Lbltime

Caption
00:00:00

Autosize
True

Fontsize
14

Borderstyle
1-Fixed Single
Timer1
Interval
1000

15.  Write the following code for frmTime form.

General/Declarations
Dim t As New Time
Private Sub Form_Load()
    Timer1_Timer
End Sub

Private Sub Timer1_Timer()
    t.SetToCurrent
    lblTime.Caption = t.GetTime
End Sub
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).
Project                   aexetimerserver.vbp

Form                      frmtime.frm

Module                  aexemodule.bas
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:

1.      Select Time class in project explorer and click on right mouse button.
2.      Select  Save Time.Cls As option from popup menu and enter AEXETIME as the name of the new file.
3.      Time class is now stored under a new name  - AEXETIME.CLS. It doesn’t effect TIME.CLS and previous project continues to use TIME.CLS.

Creating  .EXE file

1.      Select File menu and choose Make aexetimeserver.exe option.
2.      Visual Basic creates aexetimeserver.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:

1.      Click on Start button on Taskbar
2.      Select Run option from the start menu
3.      Click on Browse button in Run window and select aexetimerserver.exe
4.      After selecting the file click on Ok to run the program.
5.      A small window (frmTime) is displayed 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:

1.      Select Project ->  References
2.      Uncheck  Time class by P.Srikanth
3.      Check Time class in Out-of-process server by P.Srikanth.
4.      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.

Option
Meaning
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.

MultiUse

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
Private
Yes
Yes
PublicNotCreatable
Yes
Yes
MultiUse
Yes
Yes
GlobalMultiUse
Yes
Yes
SingleUse
Yes

GlobalSingleUse
Yes


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.

To register TIMERSERVER.DLL, enter:

1.      Copy timerserver.dll into required directory. Let us assume the name of the directory is servers, which is in root directory of C drive.
2.      Enter into windows\system directory. because regsvr32.exe resides in windows\system directory. and normally windows\system directory is not in the path. Otherwise you can invoke it from anywhere.
3.      Invoke regsvr32 to register timerserver.dll as follows:

Regsvr32  c:\servers\timeserver.dll

 4.      If it is successful, you will see a window

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.

Regsvr32   /u  c:\servers\timeserver.dll

No comments:

ActiveX EXE

How to register and unregister ActiveX components using REGSVR32.EXE?
In this chapter let us create an ActiveX EXE, which is also called as out-of-process server. One of the major advantages of the out-of-process server is, it can be run on a different machine from client machine and thus enabling you to develop Application Server in 3-tier client/server architecture. ActiveX Exe could also be run as a separate program. It is suitable for cases where you need to create an application that may be invoked as a standalone application or as a server. When it is invoked as a standalone application then users can interact with the application.
To get a feel of out-of-process server and how to use it in client application, let us create an out-of-process server that uses the Time class (created in the previous chapter), and testing application.
 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 etc.
¨         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.

1.      Create a new project using File -> New Project and select ActiveX  EXE as project type.
2.      Visual Basic creates a project with just one class module named Class1.
3.      Invoke Project Properties and click on right button.
4.      Select Add -> Class Module from popup menu.
5.      In Add Class Module dialog select Existing tab and select TIME.CLS.
6.      Click right button on Class1 and select Remove Class1 from popup menu. When you are prompted to save it, select No.
7.      At this stage, we have only one class module (TIME) in our project.
8.      Add a form using Project -> Add Form and select a simple form from Add Form dialog box.
9.      Add a code module using Project -> Add Module and click on Open in Add Module dialog box.
10.  At this stage, the project explorer
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
         frmTime.Show
   End If
End Sub
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.

Object
Property
Value
Form
Name
FrmTime

Caption
ActiveX  EXE Server
Label1
Name
Lbltime

Caption
00:00:00

Autosize
True

Fontsize
14

Borderstyle
1-Fixed Single
Timer1
Interval
1000

15.  Write the following code for frmTime form.

General/Declarations
Dim t As New Time
Private Sub Form_Load()
    Timer1_Timer
End Sub

Private Sub Timer1_Timer()
    t.SetToCurrent
    lblTime.Caption = t.GetTime
End Sub
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).
Project                   aexetimerserver.vbp

Form                      frmtime.frm

Module                  aexemodule.bas
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:

1.      Select Time class in project explorer and click on right mouse button.
2.      Select  Save Time.Cls As option from popup menu and enter AEXETIME as the name of the new file.
3.      Time class is now stored under a new name  - AEXETIME.CLS. It doesn’t effect TIME.CLS and previous project continues to use TIME.CLS.

Creating  .EXE file

1.      Select File menu and choose Make aexetimeserver.exe option.
2.      Visual Basic creates aexetimeserver.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:

1.      Click on Start button on Taskbar
2.      Select Run option from the start menu
3.      Click on Browse button in Run window and select aexetimerserver.exe
4.      After selecting the file click on Ok to run the program.
5.      A small window (frmTime) is displayed 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:

1.      Select Project ->  References
2.      Uncheck  Time class by P.Srikanth
3.      Check Time class in Out-of-process server by P.Srikanth.
4.      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.

Option
Meaning
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.

MultiUse

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
Private
Yes
Yes
PublicNotCreatable
Yes
Yes
MultiUse
Yes
Yes
GlobalMultiUse
Yes
Yes
SingleUse
Yes

GlobalSingleUse
Yes


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.

To register TIMERSERVER.DLL, enter:

1.      Copy timerserver.dll into required directory. Let us assume the name of the directory is servers, which is in root directory of C drive.
2.      Enter into windows\system directory. because regsvr32.exe resides in windows\system directory. and normally windows\system directory is not in the path. Otherwise you can invoke it from anywhere.
3.      Invoke regsvr32 to register timerserver.dll as follows:

Regsvr32  c:\servers\timeserver.dll

 4.      If it is successful, you will see a window

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.

Regsvr32   /u  c:\servers\timeserver.dll

No comments: