Java: JNA SystemParametersInfo parameters type

2019-08-03 22:03发布

问题:

I only started to experiment with JNA, and stuck trying to call this function w/o exception
Native prototype:

BOOL WINAPI SystemParametersInfo(
  __in     UINT uiAction,
  __in     UINT uiParam,
  __inout  PVOID pvParam,
  __in     UINT fWinIni
);

I've suggested such JNA equivalent:

public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

        boolean SystemParametersInfo(
                UINT_PTR uiAction,
                UINT_PTR uiParam,
                Pointer pvParam,
                UINT_PTR fWinIni
        );
        public static final int SPI_GETCLEARTYPE = 0x1048;
        public static final int SPI_GETDESKWALLPAPER = 0x0073;
}

And the question is how to call it with different pvParam types using pointer?
for example SPI_GETCLEARTYPE (where it's BOOL) and SPI_GETDESKWALLPAPER (where it's char[])

回答1:

Solved myself so the mapping:

    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class,
            new HashMap<Object, Object>() {
            {
                put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
                put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE);
            }
        });
        public static final int SPI_GETDESKWALLPAPER = 0x0073;
        public static final int SPI_GETSCREENSAVERRUNNING = 114;
        boolean SystemParametersInfo(
                int uiAction,
                int uiParam,
                Pointer pvParam,
                int fWinIni
        );
    }

and the usage:

IntByReference intPtr = new IntByReference();
//that's the place where i'm stuck trying to initialize with Pointer constructor
Pointer ptr = new Memory(Pointer.SIZE * 256);
User32.INSTANCE.SystemParametersInfo(User32.SPI_GETSCREENSAVERRUNNING, 0,intPtr.getPointer(), 0);
User32.INSTANCE.SystemParametersInfo(User32.SPI_GETDESKWALLPAPER,256, ptr, 0);