1
2
3
4
5 package oshi.driver.windows.registry;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import com.sun.jna.Pointer;
11 import com.sun.jna.platform.win32.Netapi32;
12 import com.sun.jna.platform.win32.Netapi32.SESSION_INFO_10;
13
14 import oshi.annotation.concurrent.ThreadSafe;
15 import oshi.jna.ByRef.CloseableIntByReference;
16 import oshi.jna.ByRef.CloseablePointerByReference;
17 import oshi.software.os.OSSession;
18
19
20
21
22 @ThreadSafe
23 public final class NetSessionData {
24
25 private static final Netapi32 NET = Netapi32.INSTANCE;
26
27 private NetSessionData() {
28 }
29
30 public static List<OSSession> queryUserSessions() {
31 List<OSSession> sessions = new ArrayList<>();
32 try (CloseablePointerByReference bufptr = new CloseablePointerByReference();
33 CloseableIntByReference entriesread = new CloseableIntByReference();
34 CloseableIntByReference totalentries = new CloseableIntByReference()) {
35 if (0 == NET.NetSessionEnum(null, null, null, 10, bufptr, Netapi32.MAX_PREFERRED_LENGTH, entriesread,
36 totalentries, null)) {
37 Pointer buf = bufptr.getValue();
38 SESSION_INFO_10 si10 = new SESSION_INFO_10(buf);
39 if (entriesread.getValue() > 0) {
40 SESSION_INFO_10[] sessionInfo = (SESSION_INFO_10[]) si10.toArray(entriesread.getValue());
41 for (SESSION_INFO_10 si : sessionInfo) {
42
43 long logonTime = System.currentTimeMillis() - (1000L * si.sesi10_time);
44 sessions.add(new OSSession(si.sesi10_username, "Network session", logonTime, si.sesi10_cname));
45 }
46 }
47 NET.NetApiBufferFree(buf);
48 }
49 }
50 return sessions;
51 }
52 }