Skip to content

Commit

Permalink
Addition win32 api : SendMessage, GetActiveWindow, COPYDATASTRUCT and…
Browse files Browse the repository at this point in the history
… a few constants + a demo application (#774)

* Added a few mapping of win32 api : SendMessage and GetActiveWindow.
Added some constants and structures (COPYDATASTRUCT and CWPSTRUCT from the win 32 api.
Added a Win32WindowMessagesDemo to demonstrate complex messages exchange through WM_COPYDATA and with a hooked winproc.

* Corrections done to take into account, the comment of the pull request : cf #774
* Replaced Win32WindowMessagesDemo.java by User32WindowMessagesTest.java
* changed data types to int where appropriate
* added pull request in CHANGES.md

* Addition of forgotten test class : User32WindowMessagesTest.java

* Correction of compilation error of unit test/com/sun/jna/platform/win32/User32WindowMessagesTest.java detected by travis with jdk 6.

* Merged from jna master to be up to date with 4.4 release.
Corrected comments and license info.
  • Loading branch information
cnico authored and matthiasblaesing committed Mar 19, 2017
1 parent 047b7aa commit 32ea99a
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Release 4.5.0 (Next release)

Features
--------
* [#774](https://github.com/java-native-access/jna/pull/774): Addition win32 api : SendMessage, GetActiveWindow, COPYDATASTRUCT and a few constants + a demo application - [@cnico](https://github.com/cnico).

Bug Fixes
---------
Expand Down
40 changes: 40 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/User32.java
Original file line number Diff line number Diff line change
Expand Up @@ -2445,4 +2445,44 @@ HANDLE SetWinEventHook(int eventMin, int eventMax, HMODULE hmodWinEventProc, Win
* error information, call GetLastError.</p>
*/
public int RegisterClipboardFormat(String formatName);

/**
* Retrieves the window handle to the active window attached to the
* calling thread's message queue.
*
* @return Type: HWND The return value is the handle to the active
* window attached to the calling thread's message queue. Otherwise,
* the return value is NULL.
*/
public HWND GetActiveWindow();

/**
* Sends the specified message to a window or windows.
* The SendMessage function calls the window procedure for the specified window and
* does not return until the window procedure has processed the message.
*
* To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function.
* To post a message to a thread's message queue and return immediately,
* use the PostMessage or PostThreadMessage function.
*
* @param hWnd A handle to the window whose window procedure will receive the message.
* If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all
* top-level windows in the system, including disabled or invisible unowned windows,
* overlapped windows, and pop-up windows; but the message is not sent to child windows.
* Message sending is subject to UIPI. The thread of a process can send messages
* only to message queues of threads in processes of lesser or equal integrity level.
*
* @param msg The message to be sent.
* For lists of the system-provided messages, see System-Defined Messages.
* @param wParam Additional message-specific information.
* @param lParam Additional message-specific information.
*
* @return The return value specifies the result of the message processing; it depends on the message sent.
*
* Two classic usage :
* - with a WM_USER+x msg value : wParam and lParam are numeric values
* - with a WM_COPYDATA msg value : wParam is the length of the structure and lParam a pointer to a COPYDATASTRUCT
*/
LRESULT SendMessage(HWND hWnd, int msg, WPARAM wParam, LPARAM lParam);

}
148 changes: 142 additions & 6 deletions contrib/platform/src/com/sun/jna/platform/win32/WinUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,31 @@ protected List<String> getFieldOrder() {
}
}


/**
* Contains data to be passed to another application by the WM_COPYDATA message.
*/
public class COPYDATASTRUCT extends Structure {

public COPYDATASTRUCT() {
super();
}

public COPYDATASTRUCT(Pointer p) {
super(p);
//Receiving data and read it from native memory to fill the structure.
read();
}

public ULONG_PTR dwData;
public int cbData;
public Pointer lpData;

protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "dwData", "cbData", "lpData" });
}
}

public class FLASHWINFO extends Structure {
public int cbSize = size();
public HANDLE hWnd;
Expand Down Expand Up @@ -483,6 +508,7 @@ protected List<String> getFieldOrder() {
int MOD_WIN = 0x0008;

int WH_KEYBOARD = 2;
int WH_CALLWNDPROC = 4;
int WH_MOUSE = 7;
int WH_KEYBOARD_LL = 13;
int WH_MOUSE_LL = 14;
Expand All @@ -491,6 +517,31 @@ public class HHOOK extends HANDLE { }

public interface HOOKPROC extends StdCallCallback { }

/**
* Defines the message parameters passed to a WH_CALLWNDPROC hook procedure, CallWndProc.
*/
public class CWPSTRUCT extends Structure {

public CWPSTRUCT() {
super();
}

public CWPSTRUCT(Pointer p) {
super(p);
//Receiving data and read it from native memory to fill the structure.
read();
}

public LPARAM lParam;
public WPARAM wParam;
public int message;
public HWND hwnd;

protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "lParam", "wParam", "message", "hwnd"});
}
}

