views:

607

answers:

3

I'm trying to use the Java Access Bridge to get information about Swing components from inside a C++ app. However, none of the callbacks I register ever get called. I tried enuming the windows an then calling IsJavaWindow() on each handle, but it always returns false. Any ideas on why it apparently is not working?

I presume it's a problem with my app rather than the bridge installation because the demo Monkey and Ferret programs work, initializeAccessBridge() returns true, and the debugger reveals that the WindowsAccessBridge dll is loaded.

I'm using Java 6, update 13 on Windows Vista and I think version 2.0.1 of the access bridge.

JavaAccess::JavaAccess(void)
{
   using namespace std;

   BOOL isInitialized = initializeAccessBridge();
   if(isInitialized)
   {
      cout << "Bridge Initialized!" << endl;
   }
   else
   {
      cout << "Initialization failed!" << endl;
      return;
   }

   EnumWindows((WNDENUMPROC)EnumWndProc, NULL);

   SetJavaShutdown(OnJavaShutdown);
   SetFocusGained(OnFocusGained);
   SetMouseClicked(OnMouseClicked);
}

JavaAccess::~JavaAccess(void)
{
   shutdownAccessBridge();
}

void JavaAccess::OnJavaShutdown( long vmID )
{
   using namespace std;
   cout << "Java shutdown!" << endl;
}

void JavaAccess::OnFocusGained( long vmID, FocusEvent event, AccessibleContext context )
{
   using namespace std;
   cout << "Focus Gained!" << endl;

   ReleaseJavaObject(vmID, event);
   ReleaseJavaObject(vmID, context);
}

void JavaAccess::OnMouseClicked( long vmID, jobject event, jobject source )
{
   std::cout << "Mouse clicked!" << std::endl;

   ReleaseJavaObject(vmID, event);
   ReleaseJavaObject(vmID, source);
}

BOOL CALLBACK JavaAccess::EnumWndProc( HWND hwnd, LPARAM lparam )
{
   if (IsJavaWindow(hwnd))
   {
      std::cout << "Found Java Window!" << std::endl;
      return FALSE;
   }
   else
   {
      std::cout << "Still looking" << std::endl;
      return TRUE;
   }
}

All of the callbacks are static functions.

A: 

Are you sure the OnJavaShutdown() is static? I believe the declaration should be

static oid JavaAccess::OnJavaShutdown( long vmID )
David Rabinowitz
+3  A: 

I have been fighting this one as well, and have just found a solution that actually makes sense. I ended up having to build a debug version of the WindowsAccessBridge.dll and used the debugger to step into it to watch what was happening.

  • The call to 'initializeAccessBridge' REQUIRES you to have an active windows message pump.

Inside 'initializeAccessBridge', it (eventually) creates a hidden dialog window (using CreateDialog). Once the dialog is created, it performs a PostMessage with a registered message. The JavaVM side of the access bridge responds to this message, and posts back another message to the dialog that was created (it appears to be a 'hello' type handshake between your app and the java VM). As such, if your application doesn't have an active message pump, the return message from the JavaVM never gets received by your app.

This is important as until this message is received, the bridge is never properly initialized and as such all calls to 'IsJavaWindow' fail (internally, the bridge initializes an internal structure once the message is received -- as such, no active message pump, no initializing). I'm guessing that this is why you never receive callback messages as well.

Not only that, but you must call initializeAccessBridge at a point where the message pump can process messages before you can call IsJavaWindow.

This is why JavaFerret and JavaMonkey work -- they initialize at startup, and then enumerate on response to a menu message, well after the bridge has received the initialization message via the message pump.

The way I was able to solve this in my MFC dialog app (and our MFC-based application), is to make sure that you call 'initializeAccessBridge' at a point such that the built-in MFC message pump can push the 'hello' message back to this hidden dialog BEFORE you use it. In the simple MFC dialog case, it meant calling initializeAccessBridge in OnInitDialog, and calling the enum procedure in response to a button call (for example). If you want the enum to occur as soon as the dialog appears, you could use a timer to fire (eg. 10ms) after the OnInitDialog completes to allow processing of the initialization message.

If you are planning to use this in a console app, you will need to write your own custom message pump to handle the initialization message.

Anyway, I hope this is clear enough! Whilst there is no way to know whether this is the 'correct' way (other than to pay for a Sun engineer to tell us), it definitely solved my problem.

Cheers -- Darren.

oh. and btw I found an obscure Sun page that mentioned something about the AccessBridge only working for awt-based java apps (but given that Sun hasn't updated any documentation since 2004 this may have changed). I'm not a java programmer -- for testing I grabbed a number of free Java applications (as well as the ones that came with the jdk) and tried out my test application. It worked for all of the ones I tried -- YMMV. Good luck!

Darren Ford
I was beginning to suspect that it required a message pump, and I like your explanation why. I just modified my test and it works now. Here are some bounty points!
James
A: 

Hi James,

Could you please let me know if I want to use Java Acess Bridge API in VBA,

I am getting following error, while executing the code for

"Bad DLL calling Convension"

isjavawindow(Hwnd of Java Window).

Could you please help me out.

Dim Var_g_HwndRec As Long Dim lb As Long Var_g_HwndRec = FindWindow(vbNullString, Tile of Java Window) lb = LoadLibrary("C:\WINDOWS\system32\WindowsAccessBridge.dll") MsgBox isJavaWindow(Var_g_HwndRec)

best regards

Ashish