Extracting WindowMessage in JNA Callback method

488 views
Skip to first unread message

Marc

unread,
Aug 10, 2012, 8:37:38 AM8/10/12
to jna-...@googlegroups.com
Hi,
I'm sending a message (basically command line args) from c++ Application to a Java application using window message. I would need to extract the message(i.e command line arguments) in Java using JNA in the call back method. I have heard of something like custom message but I'm finding bit difficult to achieve this. Could anyone help me on how send the message and extract it as string in Java.
Thank you.

C++ Code:
Find the application window by name and send message to the application.

       HWND hwndList = NULL;
        EnumWindows(EnumWindowsProc, (LPARAM)hwndList);
        if(_found)
        {
            ShowWindow(_hwnd, SW_RESTORE);
            int check = SetForegroundWindow(_hwnd);

string customeMessage= " my arguments";

                LRESULT res = ::SendMessage(_hwnd, WM_USERMESSAGE, wParam, customeMessage);
      //HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam
Could anyone help me here to wrap the custome message and send through SendMessageAPI ???


            DWORD err = GetLastError();
         }

BOOL CALLBACK EnumWindowsProc(HWND hWnd, long lParam) {
    char buff[255];
    bool _found=false;
    if (IsWindowVisible(hWnd)) {
        string ourWindowName = "applicaton_name";
        GetWindowTextA(hWnd, (LPSTR) buff, 254);
        string windowName = buff;
        int res = windowName.compare(ourWindowName);
        if (res == 0)
        {
            _hwnd = hwnd;
            _found = true;
        }       
        printf("%S\n", buff);
    }
    return TRUE;
}

Java Code:
=======
 
public interface INativeLibrary extends StdCallLibrary
{
   public final int WM_USERMESSAGE= 0xC001;

   static Map UNICODE_OPTIONS = new HashMap() {
      {
          put("type-mapper", W32APITypeMapper.UNICODE);
          put("function-mapper", W32APIFunctionMapper.UNICODE);
      }
  };


   final INativeLibrary instance = (INativeLibrary) Native.loadLibrary("user32",INativeLibrary.class, UNICODE_OPTIONS);
   static int GWL_WNDPROC = -4;
   LONG_PTR SetWindowLong(int hWnd, int nIndex, WindProc wndProc) throws LastErrorException;

     
public class WindowMessageListener
{
public void initialize()
   {
   
      final List<WindowInformation> inflList = new ArrayList<WindowInformation>()
;
      final List<Integer> order = new ArrayList<Integer>();
      int top = INativeLibrary.instance.GetTopWindow(0);
      String applicationName="application name";
      System.out.println(" before entering" + top);
      while (top!=0) {
          order.add(top);
          top = INativeLibrary.instance.GetWindow(top, INativeLibrary.GW_HWNDNEXT);
      }
      INativeLibrary.instance.EnumWindows(new IWinEnumProc()
      {
          public boolean callback(int hWnd, int lParam)
          {
          System.out.println(" hwnd = " +hWnd);
             if (INativeLibrary.instance.IsWindowVisible(hWnd)) {
                  byte[] buffer = new byte[1024];
                  INativeLibrary.instance.GetWindowTextA(hWnd, buffer, buffer.length);
                  String title = Native.toString(buffer);
                  inflList.add(new WindowInformation(hWnd, title));
//                  System.out.println(title);
          }
          return true;
      }
      }, 0);
     
      for (WindowInformation w : inflList) {
         System.out.println(w);
        
         if(w.title.contains(applicationName))
         {
         
            INativeLibrary.instance.SetWindowLong(w.hwnd, INativeLibrary.GWL_WNDPROC, procedure);
            break;
         }
         }
     
      //Set the WndProc function to use our callback listener instead of the window's one.
       
   }
      return new LRESULT(0);
   }



public class WindProc implements IWindProc
{

   @Override
   public LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam)
   {
   
      System.out.println(+ uMsg);
     
      if (uMsg == INativeLibrary.WM_USERMESSAGE) {

// Here I need to extract the message to string.

      }


Any help or pointers here would be of great help!.

Thank you,

-Marc

}

Daniel Doubrovkine

unread,
Aug 10, 2012, 12:08:24 PM8/10/12
to jna-...@googlegroups.com
Can you achieve this in pure C++? That's what I would do first. Then rewrite the "client" code with JNA.
--

dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg


Reply all
Reply to author
Forward
0 new messages