Chapter 4 Tutorial: ActiveX Components


Creating the application

To create the sample application:

  1. Write the ActiveX server-side component using Power++
  2. Build, register, and deploy the component DLL
  3. Verify that the component is registered
  4. Start the Jaguar server and Jaguar Manager
  5. Define a Jaguar package and import the component definition
  6. Generate stubs using Jaguar Manager
  7. Compile the stub code
  8. Write the client-side code
  9. Create the HTML page and run the sample

Write the ActiveX server-side component using Power++

This section shows you how to create the server-side component. The ActiveX component (AXArithmetic.dll) is included in your Jaguar installation. If you do not need or want to build the component, you can skip to "Verify that the component is registered".

Steps Create the server-side component:

  1. Start Power++.

  2. Select File | New | Target.

  3. Select ActiveX Server DLL as the target type and click Next.

  4. Assign it a target name of AXArithmetic and place it in a directory called c:\AXArithmetic. Select Finish.

  5. Select File | Save Project. Accept the default values.

  6. In the left panel of the AXArithmetic - Classes window, select the AXArithmetic.dll | IAXArithmetic folder.

  7. Select Class | Insert | Method.

    In the Method wizard, assign the method the name Multiply . In the prototype, click between the parentheses, and add this text to define the method parameters:
    double m1, double m2, double *result


    Note   Note Since the last parameter is a pointer to double , Power++ defines an IDispatch interface in which the method returns double . After the component is imported into Jaguar, the Jaguar Manager definition of the multiply method will return double .

  8. Click Finish. Power++ displays a window with a cursor in the IAXArithmetic::multiply subroutine. Insert the following code before the return statement:
    *result = m1 * m2;


  9. Select File | Save Project.


At this point, you are ready to build the component DLL. For more information on defining ActiveX components in Power++, see the Power++ Component Creation Guide.

Build, register, and deploy the component DLL

When you are creating ActiveX DLLs, Power++ allows you to configure the Run command to build, register, and deploy the component DLL.

The steps below configure the Run menu so that the component DLL is registered, then copied, to the Jaguar dll directory.

Steps Build and deploy the component DLL:

  1. Select Run | Run Options from the Power++ menu.

  2. In the General tab of the Run Options window, make sure Do nothing is selected.

  3. Under Copy Files:

    1. Select the option to copy the DLL. Use the Browse button to select the Jaguar dll directory as the destination folder.

    2. Select the option to register the ActiveX server automatically.

    3. Do not select the option to reinstall the component in the Component Palette.


  4. Click OK to close the Run Options window.

  5. Select File | Save Project.

  6. Select Run | Run from the Power++ menu.


Verify that AXArithmetic.dll has been built in the c:\AXArithmetic\debug directory and copied to the Jaguar dll directory.

Verify that the component is registered

ActiveX component DLLs must be registered in the NT Registry before applications can call the DLLs. If you configured the Run options correctly, Power++ should have automatically registered your component.

In Jaguar, the component's name in Jaguar Manager must match the component's program ID (progid) that is stored in the NT Registry. In the steps below, you will confirm the component registration.

Steps Verify that the component is registered:

  1. Open the Registry by selecting Start | Run. Enter regedt32 in the window. Click OK.

  2. Select the HKEY_CLASSES_ROOT on Local Machine window.

  3. Scroll down to AXArithmeticCtl. The AXArithmetic ActiveX control has been registered to your machine under this name. Use this name as the component name for the ActiveX control in Jaguar Manager. There may also be an AXArithmeticCtl.1 entry, which you can ignore.

  4. Close regedt32.


Start the Jaguar server and Jaguar Manager

Steps Start the Jaguar server:

  1. If the Jaguar server is not already running, follow the instructions under "Starting Jaguar" to start the server.


Steps Start Jaguar Manager:

  1. If Jaguar Manager is not already running, start it as described in "Starting Jaguar Manager and Security Manager".


Define a Jaguar package and import the component definition

This section shows you how to create a package for a new component, and import a component's method definition into the package. You can either import ActiveX method definitions from a type library (.tlb) file or a dynamic link library (.dll) file into Jaguar Manager, or you can manually enter method definitions. This tutorial imports method definitions from a type library file.

For more information about importing or creating method definitions and configuring packages and component properties, see the Jaguar CTS Programmer's Guide.

Define a new package

Packages provide a way of organizing related components in a Jaguar application. All components created in the Jaguar tutorials are installed in the Tutorial package. Follow these steps to create the Tutorial package if it does not already exist:

Steps Create the Tutorial package:

  1. Navigate to:

    Jaguar Manager | Servers | Jaguar

  2. In the right panel, click Packages. If the Tutorial package is displayed, skip to the next section, "Define the Jaguar ActiveX component".

  3. Select File | Install Package.

  4. Click Create New Package.

    You see the Package Properties window.

  5. Click OK.

    When you click on the Installed Packages expansion sign, it now includes Tutorial.


