Java: JNA SystemParametersInfo parameters type

十年热恋 提交于 2019-12-11 07:36:42

问题


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);


来源:https://stackoverflow.com/questions/5132431/java-jna-systemparametersinfo-parameters-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!