kohsuke / com4j

Type-safe Java/COM binding
http://com4j.kohsuke.org/
BSD 2-Clause "Simplified" License
146 stars 78 forks source link

Using com4j to drive UIAutomationClient (WIndows Automation) #54

Closed mmarquee closed 8 years ago

mmarquee commented 8 years ago

I am using com4j for UIAutomationClient, and whilst it largely works, there are several methods that were either not extracted, or fail to work when implemented manually.

One of these is the following ..

@VTID(43) void GetCurrentBoundingRectangle( /[out, retval]_/ RECT retVal );

When I call this, I get the following execption ...

Exception in thread "main" com4j.IllegalAnnotationException: no default conversion available for class com.sun.jna.platform.win32.WinDef$RECT

So, is there a way around this - i.e. to add conversions ?

Thanks

Mark H

j4james commented 8 years ago

The hack I used to work around this was to pretend the return type was a GUID, since that is a supported type and uses the same number of bytes as a RECT. You need to declare the method as something like this:

    @VTID(43)
    @ReturnValue(type = GUID)
    Object Get_CurrentBoundingRectangle();

Then use reflection to access the v field of the returned object to get at the raw data. This is an array of 2 long ints, so you need to do a bit of shifting and masking to convert that to 4 regular ints with the coordinates.

mmarquee commented 8 years ago

I couldn't get this to work either way, so eventually I solved this by getting the handle of the element, and the calling user32.GetWindowRect(this.handle, rect), which seems to work OK. There are other examples of the calls to IUIAutomation not working, this was just a specific example.

j4james commented 8 years ago

In the project I was working on, I built the com4j library myself from the latest source (including the native c++ parts), so that might be why it's working for me and not for you (assuming you're using the old 2.1 release from 2014).

But if you're happy using GetWindowRect that's great - just bear in mind that a lot of elements won't necessarily have a window handle associated with them, so that solution won't work in all situations.

mmarquee commented 8 years ago

Yes, I've noticed that and it only seems to work for actual windows.