How to process WM_QUERYENDSESSION messages using JNA

I want to receive WM_QUERYENDSESSION messages in Java using JNA so that I can execute the shutdown method because Runtime # addShutdownHook (Thread) does not work on Windows [1]. I know this can be done since I saw that it is implemented using JNIWrapper, but I would like to have a JNA based solution.

JNIWrapper Solution

import java.io.File;
import java.io.RandomAccessFile;

import javax.swing.JFrame;

import com.jniwrapper.win32.Msg;
import com.jniwrapper.win32.ui.WindowMessage;
import com.jniwrapper.win32.ui.WindowMessageListener;
import com.jniwrapper.win32.ui.WindowProc;
import com.jniwrapper.win32.ui.Wnd;

public class ShutdownJNIWrapper {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Shutdown Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        Wnd frameHandle = new Wnd(frame);
        WindowProc frameWindowProc = new WindowProc(frameHandle);
        frameWindowProc.addMessageListener(new WindowMessageListener() {
            public boolean canHandle(WindowMessage windowMessage, boolean beforeWindowProc) {
                return windowMessage.getMsg() == Msg.WM_QUERYENDSESSION && beforeWindowProc;
            }

            public int handle(WindowMessage windowMessage) {
                doSomething();

                return 0;
            }

            private void doSomething() {
                final File file = new File("shutdown-jniwrapper.txt");

                try {
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");
                    raf.writeUTF("quit");
                    raf.close();
                } catch (Exception e) {
                }
            }
        });
        frameWindowProc.substitute();
    }
}

I tried to create my own User32 class with a WindowProc callback and the SetWindowLong method, which takes it as a parameter, but I get the following exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'SetWindowLong': The specified procedure could not be found.

at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:344)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:324)
at com.sun.jna.Library$Handler.invoke(Library.java:203)
at $Proxy0.SetWindowLong(Unknown Source)
at ShutdownJNA.main(ShutdownJNA.java:34)

Here is my User32 class:

import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary;

public interface MyUser32 extends StdCallLibrary { 

    public static final int WM_QUERYENDSESSION = 0x11;

    public static int GWL_WNDPROC = -4;

    interface WindowProc extends StdCallCallback {
        LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam);
    }

    int SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);

    int SetWindowLong(HWND hWnd, int nIndex, WindowProc dwNewLong);
};

and a class that is trying to put everything together:

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;


public class ShutdownJNA extends JPanel {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Shutdown Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        try {
            HWND hwnd = new HWND();
            hwnd.setPointer(Native.getComponentPointer(frame));

            MyUser32.WindowProc proc = new MyUser32.WindowProc() {
                public LRESULT callback(HWND wnd, int msg, WPARAM param, LPARAM param2) {
                    if (msg == MyUser32.WM_QUERYENDSESSION) {}

                    return new LRESULT(0);
                }
            };

            MyUser32 user32 = (MyUser32)Native.loadLibrary("user32", MyUser32.class); //$NON-NLS-1$
            user32.SetWindowLong(hwnd, MyUser32.GWL_WNDPROC, proc);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[1] Shutdown start is not performed when the application is started using javaw.exe

+3
source share
1

, jar JNA . jna.jar, platform.jar, . , DLL, JNA, (, user32.dll), .

+1

Source: https://habr.com/ru/post/1790053/


All Articles