Develop ActiveX client application code
Develop an ActiveX Windows program, such as Visual Basic, VBScript, and Active Server Pages, to use the WebSphere ActiveX to EJB bridge to access enterprise beans.
Important: This topic assumes that we are familiar with ActiveX programming and developing on the Windows platform. For information about the programming concepts of ActiveX application clients and the ActiveX to EJB bridge, refer to the ActiveX to Enterprise JavaBeans™ (EJB) Bridge topic, and related topics.
Consider the information given in ActiveX to EJB bridge as good programming guidelines.
To use the ActiveX to EJB bridge to access a Java class, develop your ActiveX program to complete the following steps:
Tasks
- Create an instance of the XJB.JClassFactory object.
- Create JVM code within the ActiveX program process, by calling the XJBInit() method of the XJB.JClassFactory object. After the ActiveX program has created an XJB.JClassFactory object and called the XJBInit() method, the JVM code is initialized and ready for use.
- Create a proxy object for the Java class, using the XJB.JClassFactory FindClass() and NewInstance() methods. The ActiveX program can use the proxy object to access the Java class, object fields, and methods.
- Call methods on the Java class, using the Java method invocation syntax, and access Java fields as required.
- Use the helper functions to do the conversion in cases where automatic conversion is not possible. We can convert between the following data types:
- Java Byte and Visual Basic Byte
- Visual Basic Currency types and Java 64-bit
- Implement methods to handle any errors returned from the Java class. In Visual Basic or VBScript, use the Err.Number and Err.Description fields to determine the actual Java error.
Example
View a System.out message: The ActiveX to EJB bridge does not have a console available to view Java System.out messages. To view these messages when running a stand-alone client program (such as Visual Basic), redirect the output to a file.
The following example illustrates how to redirect output to a file:
launchClientXJB.bat MyProgram.exe > output.txt
- To view the System.out messages when running a Service program such as Active Server Pages, override the Java System.out OutputStream object to FileOutputStream. For example, in VBScript:
'Redirect system.out to a file ' Assume that oXJB is an initialized XJB.JClassFactory object Dim clsSystem Dim oOS Dim oPS Dim oArgs ' Get the System class Set clsSystem = oXJB.FindClass("java.lang.System") ' Create a FileOutputStream object ' Create a PrintStream object and assign to it our FileOutputStream Set oArgs = oXJB.GetArgsContainer oArgs.AddObject "java.io.OutputStream", oOS Set oPS = oXJB.NewInstance(oXJB.FindClass("java.io.PrintStream"), oArgs) ' Set our System OutputStream to our file clsSystem.setOut oPS
ActiveX client application using helper methods for data type conversion. Generally, data type conversion between ActiveX (Visual Basic and VBScript) and Java methods occurs automatically, as described in ActiveX to EJB bridge, converting data types. However, the byte helper function and currency helper function are provided for cases where automatic conversion is not possible.
- Byte helper function
Because the Java Byte data type is signed (-127 through 128) and the Visual Basic Byte data type is unsigned (0 through 255), convert unsigned Bytes to a Visual Basic Integers, which look like the Java signed byte. To make this conversion, we can use the following helper function:
Private Function GetIntFromJavaByte(Byte jByte) as Integer GetIntFromJavaByte = (CInt(jByte) + 128) Mod 256 - 128 End Function- Currency helper function
Visual Basic 6.0 cannot properly handle 64-bit integers like Java methods can (as the Long data type). Therefore, Visual Basic uses the Currency type, which is intrinsically a 64-bit data type. The only side effect of using the Currency type (the Variant type VT_CY) is that a decimal point is inserted into the type. To extract and manipulate the 64-bit Long value in Visual Basic, use code like the following example. For more details on this technique for converting Currency data types, see Q189862, "HOWTO: Do 64-bit Arithmetic in VBA", on the Microsoft Knowledge Base.
' Currency Helper Types Private Type MungeCurr Value As Currency End Type Private Type Munge2Long LoValue As Long HiValue As Long End Type ' Currency Helper Functions Private Function CurrToText(ByVal Value As Currency) As String Dim Temp As String, L As Long Temp = Format$(Value, "#.0000") L = Len(Temp) Temp = Left$(Temp, L - 5) & Right$(Temp, 4) Do While Len(Temp) > 1 And Left$(Temp, 1) = "0" Temp = Mid$(Temp, 2) Loop Do While Len(Temp) > 2 And Left$(Temp, 2) = "-0" Temp = "-" & Mid$(Temp, 3) Loop CurrToText = Temp End Function Private Function TextToCurr(ByVal Value As String) As Currency Dim L As Long, Negative As Boolean Value = Trim$(Value) If Left$(Value, 1) = "-" Then Negative = True Value = Mid$(Value, 2) End If L = Len(Value) If L < 4 Then TextToCurr = CCur(IIf(Negative, "-0.", "0.") & _ Right$("0000" & Value, 4)) Else TextToCurr = CCur(IIf(Negative, "-", "") & _ Left$(Value, L - 4) & "." & Right$(Value, 4)) End If End Function ' Java Long as Currency Usage Example Dim LC As MungeCurr Dim L2 As Munge2Long ' Assign a Currency Value (really a Java Long) ' to the MungeCurr type variable LC.Value = cyTestIn ' Coerce the value to the Munge2Long type variable LSet L2 = LC ' Perform some operation on the value, now that we ' have it available in two 32-bit chunks L2.LoValue = L2.LoValue + 1 ' Coerce the Munge value back into a currency value LSet LC = L2 cyTestIn = LC.Value
What to do next
After developing the ActiveX client application code, deploy and run the ActiveX application.
Subtopics
- Example: Using an ActiveX client application to access a Java class or object
Use Java proxy objects with the ActiveX to EJB bridge.- Example: ActiveX client application calling Java methods
In the ActiveX to Enterprise Java Beans (EJB) bridge, methods are called using the native language method invocation syntax.- ActiveX client programming best practices
The best way to access Java components is to use the Java language. IBM recommends that we do as much programming as possible in the Java language and use a small simple interface between your COM Automation container (for example, Visual Basic) and the Java code. This interface avoids any overhead and performance problems that can occur when moving across the interface.- ActiveX client programming, tips for passing arrays
Arrays are very similar between Java and Automation containers like Visual Basic and VBScript. This topic provides some important points to consider when passing arrays back and forth between these containers.- ActiveX client programming, Java field programming tips
Using the ActiveX to EJB bridge to access Java fields has the same case sensitivity issue that it has when invoking methods. Field names must use the same case as the Java field syntax.- ActiveX client programming, JClassProxy and JObjectProxy classes
The majority of tasks for accessing your Java classes and objects are handled with the JClassProxy and JObjectProxy objects.- ActiveX client programming, Java virtual machine initialization tips
Initialize the JVM code with the ActiveX to Enterprise Java Beans (EJB) bridge. For an ActiveX client program (Visual Basic, VBScript, or ASP) to access Java classes or objects, the first step that the program must do is to create JVM code within its process.- ActiveX to Java primitive data type conversion values
All primitive Java data types are automatically converted to native ActiveX Automation types. However, not all Automation data types are converted to Java types (for example, VT_DATE). Variant data types are used for data conversion.- ActiveX client programming, handling error codes
All exceptions thrown in Java code are encapsulated and thrown again as a COM error through the ISupportErrorInfo interface and the EXCEPINFO structure of IDispatch::Invoke(), the Err object in Visual Basic and VBScript. Because there are no error numbers associated with Java exceptions, whenever a Java exception is thrown, the entire stack trace is stored in the error description text and the error number assigned is 0x6003.- ActiveX client programming, threading tips
The ActiveX to EJB bridge supports both free-threaded and apartment-threaded access and implements the Free Threaded arshaler to work in a hybrid environment such as Active Server Pages (ASP). Each thread created in the ActiveX process is mirrored in the Java environment when the thread communicates through the ActiveX to EJB bridge.- Example: Enabling logging and tracing for activeX client applications
The ActiveX to EJB bridge provides two logging and tracing formats: Windows Application Event Log and Java Trace Log.- Example: Viewing a System.out message
The ActiveX to EJB bridge does not have a console available to view Java System.out messages. To view these messages when running a stand-alone client program (such as Visual Basic), redirect the output to a file.
Related:
ActiveX to EJB Bridge Running an ActiveX client application Running an ActiveX client application ActiveX to Java primitive data type conversion values A client program does not work