How To Create a Visual Basic Compatible OCX from a Java Bean (318768)



The information in this article applies to:

  • Microsoft Visual J++ 6.0
  • Microsoft SDK for Java 4.0

This article was previously published under Q318768

SUMMARY

This article shows you how to convert the Microsoft software development kit (SDK) for Java sample Java bean component, SampleBean, into an ActiveX control (.ocx) file that is compatible with Visual Basic 6.0 and other ActiveX control containers.

MORE INFORMATION

The SampleBean sample component that is installed in the \Samples\ActiveX\SampleBean directory when you install the Microsoft SDK for Java is typically registered as a COM control. You build the sample with the supplied Makefile and then register the SampleBean sample component with the supplied Register.bat file. Packaging this Java bean as an OCX component makes distribution to client machines easier because the class files and the associated Type Library (TLB) file are packaged in a single self-registering OCX component.

The process shown in this article uses the VJREG tool to register the bean file and to generate an associated TLB file. The VJREG tool loads a class file and then searches the class file for attribute bytes that are written by the Microsoft Java Compiler (JVC) as a result of an @com.register tag in the Java source code file. The VJREG tool then creates registry entries based on these tags that register your Java COM component.

To add the @com.register tag to the SampleBean.java source file that is supplied with the SDK sample, follow these steps:
  1. Open SampleBean.java in a text editor.
  2. Locate the SampleBean class declaration:
    public class SampleBean extends Panel {  ....
    					
  3. Modify the class declaration by adding an @com.register comment tag immediately preceding the class declaration:
    /**
     * @com.register ( clsid=8E6DC8F3-A3EE-11d0-AFDF-0080C71F7993, typelib=A7303E93-969F-47CA-A14C-2F74AFA83488 )
     */ 
    public class SampleBean extends Panel {  ....
    					
    NOTE: The @com.register tag uses two, 32 hexadecimal digit numbers known as Globally Unique Identifiers (GUIDs):

    clsid=8E6DC8F3-A3EE-11d0-AFDF-0080C71F7993

    -and-

    typelib=A7303E93-969F-47CA-A14C-2F74AFA83488

  4. Replace the two GUIDs with the new GUIDs that you generate with the GUIDGEN tool.NOTE: GUIDs are unique numbers that are used to identify a component. You must not use the GUIDs listed in this article because your component then no longer has unique GUIDs. The GUIDGEN tool is included with the Microsoft SDK for Java, Visual J++ 6.0, and the Microsoft Windows Platform SDK.

  5. Run GUIDGEN and then click 4. Registry Format (ie. {xxxxxxxx-xxx ... xxx }) as the format for the new GUIDs.
  6. Copy the GUID to the clipboard and then paste the GUID in the @com.register clsid= tag.

    For example, if a new GUID is generated as follows:

    {F4704817-BB92-4585-96AD-02AE305D3EC7}

    in GUIDGEN, then replace the clsid= portion of the @com.register tag with:

    clsid=F4704817-BB92-4585-96AD-02AE305D3EC7NOTE: Remove the curly brackets to prevent compiler errors.

    In the following step, you generate a new GUID and then you repeat this step for the typelib= section of the @com.register tag:

  7. In GUIDGEN, click New GUID to generate a new GUID. Copy that GUID to the @com.register typelib= tag. Remember to delete the curly brackets. Alternatively, you can create a new Visual J++ project around the SampleBean source files as follows:

  8. After you create the new SampleBean Visual J++ project, in the Project Properties dialog box, click the COM Classes tab, and then select the SampleBean class from the check box.

    Notice the Visual J++ editor automatically adds an @com.register tag with two new GUIDs to the SampleBean class.
  9. Compile the Java source files and build the OCX. Use the following command lines with the Microsoft SDK for Java tools to complete this step:
    JVC /x- /nomessage SampleBean.java
    VJREG /typelib SampleBean.tlb SampleBean.class
    JEXEGEN /reg /d /out:SampleBean.ocx /base:. SampleBean.tlb *.class
    					
    In this case, JVC is the Microsoft Java compiler that compiles SampleBean.java into SampleBean.class. VJREG is the tool that generates the Type Library file SampleBean.tlb, and then registers SampleBean.class as a COM component. JEXEGEN is the tool that packages the SampleBean.class file, the SampleBeanEventListener.class file, and the SampleBean.tlb file in a self-registering COM component, the SampleBean.ocx file.

    Alternatively, Visual J++ can be used to build the COM component as follows:

  10. Open the Project Properties dialog box in your SampleBean project.
  11. Click the Output Format tab, and then select Enable Package in the check box.
  12. In the Packaging Type section, click COM DLL as the packaging type.
NOTE: A COM DLL file and an OCX file are the same types of files with different extensions. You can manually rename your .dll file generated by Visual J++ to have an .ocx extension. Or, you can add a step in the Project Properties dialog box, from the Custom tab, Post-build command(s) section to run the DOS rename command.

As soon as you build the .ocx file or the .dll file, you can then copy the file to the client computer and register the file with the REGSVR32 tool, as follows:
REGSVR32 SampleBean.ocx
				
You can then create the SampleBean control in Visual Basic or in any other ActiveX control container, such as the ActiveX Control Test Container that is installed with Microsoft Visual Studio .NET.

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbhowto KB318768