Vishnu
Vishnu

Reputation: 12293

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?

Upvotes: 1

Views: 375

Answers (1)

technomage
technomage

Reputation: 10069

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");
    }
    // ...
}

Upvotes: 1

Related Questions