Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add definitions for Wincon.GetConsoleScreenBufferInfo() #1190

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f4bb4a7
Add definitions for Wincon.GetConsoleScreenBufferInfo()
rednoah May 10, 2020
2757af3
Added testGetConsoleScreenBufferInfo()
rednoah May 11, 2020
b3c10e8
Change toString() to be more in line with other toString() methods
rednoah May 11, 2020
aad550b
Activate test case and make it work for both interactivate and non-in…
rednoah May 11, 2020
1d76826
Added Windows Console API bindings:
rednoah May 13, 2020
1dfdd22
[checkstyle] [ERROR] /home/travis/build/java-native-access/jna/contri…
rednoah May 13, 2020
92a6d5a
Prefer native types over WinDef types
rednoah May 14, 2020
061b826
Fix statvfs filesystem - it is unstable in docker environments
matthiasblaesing May 10, 2020
a6a2b43
Merge pull request #1188 from matthiasblaesing/unittest-docker
matthiasblaesing May 11, 2020
781ca72
Remove invalid GetTcpStatistics test
dbwiddis May 10, 2020
db60d47
Merge pull request #1189 from dbwiddis/tcptest
matthiasblaesing May 11, 2020
889f5dd
Add getTokenPrimaryGroup to Advapi32Util
dbwiddis May 13, 2020
aafe9a5
Only fetch the latest ant version in Travis script
dbwiddis May 15, 2020
57b5dc8
Merge pull request #1193 from dbwiddis/patch-1
matthiasblaesing May 15, 2020
f9f835c
Merge branch 'master' of https://github.com/java-native-access/jna in…
dbwiddis May 15, 2020
c6c7a48
Merge pull request #1191 from dbwiddis/primarygroup
matthiasblaesing May 15, 2020
54f990a
Add toString to classes extending ByReference
dbwiddis Apr 25, 2020
8fd977f
Merge branch 'pr-1182'
matthiasblaesing May 15, 2020
c6f2e5a
ByReferencePlatformToStringTest.java was not correctly merged
matthiasblaesing May 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist: trusty
language: java

