Search code examples
javawinapijna

Why does my call to winapi GetWindowPlacement fail (using JNA)?


These are the winapi methods

BOOL WINAPI SetWindowPlacement(
  _In_       HWND            hWnd,
  _In_ const WINDOWPLACEMENT *lpwndpl
);
typedef struct tagWINDOWPLACEMENT {
  UINT  length;
  UINT  flags;
  UINT  showCmd;
  POINT ptMinPosition;
  POINT ptMaxPosition;
  RECT  rcNormalPosition;
} WINDOWPLACEMENT, *PWINDOWPLACEMENT, *LPWINDOWPLACEMENT;

My Java code:-

class WINDOWPLACEMENT{
   public int length;
   public int flags;
   public int showCmd;
   public POINT    ptMinPosition;
   public POINT    ptMaxPosition;
   public RECT     rcNormalPosition;
}
WINDOWPLACEMENT wind = new WINDOWPLACEMENT();
User32Extra.INSTANCE.GetWindowPlacement(hwndLSM, wind);

The error is

java.lang.IllegalArgumentException: Unsupported argument type jna.extra.WINDOWPLACEMENT at parameter 1 of function GetWindowPlacement

How to use GetWindowPlacement/SetWindowPlacement with JNA?


Solution

  • java.extra.WINDOWPLACEMENT must extend com.sun.jna.Structure and properly implement getFieldOrder().

    EDIT

    Setting length in the constructor, and getFieldOrder() definition:

    public class WINDOWPLACEMENT extends Structure {
        public WINDOWPLACEMENT() {
            this.length = size();
        }
        public List getFieldOrder() {
            return Arrays.asList("length", "flags", "showCmd", "ptMinPosition", "ptMaxPosition", "rcNormalPosition");
        }
        // ...
    }