javac#interopjava-access-bridge

Java Application Window not recognized as java window in C# InterOp


I am using Java Access Bridge API with interop in C#. When trying to create a new AccessibleWindow with the hwnd obtained from the user32.dll method GetForegroundWindow(), it does not recognize the window as a java window, returning null. IsJavaWindow() returns false, but calling it a second time returns true. I tested this out with the example "SwingSet2" application.

        public void Initialize()
        {
            if(!Initialized)
            {
                accessBridge = new AccessBridge();
                var hwnd = WindowsNativeMethods.GetForegroundWindow();
                var window = accessBridge.CreateAccessibleWindow(hwnd);
                window.AccessBridge.Initialize();
                window.AccessBridge.Functions.GetAccessibleContextFromHWND(hwnd, out vmId, out mainContext);
                Initialized = true;      
            }
        }

I am also using code from this repo: Google Access Bridge


Solution

    1. Initialize() or the initialization code in general needs to called in a UI thread or a message pumping thread.
    2. Using the IntPtr from GetForegroundWindow() or GetActiveWindow() always returns false in IsJavaWindow(), but using FindWindow() works from user32.dll's methods.