install:
- export APACHE_ANT_BASE=$(curl http://apache.mirror.iphh.net/ant/binaries/ | grep "apache-ant-1.9..*-bin.tar.gz" | sed 's/.*href="\(.*\)-bin.tar.gz".*/\1/g')
- export APACHE_ANT_BASE=$(curl http://apache.mirror.iphh.net/ant/binaries/ | grep "apache-ant-1.9..*-bin.tar.gz" | tail -1 | sed 's/.*href="\(.*\)-bin.tar.gz".*/\1/g')
- 'echo "Apache Ant ARCHIVE: $APACHE_ANT_BASE"'
- '[ "${TRAVIS_OS_NAME}" = "linux" ] && wget http://apache.mirror.iphh.net/ant/binaries/$APACHE_ANT_BASE-bin.tar.gz && tar xzf $APACHE_ANT_BASE-bin.tar.gz && sudo mv $APACHE_ANT_BASE /usr/local/$APACHE_ANT_BASE && sudo rm -f /usr/local/ant && sudo ln -s /usr/local/$APACHE_ANT_BASE /usr/local/ant && sudo ln -s /usr/local/$APACHE_ANT_BASE/bin/ant /usr/local/bin/ant || true'
- '[ "${TRAVIS_OS_NAME}" = "osx" ] && brew update || true'
Expand Down
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Features
* [#1168](https://github.com/java-native-access/jna/pull/1168): Add `c.s.j.p.win32.Kernel32#SetProcessAffinityMask` - [@dbwiddis](https://github.com/dbwiddis).
* [#1169](https://github.com/java-native-access/jna/issues/1169): Wait for process in getLinuxLdPaths - [@rdesgroppes](https://github.com/rdesgroppes).
* [#1178](https://github.com/java-native-access/jna/pull/1178): Add `c.s.j.p.win32.IPHlpAPI#GetTcpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetUdpStatistics`, `c.s.j.p.win32.IPHlpAPI#GetTcpStatisticsEx` and `c.s.j.p.win32.IPHlpAPI#GetUdpStatisticsEx` - [@dbwiddis](https://github.com/dbwiddis).
* [#1191](https://github.com/java-native-access/jna/pull/1191): Add `c.s.j.p.win32.Advapi32Util#getTokenPrimaryGroup` - [@dbwiddis](https://github.com/dbwiddis).
* [#1182](https://github.com/java-native-access/jna/pull/1182): Add `toString` to classes extending `c.s.j.ptr.ByReference` - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
* [#1183](https://github.com/java-native-access/jna/pull/1183): `c.s.j.p.win32.WinDef.CHARByReference#getValue` should only read one byte - [@dbwiddis](https://github.com/dbwiddis).
* [#1184](https://github.com/java-native-access/jna/pull/1184): `c.s.j.p.win32.WinDef.ULONGLONG` should always be 8 bytes - [@dbwiddis](https://github.com/dbwiddis).


Release 5.5.0
=============

Expand Down
41 changes: 41 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Advapi32Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Advapi32Util.Account;
import com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC;
import com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC;
import com.sun.jna.platform.win32.WinBase.FILETIME;
Expand All @@ -86,6 +87,7 @@
import com.sun.jna.platform.win32.WinNT.SECURITY_IMPERSONATION_LEVEL;
import com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES;
import com.sun.jna.platform.win32.WinNT.SID_NAME_USE;
import com.sun.jna.platform.win32.WinNT.TOKEN_PRIMARY_GROUP;
import com.sun.jna.platform.win32.WinNT.TOKEN_TYPE;
import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;
Expand Down Expand Up @@ -475,6 +477,45 @@ public static Account[] getTokenGroups(HANDLE hToken) {
return userGroups.toArray(new Account[0]);
}

/**
* This function returns the primary group associated with a security token,
* such as a user token.
*
* @param hToken
* Token.
* @return Token primary group.
*/
public static Account getTokenPrimaryGroup(HANDLE hToken) {
// get token group information size
IntByReference tokenInformationLength = new IntByReference();
if (Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenPrimaryGroup, null, 0,
tokenInformationLength)) {
throw new RuntimeException("Expected GetTokenInformation to fail with ERROR_INSUFFICIENT_BUFFER");
}
int rc = Kernel32.INSTANCE.GetLastError();
if (rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
throw new Win32Exception(rc);
}
// get token group information
WinNT.TOKEN_PRIMARY_GROUP primaryGroup = new WinNT.TOKEN_PRIMARY_GROUP(tokenInformationLength.getValue());
if (!Advapi32.INSTANCE.GetTokenInformation(hToken, WinNT.TOKEN_INFORMATION_CLASS.TokenPrimaryGroup,
primaryGroup, tokenInformationLength.getValue(), tokenInformationLength)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
Account group;
try {
group = Advapi32Util.getAccountBySid(primaryGroup.PrimaryGroup);
} catch (Exception e) {
group = new Account();
group.sid = primaryGroup.PrimaryGroup.getBytes();
group.sidString = Advapi32Util.convertSidToStringSid(primaryGroup.PrimaryGroup);
group.name = group.sidString;
group.fqn = group.sidString;
group.accountType = SID_NAME_USE.SidTypeGroup;
}
return group;
}

/**
* This function returns the information about the user who owns a security
* token,
Expand Down
143 changes: 143 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
import com.sun.jna.Union;
import com.sun.jna.platform.win32.BaseTSD.LONG_PTR;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
Expand Down Expand Up @@ -1774,4 +1775,146 @@ public HGLRCByReference(HGLRC h) {
super(h);
}
}

/**
* COORD structure
*/
@FieldOrder({ "X", "Y" })
public static class COORD extends Structure {

public short X;
public short Y;

@Override
public String toString() {
return String.format("COORD(%s,%s)", X, Y);
}
}

/**
* SMALL_RECT structure
*/
@FieldOrder({ "Left", "Top", "Right", "Bottom" })
public static class SMALL_RECT extends Structure {

public short Left;
public short Top;
public short Right;
public short Bottom;

@Override
public String toString() {
return String.format("SMALL_RECT(%s,%s)(%s,%s)", Left, Top, Right, Bottom);
}
}

/**
* CONSOLE_SCREEN_BUFFER_INFO structure
*/
@FieldOrder({ "dwSize", "dwCursorPosition", "wAttributes", "srWindow", "dwMaximumWindowSize" })
public static class CONSOLE_SCREEN_BUFFER_INFO extends Structure {

public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;

@Override
public String toString() {
return String.format("CONSOLE_SCREEN_BUFFER_INFO(%s,%s,%s,%s,%s)", dwSize, dwCursorPosition, wAttributes, srWindow, dwMaximumWindowSize);
}
}

/**
* INPUT_RECORD structure
*/
@FieldOrder({ "EventType", "Event" })
public static class INPUT_RECORD extends Structure {

public static final short KEY_EVENT = 0x01;
public static final short MOUSE_EVENT = 0x02;
public static final short WINDOW_BUFFER_SIZE_EVENT = 0x04;

public short EventType;
public Event Event;

public static class Event extends Union {
public KEY_EVENT_RECORD KeyEvent;
public MOUSE_EVENT_RECORD MouseEvent;
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
}

@Override
public void read() {
super.read();
switch (EventType) {
case KEY_EVENT:
Event.setType("KeyEvent");
break;
case MOUSE_EVENT:
Event.setType("MouseEvent");
break;
case WINDOW_BUFFER_SIZE_EVENT:
Event.setType("WindowBufferSizeEvent");
break;
}
Event.read();
}

@Override
public String toString() {
return String.format("INPUT_RECORD(%s)", EventType);
}
}

/**
* KEY_EVENT_RECORD structure
*/
@FieldOrder({ "bKeyDown", "wRepeatCount", "wVirtualKeyCode", "wVirtualScanCode", "uChar", "dwControlKeyState" })
public static class KEY_EVENT_RECORD extends Structure {

public boolean bKeyDown;
public short wRepeatCount;
public short wVirtualKeyCode;
public short wVirtualScanCode;
public char uChar;
public int dwControlKeyState;

@Override
public String toString() {
return String.format("KEY_EVENT_RECORD(%s,%s,%s,%s,%s,%s)", bKeyDown, wRepeatCount, wVirtualKeyCode, wVirtualKeyCode, wVirtualScanCode, uChar, dwControlKeyState);
}
}

/**
* MOUSE_EVENT_RECORD structure
*/
@FieldOrder({ "dwMousePosition", "dwButtonState", "dwControlKeyState", "dwEventFlags" })
public static class MOUSE_EVENT_RECORD extends Structure {

public COORD dwMousePosition;
public int dwButtonState;
public int dwControlKeyState;
public int dwEventFlags;

@Override
public String toString() {
return String.format("MOUSE_EVENT_RECORD(%s,%s,%s,%s)", dwMousePosition, dwButtonState, dwControlKeyState, dwEventFlags);
}
}

/**
* WINDOW_BUFFER_SIZE_RECORD structure
*/
@FieldOrder({ "dwSize" })
public static class WINDOW_BUFFER_SIZE_RECORD extends Structure {

public COORD dwSize;

@Override
public String toString() {
return String.format("WINDOW_BUFFER_SIZE_RECORD(%s)", dwSize);
}
}
}
28 changes: 28 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
import com.sun.jna.platform.win32.WinNT.PSID;
import com.sun.jna.Union;
import com.sun.jna.ptr.ByReference;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;
Expand Down Expand Up @@ -427,6 +428,33 @@ public TOKEN_USER(int size) {
}
}

/**
* The TOKEN_PRIMARY_GROUP structure specifies a group security identifier (SID)
* for an access token.
*/
@FieldOrder({ "PrimaryGroup" })
public static class TOKEN_PRIMARY_GROUP extends Structure {
/**
* A pointer to a SID structure representing a group that will become the
* primary group of any objects created by a process using this access token.
* The SID must be one of the group SIDs already in the token.
*/
public PSID.ByReference PrimaryGroup;

public TOKEN_PRIMARY_GROUP() {
super();
}

public TOKEN_PRIMARY_GROUP(Pointer memory) {
super(memory);
read();
}

public TOKEN_PRIMARY_GROUP(int size) {
super(new Memory(size));
}
}

/**
* The TOKEN_GROUPS structure contains information about the group security
* identifiers (SIDs) in an access token.
Expand Down
41 changes: 41 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Wincon.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
package com.sun.jna.platform.win32;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.CONSOLE_SCREEN_BUFFER_INFO;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.INPUT_RECORD;
import com.sun.jna.platform.win32.WinDef.LPVOID;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;

Expand Down Expand Up @@ -182,6 +185,9 @@ public interface Wincon {
int ENABLE_INSERT_MODE=0x0020;
int ENABLE_QUICK_EDIT_MODE=0x0040;
int ENABLE_EXTENDED_FLAGS=0x0080;
int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
int DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;

/* If the hConsoleHandle parameter is a screen buffer handle, the mode
* can be one or more of the following values
Expand Down Expand Up @@ -249,4 +255,39 @@ public interface Wincon {
* @see <a href="https://msdn.microsoft.com/en-us/library/ms686050(v=vs.85).aspx">SetConsoleTitle documentation</a>
*/
boolean SetConsoleTitle(String lpConsoleTitle);

/**
* Retrieves information about the specified console screen buffer.
* @param hConsoleOutput A handle to the console screen buffer.
* @param lpConsoleScreenBufferInfo A pointer to a CONSOLE_SCREEN_BUFFER_INFO structure that receives the console screen buffer information.
* @return {@code true} if successful - if {@code false} then use
* {@code GetLastError()} to get extended error information
* @see <a href="https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo">GetConsoleScreenBufferInfo documentation</a>
*/
boolean GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

/**
* Reads data from a console input buffer and removes it from the buffer.
* @param hConsoleInput A handle to the console input buffer.
* @param lpBuffer A pointer to an array of INPUT_RECORD structures that receives the input buffer data.
* @param nLength The size of the array pointed to by the lpBuffer parameter, in array elements.
* @param lpNumberOfEventsRead A pointer to a variable that receives the number of input records read.
* @return {@code true} if successful - if {@code false} then use
* {@code GetLastError()} to get extended error information
* @see <a href="https://docs.microsoft.com/en-us/windows/console/readconsoleinput">ReadConsoleInput documentation</a>
*/
boolean ReadConsoleInput(HANDLE hConsoleInput, INPUT_RECORD[] lpBuffer, int nLength, IntByReference lpNumberOfEventsRead);

/**
* Writes a character string to a console screen buffer beginning at the current cursor location.
* @param hConsoleOutput A handle to the console screen buffer.
* @param lpBuffer A pointer to a buffer that contains characters to be written to the console screen buffer.
* @param nNumberOfCharsToWrite The number of characters to be written.
* @param lpNumberOfCharsWritten A pointer to a variable that receives the number of characters actually written.
* @param lpReserved Reserved; must be NULL.
* @return {@code true} if successful - if {@code false} then use
* {@code GetLastError()} to get extended error information
* @see <a href="https://docs.microsoft.com/en-us/windows/console/writeconsole">WriteConsole documentation</a>
*/
boolean WriteConsole(HANDLE hConsoleOutput, String lpBuffer, int nNumberOfCharsToWrite, IntByReference lpNumberOfCharsWritten, LPVOID lpReserved);
}
Loading