Define the Jaguar ActiveX component

You must install the AXArithmetic component on the Jaguar server before you can add methods to it.

Steps Define the Jaguar ActiveX component:

  1. Click on the Tutorial package.

  2. Select File | Install component.

  3. Click on Create and Install a New Component.

    For the component name, enter AXArithmeticCtl . Click Create New Component.

  4. Select Import from ActiveX File. Click OK.

  5. In the Import ActiveX window, click Browse. Navigate to the directory called c:\AXArithmetic. Change to the release subdirectory, then double-click the file DTAXArithmetic.tlb.

  6. On the Component Properties sheet, select ActiveX. Set the stub package name to Tutorial. Click OK.

  7. You should see AXArithmeticCtl displayed as a component in the Tutorial package. Select the AXArithmeticCtl component. Its methods display on the right side of the screen. Double-click on the multiply method to display its property sheet. Verify that the method returns double and accepts two double input parameters.


Generate stubs using Jaguar Manager

Once you have created the package, component, and methods, you need to generate the stub code used as a basis for creating the client-side applet that invokes the server-side component methods.

Steps Generate stubs:

  1. Click on the Tutorial package and select the AXArithmeticCtl component.

  2. Select File | Generate Stubs/Skeletons.

  3. Select Generate Stubs, then select Generate Java Stubs. Fill in the Java Stubs fields as follows:

    1. Select Using CORBA datatypes from the drop-down list.

    2. Select JDK 1.1 for Java version.

    3. In the Java Code Base field, type the full path to your Jaguar html\classes subdirector--for example, %JAGUAR%\html\classes .

    4. For Java Package, enter Tutorial .


  4. Unselect Generate C++ Stubs.

  5. Unselect Generate Skeletons.

  6. Click Generate.


Compile the stub code

Change to the Jaguar html\classes\Tutorial subdirectory and compile the generated files using a JDK 1.1 compiler--for example:

cd %JAGUAR%\html\classes\Tutorial
d:\j11 *.java

Write the client-side code

Create a directory called %JAGUAR%\html\classes\TutorialApps if it does not exist. In this directory, create the Java file below as AXClient.java.

A copy of AXClient.java can be found in the html/docs/tutorial subdirectory of your Jaguar installation. Here is the source for AXClient.java:

//
// This is a sample client that invokes the
// AXArithmeticCtl component created in the Jaguar
// ActiveX component tutorial.
//
package TutorialApps;

import org.omg.CORBA.*;
import org.omg.CosNaming.*; // CORBA CosNaming interfaces
import org.omg.CosNaming.NamingContextPackage.*;
// CosNaming user exceptions
import SessionManager.*;
import java.awt.*;

import Tutorial.*; // Package for component stub classes

