0

我有一个使用 java 本机访问的简单程序,它可以在 32 位 java 上完美运行,但不能在 64 位 jvm 上运行。

在 64 位 os 和 jvm 上的程序输出如下

Day of the Week 4
Year :  2013
Processor Type : 8664
System Metrics : 960
Setting HWPointer
Getting current wnd proc ptr
Exception in thread "main" com.sun.jna.LastErrorException: [1413] Invalid index.
    at com.sun.jna.Native.invokeLong(Native Method)
    at com.sun.jna.Function.invoke(Function.java:386)
    at com.sun.jna.Function.invoke(Function.java:315)
    at com.sun.jna.Library$Handler.invoke(Library.java:212)
    at com.sun.proxy.$Proxy2.GetWindowLongW(Unknown Source)
    at SutdownJna.main(SutdownJna.java:71)

这是代码。

    public class SutdownJna {

    public interface User33 extends User32 {

//        static int GWL_WNDPROC = -4;
        User33 INSTANCE = (User33) Native.loadLibrary("user32", User33.class,
                W32APIOptions.DEFAULT_OPTIONS);

        interface WNDPROC extends StdCallLibrary.StdCallCallback {

            LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
        }

        LONG_PTR GetWindowLongW(HWND hWnd, int nIndex) throws LastErrorException;

        LRESULT CallWindowProcW(LONG_PTR proc, HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);

        LONG_PTR SetWindowLongW(HWND hWnd, int nIndex, User33.WNDPROC wndProc);
    }
    public static final int WM_QUERYENDSESSION = 0x11;

    public static void main(String[] args) {
        User33.WNDPROC wndProcCallbackListener = null;

        final JFrame frame = new JFrame("Shutdown Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        Kernel32 INSTANCE = (Kernel32) Native
                .loadLibrary("Kernel32", Kernel32.class);

        SYSTEMTIME time = new SYSTEMTIME();
        INSTANCE.GetSystemTime(time);
        System.out.println("Day of the Week " + time.wDayOfWeek);
        System.out.println("Year :  " + time.wYear);

        SYSTEM_INFO systeminfo = new SYSTEM_INFO();
        INSTANCE.GetSystemInfo(systeminfo);
        System.out.println("Processor Type : " + systeminfo.dwProcessorType);
        System.out.println("System Metrics : " + User32.INSTANCE.GetSystemMetrics(1));
        HWND hwnd = new HWND();
        System.out.println("Setting HWPointer");
        hwnd.setPointer(Native.getComponentPointer(frame));
        System.out.println("Getting current wnd proc ptr");
        final LONG_PTR prevWndProc = User33.INSTANCE.GetWindowLongW(hwnd, User33.GWL_WNDPROC);
        System.out.println("Creating new wnd proc ptr");
        User33.WNDPROC proc = new User33.WNDPROC() {
            public LRESULT callback(HWND wnd, int msg, WPARAM param, LPARAM param2) {
                System.out.println("Received msg : " + msg);
                if (msg != WM_QUERYENDSESSION) {
                    return User33.INSTANCE.CallWindowProcW(prevWndProc, wnd, msg, param, param2);
                } else {
                    return new LRESULT(0);
                }
            }
        };

        System.out.println("Setting new Proc Handler " + proc);
        User33.INSTANCE.SetWindowLongW(hwnd, User33.GWL_WNDPROC, proc);

    }
}

测试是在 windows 7 32 位和 windows 7 64 位上进行的。

4

1 回答 1

1

请参阅GetWindowLongPtr. 您正在检索一个指针值,该值在 64 位系统上为 64 位。不清楚GetWindowLongPtr是宏还是 32 位上的函数(UnsatisfiedLinkError如果它只是一个宏,你会得到一个)。

于 2013-07-11T11:44:12.853 回答