Java/COM Apartment Sample

This sample is located in \Samples\Com\JCOMApartment.

Note   To use this sample, it is recommended that you have Microsoft® Visual C++® version 5.0 or higher installed.

Description
Using the Sample
Key Project Files
Technologies Demonstrated

Description

This sample, OLEAPT, demonstrates OLE apartment model threading. The term apartment describes a thread with a message queue that supports OLE/COM objects. Objects within an apartment are reentrant only in the traditional Microsoft® Windows® sense, similar to single-threaded OLE. Therefore, operations that yield to the message queue can cause further messages to be sent to any objects within the apartment. Apartment model threading allows more than one apartment to exist (previously there was only one: the main application thread).

The client creates an instance of the COM object, CObject, that is served by the COMApartment server. The CObject aggregates the Java/COM object Pinger, which contains the methods used in this sample. The Java/COM object's ping method returns the number of times the method has been called and the Windows ThreadID that the COM object is serviced from.

The title of the Client window displays the Windows thread ID. Each Instance of the client simply creates an instance of its worker COM object in the server. When it shuts down, it releases its reference to the object.

The server registers a class factory on the main application thread. When requests arrive from clients to create instances of the class, the server class factory creates an instance of the CObject on one of its apartment threads. The server window is updated with the number of COM objects serviced by each of its apartment threads.

The only mechanism used here (Microsoft® Windows NT® version 3.5 or later) is the ability to call CoInitialize in multiple threads.

This sample can be compiled as Unicode or ANSI so that it can run on Windows NT version 3.51 or later, Microsoft® Windows® 95, and Microsoft® Windows® 98.

Using the Sample

To compile the sample

Use Nmake.exe to compile the makefile in the base directory \Samples\Com\JCOMApartment. Type the following command:

Nmake

To install the sample

Use the following command from the base directory \Samples\Com\Marshal to register the proxy/stub DLL on the client and server computers:

Regsvr32 proxystub.dll

To uninstall the sample

Use the following command from the base directory to remove the proxy/stub DLL from the registry on the client and server computers:

Regsvr32 /u proxystub.dll

To run the sample

  1. Run COMApartment.exe to start the server.

  2. Use the following command to create clients:

    jview sample.apartment.Client

Key Project Files

IPing.idl

This file contains the IDL (Interface Definition Language) definition of the simple pinger COM object. This object implements the custom IPing interface that contains the single method used in this sample.

oleapt.Cpp

This C application acts as the server for the Java/COM Single-Threaded Apartment (STA) objects. The application displays a window that lists the threads and the number of COM objects hosted on each thread.

proxystub.def

This file contains the exported DLL functions required for registering a COM object using Regsvr32.exe. It is used for building the proxystub DLL.

Client.java

This client application displays a frame, creates an instance of the Java/COM object, and presents a button for invoking the object's ping method.

Pinger.java

This class implements the Java/COM object with the custom IPing interface.

Technologies Demonstrated

COM

This sample shows how to:

Native Code

© 1999 Microsoft Corporation. All rights reserved. Terms of use.