public class AXClient extends java.applet.Applet {

// User interface controls
Button mult_button;
TextField m1_text;
TextField m2_text;
TextField result_text;

// Component's name (relative to the server's
// initial name context), of the form package/component
String _compName = "Tutorial/AXArithmeticCtl";

// Component stub instance
Tutorial.AXArithmeticCtl _comp = null;

public void init()
{

// Draw GUI controls
m1_text = new TextField(" ");
m1_text.setText("2.5");
this.add(m1_text);
Label l1 = new Label("*");
this.add(l1);
m2_text = new TextField(" ");
m2_text.setText("3.1");
this.add(m2_text);
Label l2 = new Label("=");
this.add(l2);
result_text = new TextField(" ");
result_text.setEditable(false);
this.add(result_text);
mult_button = new Button("Multiply");
this.add(mult_button);

try {

//
// Initialize the CORBA client-side ORB and
// obtain a stub for the Jaguar component instance.
//
System.out.println("... Creating Jaguar session.");

//
// Initialize the ORB. Note that the org.omg.CORBA.ORBClass
// property must be set in applet parameters.
//
java.util.Properties props = new java.util.Properties();
ORB orb = ORB.init(this, props);

NamingContext nc = null;
org.omg.CORBA.Object objRef = null;
objRef = orb.resolve_initial_references("NameService");
nc = NamingContextHelper.narrow(objRef);

//
// Create an instance of the Jaguar SessionManager::Manager
// CORBA IDL object.
//
SessionManager.Manager manager = null;

NameComponent managerName[] =
{ new NameComponent("AuthenticationService", "") };
manager = ManagerHelper.narrow(
nc.resolve(managerName));

//
// Create an authenticated session with user "Guest"
// and password "GuestPassword".
//
SessionManager.Session session =
manager.createSession("Guest", "GuestPassword");

System.out.println("... Creating component instance.");

//
// Create a stub object instance for the
// Tutorial/AXArithmeticCtl Jaguar component.
//

NameComponent compNc[] =
{ new NameComponent(_compName, "") };
Factory compFactory =
FactoryHelper.narrow ( nc.resolve(compNc) );
_comp = Tutorial.AXArithmeticCtlHelper.narrow(
compFactory.create(session) );

System.out.println("... Created component instance.");

} catch (NotFound nfe) {
// This can happen if you have installed
// the tutorial component under a different
// package or component name in Jaguar Manager.
System.out.println("Error: Component " + _compName
+ " not found. Check the package and component "
+ "name in Jaguar Manager.\n");
System.out.println("NotFound exception details:\n");
nfe.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.UserException ue) {
// Check for other CosNaming exceptions
System.out.println("CORBA CosNaming exception:\n"
+ ue.toString());
ue.printStackTrace();
_comp = null;
} catch (org.omg.CORBA.SystemException se) {
System.out.println(
"Received CORBA system exception "
+"while instantiating component:\n"
+ se.toString() );
se.printStackTrace();
_comp = null;
}

} // init()

// Handle button clicks
public boolean action(Event e, java.lang.Object arg) {
if (e.target == mult_button) {
doMultiply();
return true;
}
else return false;
}

// Call the multiply method and update the displayed result
private void doMultiply() {

// Harvest user input
Double d1 = null;
Double d2 = null;
try {
d1 = new Double(m1_text.getText());
d2 = new Double(m2_text.getText());
} catch (NumberFormatException nfe)
{
this.showStatus("ERROR: Bad number format.");
result_text.setText("");
return;
}

// Call the server component method
double m1 = d1.doubleValue();
double m2 = d2.doubleValue();
try {
double result = _comp.multiply(m1, m2);
result_text.setText((new Double(result)).toString());
}
catch (org.omg.CORBA.SystemException se)
{
System.out.println(
"Exception executing multiply method: "
+ se.toString() );
se.printStackTrace();
}

} // doMultiply

}

Compile this application using a JDK 1.1 compiler:

j11 AXClient.java

Create the HTML page and run the sample

In a text editor, create an HTML file that calls the applet. Name the file runaxtut.html and create it in the %JAGUAR%\html\classes\TutorialApps subdirectory.

A copy of this file is located in your Jaguar installation directory, in the html/docs/tutorial subdirectory. As an alternative to creating the file, copy this file to your %JAGUAR%\html\classes\TutorialApps subdirectory. Here is the text of runaxtut.html:

<html><body bgcolor="#FFFFFF">
<head><title>This Applet runs the Jaguar tutorial ActiveX
component.</head></title>
<hr>

<center>
<applet
codebase="/classes"
code="TutorialApps/AXClient.class"
width=600 height=400>
<h2>This would be a Cool Applet,
but you are not running a Java enabled browser...</h2>
<!-- Specify the Java ORB implementation class -->
<param name = "org.omg.CORBA.ORBClass"
value = "com.sybase.CORBA.ORB">
<!-- Specify the URL for connections to the
Jaguar CosNaming service.
"iiop://:9000/" means to use port 9000 on the
applet's download host, with no initial
name context -->
<param name = "com.sybase.CORBA.NameServiceURL"
value = "iiop://:9000/">
</applet>
</center>

<hr>
</body></html>

No changes are required to this HTML file if your server is configured to use the default IIOP port number, 9000. If you (or your administrator) changed the IIOP port number after installing Jaguar, edit the port number in the HTML file to match.

Run the sample

You do not need to restart the Jaguar server before running the sample.

Note   Classpath conflicts on development workstations If you are running the sample client on a machine where EAServer has been installed, you must configure your browser to eliminate conflicts between Jaguar classes that are downloaded with the applet and classes that are loaded from the local classpath. See "Configuring browsers to run applets on development machines" for instructions.

Steps Run the sample:

  1. Start a Netscape 4.x, Internet Explorer 4.x, or HotJava Web browser. If necessary, you can run the applet on a different machine than the Jaguar host, as long as your server uses a real host address and not localhost or 127.0.0.1.

  2. Connect to the following URL, substituting your Jaguar server's host name for host:
    http://host:8080/classes/TutorialApps/runaxtut.html


    As installed, Jaguar uses 8080 as the HTTP port number. If your server uses a different HTTP port number, change the port number in the URL to match.

  3. Enter numeric values to be multiplied, then click the Multiply button to invoke the Jaguar method. The return value (product) is displayed.


Debugging

Steps Debug if necessary:

  1. Look at the %JAGUAR%\bin\srv.log file and the Java console to check debug messages.


 


Copyright © 2000 Sybase, Inc. All rights reserved.