Accessing SYSINFO ActiveX Control to be notified of Windows System Events

Java/J2EE COM Interoperability Products Page

Introduction

An ActiveX Control is a kind of COM component that generally requires hosting in a GUI container. ActiveX Controls typically have the extension '.ocx'.

J-Integra® lets you embed a COM ActiveX Control inside a Java GUI as though the ActiveX Control were a Java GUI component. It lets you access the control's methods and properties and subscribe to its events using the standard Java mechanisms.

Note that using a Java class generated by 'com2java' with the option to generate AWT classes from ActiveX Controls set, J-Integra® will run in native mode automatically.

This example uses the sysinfo.ocx ActiveX Control that comes with Visual BASIC. It is described here.

The example displays a message when the system time is changed.

The Steps Involved

  1. Generate the proxies used to access the SYSINFO OCX

  2. Create and run the Java Application

Generate the proxies used to access the SYSINFO OCX

J-Integra®'s com2java tool (in the J-Integra® 'bin' directory) analyzes a COM type library (which contains descriptions of COM classes and interfaces) and outputs corresponding Java classes and interfaces which can be used to access those COM classes.

  1. First, create the directory into which the proxies will be generated. This example assumes your working directory is d:\pure, and that the proxies will be generated into d:\pure\sysinfo. Create these two directories, or your own equivalents elsewhere.

  2. Start the J-Integra® 'com2java' tool, which will be in \jintegra\bin\com2java.exe. Click on the Select button to select sysinfo.ocx as the type library, which by default is in \WINNT\system32\SYSINFO.OCX on the Windows drive under Windows NT.

  3. Next click on the Options button and check the Generate Java AWT Classes checkbox (don't forget to uncheck it later). Click on OK to leave the Options dialog and return to the main dialog. Do not check this option if your Java client does not embed an ActiveX Control.

  4. Finally enter sysinfo as the Java Package in the main dialog, and then click on the Generate Proxies ... button, and select the d:\pure\sysinfo directory you created above (or its equivalent) as the output directory.

This will have generated a SysInfo.java Java class which incorporates all the methods that the SYSINFO OCX contains, as well as event classes and an event adapter which can be used to allow Java clients to listen to all of the events that the SYSINFO OCX can generate.

Create and run the Java Application

Use your favourite editor to create a file called d:\pure\SysInfoClient.java, with the following contents:

import java.awt.*;
import java.awt.event.*;

public class SysInfoClient {

  public static void main(String[] args) throws Exception {
    Frame frame = new Frame("SYSINFO OCX Java client");
    frame.setSize(new Dimension(300, 10));
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

   
// Create the SYSINFO OCX and listen for the time change event
   
sysinfo.SysInfo si = new sysinfo.SysInfo();
    si.setVisible(false);
    si.addDSysInfoEventsListener(new sysinfo.DSysInfoEventsAdapter() {
      public void timeChanged(sysinfo.DSysInfoEventsTimeChangedEvent theEvent) {
        System.out.println("The time has changed");
      }
    });

    // Add SysInfo control to the frame, and display the frame
    frame.add(si);
    frame.validate();
    frame.setVisible(true);
  }
}

Start a DOS box, and make sure your PATH includes the JDK \bin and \jre\bin directories and the J-Integra® bin directory, and that your CLASSPATH includes the J-Integra® runtime (jintegra.jar), and the current directory (.). Compile the Java class you created and run it. If you get errors about classes not being found, then your CLASSPATH is not set correctly:

กก

cd d:\pure
set path=C:\java\j2sdk1.4.2\bin;C:\java\j2sdk1.4.2\jre\bin;C:\jintegra\bin;%PATH%
set CLASSPATH=%CLASSPATH%;d:\jintegra\lib\jintegra.jar;.
javac SysInfoClient.java
java SysInfoClient

You should see the following GUI. Change the system time using the standard Windows mechanism (double-click on the time displayed in the taskbar). A notification will be sent to your Java client:

If you get an error, please check your CLASSPATH and PATH, and if all else fails, follow the steps at the end of the Trouble Shooter section of the J-Integra® documentation.

The SYSINFO OCX can notify you about many different kinds of events, for example it will let you find out when the machine is about to be suspended.

If you get a NullPointerException when programming your own Java GUI which embeds an ActiveX Control, please refer to J-Integra® Knowledge Base article 30945.