views:

11

answers:

0

Hi, I have been trying to do the following thing in windows, using JNA: add a button to a window that I haven't created. First, however, I would like to create a WNDPROC listener so I know what messages are being sent.

The problem is that messages are not being sent/received in the message pump, while they should be (because windows constantly sends WNDPROC messages everywhere). Here is my code so far:

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
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.platform.win32.WinUser.HHOOK;
import com.sun.jna.platform.win32.WinUser.HOOKPROC;
import com.sun.jna.platform.win32.WinUser.MSG;
import winToJnaApi.CWMouseHook.MOUSEHOOKSTRUCT;
import winToJnaApi.User32Extended.POINT;


public class maintt
{
    final User32 USER32INST;
    static boolean isHooked;
    public Thread thrd;
    private HHOOK hhk;
    private static CallWndProc windowHook;
    public maintt(){
         USER32INST = User32.INSTANCE;
         isHooked = false;
         windowHook=hookTheWindow();
         Native.setProtected(true);
    }
    public void setWindowHook()
    {
        thrd = new Thread(new Runnable() {
            @Override
            public void run()
                {
                        try {
                            if(!isHooked)
                            {
                                hhk = USER32INST.SetWindowsHookEx(4, windowHook,Kernel32.INSTANCE.GetModuleHandle(null),0);
                                isHooked = true;
                                System.out.println("Window hook is set.");
                                // message dispatch loop (message pump)
                                MSG msg = new MSG();
                                while ((USER32INST.GetMessage(msg, null, 0, 0)) != 0)
                                {
                                    System.out.print("blaaaa");
                                    USER32INST.TranslateMessage(msg);
                                    USER32INST.DispatchMessage(msg);
                                    System.out.print(isHooked);
                                    if (!isHooked)
                                        break;
                                }
                                System.out.print("LOL!");
                            }
                            else
                                System.out.println("The Hook is already installed.");
                    }
                    catch (Exception e)
                    {
                        System.err.println("Caught exception in Hook!");
                    }
            }
        },"Named thread");
        thrd.start();
    }


    private interface CallWndProc extends HOOKPROC
    {
         LRESULT callback(int nCode, WPARAM wParam, CWPSTRUCT lParam);
    }
    private CallWndProc hookTheWindow()
    {
        return new CallWndProc()
        {
            @Override
            public LRESULT callback(int nCode, WPARAM wParam, CWPSTRUCT info)
            {

                if (nCode >= 0)
                {
                    {
                       System.out.print("callback received, yeeha");
                    }
                }
                return USER32INST.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
            }
        };
    }



    public static class CWPSTRUCT extends Structure
    {
         public static class ByReference extends CWPSTRUCT implements Structure.ByReference {};
         public  LPARAM lParam;
         public WPARAM wParam;
         public int message;
         public int  hwnd;
    }


}

I use 3.7 version of platform and jna, so I guess it is easily reproducable. When I set the hook, it never receives a in the message pump, and I suppose it should as the hook checks all threads for WNDPROC messages.

Any pointers how I can fix that?