Accessing SYSINFO ActiveX Control to be notified of Windows System Events |
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.
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.
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.
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.
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.
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.
Use your favourite editor to create a file called d:\pure\SysInfoClient.java, with the following contents:
import java.awt.*; |
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 |
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.