Home : Running the COM Accessing Java Early Binding Example (with Events) Using a VC++ Client
Q71119 - HOWTO: Running the COM Accessing Java Early Binding Example (with Events) Using a VC++ Client

Running the COM Accessing Java Early Binding Example (with Events) Using a Visual C++ Client

 

The attachment, EarlyBindExample.zip, contains the COM accessing Java early binding example from the documentation, but also includes a VC++ client in addition to the VB client. To run the example with a VC++ client, do the following:

  1. Download the attachment and extract the files to your hard disk.
  2. Follow the instructions in the readme.txt file to setup and run the example.
  3. To run the VC++ client, open the project file in the cxx client directory.
  4. Select the Build->Rebuild All option from the main menu.
  5. After the project is compiled and linked, run the executable by selecting Build->Execute Event.exe

 

If you click the button, you will see the following message box appear:

 

Brief Explanation

What follows is a brief explanation of the VC++ code and how the event gets handled. If you look at the Event Example project, you will see the following classes in the right pane:

EventSink
ExeModule

ExeModule is the default class that gets created by the ATL wizard whenever an ATL Project is created. In this case, the COM class will reside in an exe. EventSink is a class that was created to handle events. Sinks are used extensively in the COM event model, however explaining the COM event model would be outside the scope of this example.

EventSink has one method and one constructor. If you look at the header file for EventSink (EventSink.h), you will see that EventSink implements IDispEventSimpleImpl and IDispatchImpl. This simply means that IEventSink (the COM interface of EventSink) derives from IDispatch and supports events. Also, by implementing the above two interfaces, all of the IDispatch methods are automatically implemented (this is all taken care of by ATL).

The one method that EventSink has is Invoke. This is the Invoke of IDispatch, and it is implemented so that a message box can be displayed whenever an event occurs. In the COM object model, the invoke method of the registered event sink is called whenever an event occurs.

If you direct you attention to the _tWinMain() method in Events.cpp, you will see the block of code that actually creates the Java Frame, adds the Java Button to it, and then makes all the necessary to calls to set up COM events. To set up the events, the falling steps are taken:
//Here the ConnectionPointContainer is queried from the Java Button
st = pJavaAwtButton->QueryInterface(IID_IConnectionPointContainer,
                                    (void **) &pCPC);

//Here the ConnectionPoint that corresponds to the EventActionListener is found
st = pCPC->FindConnectionPoint(DIID__JavaAwtEventActionListener, &pCP);

//Here an instance of the EventSink class is created
st = CoCreateInstance(CLSID_EventSink,
                      0,
                      CLSCTX_SERVER,
                      DIID__JavaAwtEventActionListener,
                      (void**) &pSink);

//Here the Connection point is notified that an Event sink exists, so that the events that occur can trigger a call to Invoke()
st = pCP->Advise(reinterpret_cast (pSink), &dwCookie);

Please feel free to change the example according to your needs (and for experimentation). COM events are very difficult to get working, however, J-Integra has been designed so that the integration is seamless.

Related Articles
No Related Articles Available.

Article Attachments
EarlyBindExample.zip

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 2/20/2007.
Last Modified by J-Integra KB Admin.
Article has been viewed 11577 times.
Rated 4 out of 10 based on 6 votes.
Print Article
Email Article