/**
* The WM_PAINT message is sent when the system or another application makes
* a request to paint a portion of an \ application's window.
Expand Down Expand Up @@ -560,6 +611,11 @@ public interface HOOKPROC extends StdCallCallback { }
* usually of the form WM_USER+x, where x is an integer value.
*/
int WM_USER = 0x0400;

/**
* An application sends the WM_COPYDATA message to pass data to another application.
*/
int WM_COPYDATA = 0x004A;

int WM_KEYUP = 257;
int WM_SYSKEYDOWN = 260;
Expand Down Expand Up @@ -805,11 +861,97 @@ protected List<String> getFieldOrder() {
*/
int GW_ENABLEDPOPUP = 6;

/**
* If the calling thread and the thread that owns the window are attached
* to different input queues, the system posts the request to the thread
* that owns the window. This prevents the calling thread from blocking
* its execution while other threads process the request.
*/
int SWP_ASYNCWINDOWPOS = 0x4000;

/**
* Prevents generation of the WM_SYNCPAINT message.
*/
int SWP_DEFERERASE = 0x2000;

/**
* Draws a frame (defined in the window's class description) around the window.
*/
int SWP_DRAWFRAME = 0x0020;

/**
* Applies new frame styles set using the SetWindowLong function. Sends
* a WM_NCCALCSIZE message to the window, even if the window's size is
* not being changed. If this flag is not specified, WM_NCCALCSIZE is
* sent only when the window's size is being changed.
*/
int SWP_FRAMECHANGED = 0x0020;

/**
* Hides the window.
*/
int SWP_HIDEWINDOW = 0x0080;

/**
* Does not activate the window. If this flag is not set, the window is
* activated and moved to the top of either the topmost or non-topmost
* group (depending on the setting of the hWndInsertAfter parameter).
*/
int SWP_NOACTIVATE = 0x0010;

/**
* Discards the entire contents of the client area. If this flag is not
* specified, the valid contents of the client area are saved and copied
* back into the client area after the window is sized or repositioned.
*/
int SWP_NOCOPYBITS = 0x0100;

/**
* Retains the current position (ignores X and Y parameters).
*/
int SWP_NOMOVE = 0x0002;

/**
* Does not change the owner window's position in the Z order.
*/
int SWP_NOOWNERZORDER = 0x0200;

/**
* Does not redraw changes. If this flag is set, no repainting of any kind
* occurs. This applies to the client area, the nonclient area (including
* the title bar and scroll bars), and any part of the parent window
* uncovered as a result of the window being moved. When this flag is
* set, the application must explicitly invalidate or redraw any parts
* of the window and parent window that need redrawing.
*/
int SWP_NOREDRAW = 0x0008;

/**
* Same as the SWP_NOOWNERZORDER flag.
*/
int SWP_NOREPOSITION = 0x0200;

/**
* Used by User32.SetWindowPos. <br>
* Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
*/
int SWP_NOSENDCHANGING = 0x0400;

/**
* Retains the current size (ignores the cx and cy parameters).
*/
int SWP_NOSIZE = 0x0001;

/**
* Retains the current Z order (ignores the hWndInsertAfter parameter).
*/
int SWP_NOZORDER = 0x0004;

/**
* Displays the window.
*/
int SWP_SHOWWINDOW = 0x0040;

/**
* Minimizes the window.
*/
Expand Down Expand Up @@ -932,12 +1074,6 @@ protected List<String> getFieldOrder() {
*/
int BS_LEFTTEXT = 0x00000020;

/**
* Used by User32.SetWindowPos. <br>
* Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
*/
int SWP_NOSENDCHANGING = 0x0400;


/**
* Contains information about a simulated message generated by an input
Expand Down
25 changes: 25 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/User32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,29 @@ public void testGetClassLong() {
assertEquals("GetClassLong result should be 0", 0, result);
assertEquals("GetLastError should be ERROR_INVALID_WINDOW_HANDLE.", WinError.ERROR_INVALID_WINDOW_HANDLE, Native.getLastError());
}

@Test
public void testGetActiveWindow() {

HWND result = User32.INSTANCE.GetActiveWindow();
assertNull("GetActiveWindow result should be null", result);
assertEquals("GetLastError should be ERROR_SUCCESS.", WinError.ERROR_SUCCESS, Native.getLastError());
}

@Test
public void testSendMessage() {
DesktopWindow explorerProc = getWindowByProcessPath("explorer.exe");

assertNotNull(explorerProc);

LRESULT result = User32.INSTANCE
.SendMessage(explorerProc.getHWND(),
WinUser.WM_USER,
new WPARAM(124),
new LPARAM(12345));

assertNotEquals(0, result);

}

}
Loading

0 comments on commit 32ea99a

Please sign in to comment.