Skip to content

Commit

Permalink
Type mapping and enum fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed May 21, 2020
1 parent 9c24002 commit ee60182
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 32 deletions.
5 changes: 5 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/IPHlpAPI.java
Expand Up @@ -52,8 +52,13 @@ public interface IPHlpAPI extends Library {
int MAX_SCOPE_ID_LEN = 256;

// Source: Winsock2.h
int AF_UNSPEC = 0; // The address family is unspecified.
int AF_INET = 2; // The Internet Protocol version 4 (IPv4) address family.
int AF_IPX = 6; // The IPX/SPX address family.
int AF_NETBIOS = 17; // The NetBIOS address family.
int AF_INET6 = 23; // The Internet Protocol version 6 (IPv6) address family.
int AF_IRDA = 26; // The Infrared Data Association (IrDA) address family.
int AF_BTH = 32; // The Bluetooth address family.

/**
* The MIB_IFROW structure stores information about a particular interface.
Expand Down
19 changes: 10 additions & 9 deletions contrib/platform/src/com/sun/jna/platform/win32/Netapi32.java
Expand Up @@ -27,14 +27,14 @@
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
import com.sun.jna.WString;
import com.sun.jna.platform.win32.DsGetDC.PDOMAIN_CONTROLLER_INFO;
import com.sun.jna.platform.win32.Guid.GUID;
import com.sun.jna.platform.win32.NTSecApi.PLSA_FOREST_TRUST_INFORMATION;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.win32.W32APITypeMapper;

/**
* Netapi32.dll Interface.
Expand All @@ -52,17 +52,17 @@ public interface Netapi32 extends StdCallLibrary {
*/
@FieldOrder({ "sesi10_cname", "sesi10_username", "sesi10_time", "sesi10_idle_time" })
class SESSION_INFO_10 extends Structure {
public String sesi10_cname;
public String sesi10_username;
public WString sesi10_cname;
public WString sesi10_username;
public int sesi10_time;
public int sesi10_idle_time;

public SESSION_INFO_10() {
super(W32APITypeMapper.DEFAULT);
super();
}

public SESSION_INFO_10(Pointer p) {
super(p, Structure.ALIGN_DEFAULT, W32APITypeMapper.DEFAULT);
super(p);
read();
}
}
Expand Down Expand Up @@ -114,11 +114,12 @@ public SESSION_INFO_10(Pointer p) {
* continue an existing session search. The handle should be zero on
* the first call and left unchanged for subsequent calls. If
* resume_handle is NULL, no resume handle is stored.
* @return If the function succeeds, the return value is NERR_Success. If the
* function fails, the return value is an error code.
* @return If the function succeeds, the return value is NERR_Success (0). If
* the function fails, the return value is an error code.
*/
int NetSessionEnum(String servername, String UncClientName, String username, int level, PointerByReference bufptr,
int prefmaxlen, IntByReference entriesread, IntByReference totalentries, IntByReference resume_handle);
int NetSessionEnum(WString servername, WString UncClientName, WString username, int level,
PointerByReference bufptr, int prefmaxlen, IntByReference entriesread, IntByReference totalentries,
IntByReference resume_handle);

/**
* Retrieves join status information for the specified computer.
Expand Down
114 changes: 101 additions & 13 deletions contrib/platform/src/com/sun/jna/platform/win32/Wtsapi32.java
Expand Up @@ -113,10 +113,77 @@ public interface Wtsapi32 extends StdCallLibrary {
int WTS_PROCESS_INFO_LEVEL_0 = 0;
int WTS_PROCESS_INFO_LEVEL_1 = 1;

/**
* Defined in {@code winsta.h} and present in this interface to properly size
* the {@link WTSINFO} structure.
*/
int DOMAIN_LENGTH = 17;

/**
* Defined in {@code winsta.h} and present in this interface to properly size
* the {@link WTSINFO} structure.
*/
int USERNAME_LENGTH = 20;

/**
* Defined in {@code winsta.h} and present in this interface to properly size
* the {@link WTSINFO} structure.
*/
int WINSTATIONNAME_LENGTH = 32;

/**
* Specifies the connection state of a Remote Desktop Services session.
*/
public interface WTS_CONNECTSTATE_CLASS {
int WTSActive = 0;
int WTSConnected = 1;
int WTSConnectQuery = 2;
int WTSShadow = 3;
int WTSDisconnected = 4;
int WTSIdle = 5;
int WTSListen = 6;
int WTSReset = 7;
int WTSDown = 8;
int WTSInit = 9;
}

/**
* Contains values that indicate the type of session information to retrieve in
* a call to the {@link #WTSQuerySessionInformation()} function.
*/
public interface WTS_INFO_CLASS {
int WTSInitialProgram = 0;
int WTSApplicationName = 1;
int WTSWorkingDirectory = 2;
int WTSOEMId = 3;
int WTSSessionId = 4;
int WTSUserName = 5;
int WTSWinStationName = 6;
int WTSDomainName = 7;
int WTSConnectState = 8;
int WTSClientBuildNumber = 9;
int WTSClientName = 10;
int WTSClientDirectory = 11;
int WTSClientProductId = 12;
int WTSClientHardwareId = 13;
int WTSClientAddress = 14;
int WTSClientDisplay = 15;
int WTSClientProtocolType = 16;
int WTSIdleTime = 17;
int WTSLogonTime = 18;
int WTSIncomingBytes = 19;
int WTSOutgoingBytes = 20;
int WTSIncomingFrames = 21;
int WTSOutgoingFrames = 22;
int WTSClientInfo = 23;
int WTSSessionInfo = 24;
int WTSSessionInfoEx = 25;
int WTSConfigInfo = 26;
int WTSValidationInfo = 27;
int WTSSessionAddressV4 = 28;
int WTSIsRemoteSession = 29;
}

/**
* Contains information about a client session on a Remote Desktop Session Host
* (RD Session Host) server.
Expand Down Expand Up @@ -164,40 +231,61 @@ public WTS_CLIENT_ADDRESS(Pointer p) {
class WTSINFO extends Structure {
private static final int CHAR_WIDTH = Boolean.getBoolean("w32.ascii") ? 1 : 2;

public int State;
public int State; // WTS_CONNECTSTATE_CLASS
public int SessionId;
public int IncomingBytes;
public int OutgoingBytes;
public int IncomingFrames;
public int OutgoingFrames;
public int IncomingCompressedBytes;
public int OutgoingCompressedBytes;
public byte[] WinStationName = new byte[WINSTATIONNAME_LENGTH * CHAR_WIDTH];
public byte[] Domain = new byte[DOMAIN_LENGTH * CHAR_WIDTH];
public byte[] UserName = new byte[(USERNAME_LENGTH + 1) * CHAR_WIDTH];
public final byte[] WinStationName = new byte[WINSTATIONNAME_LENGTH * CHAR_WIDTH];
public final byte[] Domain = new byte[DOMAIN_LENGTH * CHAR_WIDTH];
public final byte[] UserName = new byte[(USERNAME_LENGTH + 1) * CHAR_WIDTH];
public LARGE_INTEGER ConnectTime;
public LARGE_INTEGER DisconnectTime;
public LARGE_INTEGER LastInputTime;
public LARGE_INTEGER LogonTime;
public LARGE_INTEGER CurrentTime;

public WTSINFO() {
super(W32APITypeMapper.DEFAULT);
super();
}

public WTSINFO(Pointer p) {
super(p, Structure.ALIGN_DEFAULT, W32APITypeMapper.DEFAULT);
super(p);
read();
}

/**
* Convenience method to return the null-terminated string in the
* {@link #WinStationName} member, accounting for {@code CHAR} or {@code WCHAR}
* byte width.
*
* @return The {@code WinStationName} as a string.
*/
public String getWinStationName() {
return getStringAtOffset(fieldOffset("WinStationName"));
}

/**
* Convenience method to return the null-terminated string in the
* {@link #Domain} member, accounting for {@code CHAR} or {@code WCHAR} byte
* width.
*
* @return The {@code Domain} as a string.
*/
public String getDomain() {
return getStringAtOffset(fieldOffset("Domain"));
}

/**
* Convenience method to return the null-terminated string in the
* {@link #UserName} member, accounting for {@code CHAR} or {@code WCHAR} byte
* width.
*
* @return The {@code UserName} as a string.
*/
public String getUserName() {
return getStringAtOffset(fieldOffset("UserName"));
}
Expand All @@ -210,8 +298,7 @@ private String getStringAtOffset(int offset) {
/**
* Contains extended information about a process running on a Remote Desktop
* Session Host (RD Session Host) server. This structure is returned by the
* WTSEnumerateProcessesEx function when you set the pLevel parameter to
* one.
* WTSEnumerateProcessesEx function when you set the pLevel parameter to one.
*
* @see <A HREF=
* "https://docs.microsoft.com/en-us/windows/desktop/api/wtsapi32/ns-wtsapi32-_wts_process_info_exa">WTS_PROCESS_INFO_EXA</A>
Expand Down Expand Up @@ -262,7 +349,7 @@ public WTS_PROCESS_INFO_EX(Pointer p) {
* @param ppSessionInfo
* A pointer to an array of {@link WTS_SESSION_INFO} structures that
* represent the retrieved sessions. To free the returned buffer,
* call the {@link #WTSFreeMemory} function.
* call the {@link Wtsapi32#WTSFreeMemory} function.
* @param pCount
* A pointer to the number of {@code WTS_SESSION_INFO} structures
* returned in the {@code ppSessionInfo} parameter.
Expand Down Expand Up @@ -302,7 +389,7 @@ boolean WTSEnumerateSessions(HANDLE hServer, int Reserved, int Version, PointerB
* To query information for another user's session, you must have
* Query Information permission.
* @param WTSInfoClass
* A value of the {@code WTS_INFO_CLASS} enumeration that indicates
* A value of the {@link WTS_INFO_CLASS} enumeration that indicates
* the type of session information to retrieve in a call to the
* {@code WTSQuerySessionInformation} function.
* @param ppBuffer
Expand Down Expand Up @@ -334,14 +421,15 @@ boolean WTSQuerySessionInformation(HANDLE hServer, int SessionId, int WTSInfoCla
* Registers the specified window to receive session change notifications.
*
* @param hWnd
* [in] Handle of the window to receive session change notifications.
* [in] Handle of the window to receive session change
* notifications.
*
* @param dwFlags
* [in] Specifies which session notifications are to be received.
* This parameter can be one of the following values.
*
* @return If the function succeeds, the return value is TRUE. Otherwise, it is
* FALSE. To get extended error information, call GetLastError.
* @return If the function succeeds, the return value is TRUE. Otherwise, it
* is FALSE. To get extended error information, call GetLastError.
*/
boolean WTSRegisterSessionNotification(HWND hWnd, int dwFlags);

Expand Down
20 changes: 10 additions & 10 deletions contrib/platform/test/com/sun/jna/platform/win32/Wtsapi32Test.java
Expand Up @@ -31,6 +31,8 @@
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.Wtsapi32.WTSINFO;
import com.sun.jna.platform.win32.Wtsapi32.WTS_CLIENT_ADDRESS;
import com.sun.jna.platform.win32.Wtsapi32.WTS_CONNECTSTATE_CLASS;
import com.sun.jna.platform.win32.Wtsapi32.WTS_INFO_CLASS;
import com.sun.jna.platform.win32.Wtsapi32.WTS_PROCESS_INFO_EX;
import com.sun.jna.platform.win32.Wtsapi32.WTS_SESSION_INFO;
import com.sun.jna.ptr.IntByReference;
Expand Down Expand Up @@ -105,9 +107,6 @@ public void testWTSEnumerateProcessesEx() {
}

public void testWTSEnumerateSessions() {
int WTSClientAddress = 14;
int WTSSessionInfo = 24;
int WTSClientProtocolType = 16;
PointerByReference ppSessionInfo = new PointerByReference();
IntByReference pCount = new IntByReference();
assertTrue("Enumerate Sessions failed.", Wtsapi32.INSTANCE
Expand All @@ -117,19 +116,19 @@ public void testWTSEnumerateSessions() {
WTS_SESSION_INFO sessionInfoRef = new WTS_SESSION_INFO(pSessionInfo);
WTS_SESSION_INFO[] sessionInfo = (WTS_SESSION_INFO[]) sessionInfoRef.toArray(pCount.getValue());
for (WTS_SESSION_INFO session : sessionInfo) {
if (session.State == 0) { // WTS_ACTIVE
if (session.State == WTS_CONNECTSTATE_CLASS.WTSActive) {
// Use session id to fetch additional session information
PointerByReference ppBuffer = new PointerByReference();
IntByReference pBytes = new IntByReference();
Wtsapi32.INSTANCE.WTSQuerySessionInformation(Wtsapi32.WTS_CURRENT_SERVER_HANDLE, session.SessionId,
WTSClientProtocolType, ppBuffer, pBytes);
WTS_INFO_CLASS.WTSClientProtocolType, ppBuffer, pBytes);
Pointer pBuffer = ppBuffer.getValue(); // pointer to USHORT
short protocolType = pBuffer.getShort(0); // 0 = console, 2 = RDP
assertTrue("Protocol Type must be between 0 and 2", protocolType >= 0 && protocolType <= 2);
Wtsapi32.INSTANCE.WTSFreeMemory(pBuffer);

Wtsapi32.INSTANCE.WTSQuerySessionInformation(Wtsapi32.WTS_CURRENT_SERVER_HANDLE, session.SessionId,
WTSSessionInfo, ppBuffer, pBytes);
WTS_INFO_CLASS.WTSSessionInfo, ppBuffer, pBytes);
pBuffer = ppBuffer.getValue(); // returns WTSINFO
WTSINFO wtsInfo = new WTSINFO(pBuffer);
assertEquals("State from WTSINFO must match WTS_SESSION_INFO", session.State, wtsInfo.State);
Expand All @@ -144,14 +143,15 @@ public void testWTSEnumerateSessions() {
Wtsapi32.INSTANCE.WTSFreeMemory(pBuffer);

Wtsapi32.INSTANCE.WTSQuerySessionInformation(Wtsapi32.WTS_CURRENT_SERVER_HANDLE, session.SessionId,
WTSClientAddress, ppBuffer, pBytes);
WTS_INFO_CLASS.WTSClientAddress, ppBuffer, pBytes);
pBuffer = ppBuffer.getValue(); // returns WTS_CLIENT_ADDRESS
WTS_CLIENT_ADDRESS addr = new WTS_CLIENT_ADDRESS(pBuffer);
assertTrue("Address family must be AF_INET, AF_INET6, AF_IPX, AF_NETBIOS, or AF_UNSPEC.",
addr.AddressFamily == 0 // AF_UNSPEC
addr.AddressFamily == IPHlpAPI.AF_UNSPEC
|| addr.AddressFamily == IPHlpAPI.AF_INET
|| addr.AddressFamily == IPHlpAPI.AF_INET6 || addr.AddressFamily == 6 // AF_IPX
|| addr.AddressFamily == 17); // AF_NETBIOS
|| addr.AddressFamily == IPHlpAPI.AF_INET6
|| addr.AddressFamily == IPHlpAPI.AF_IPX
|| addr.AddressFamily == IPHlpAPI.AF_NETBIOS);
Wtsapi32.INSTANCE.WTSFreeMemory(pBuffer);
}
}
Expand Down

0 comments on commit ee60182

Please sign in to comment.