[wing/wip/nacho/session-locked] utils: add method to get if the session is locked




commit 7d8e31e6118b89c736c551f24bbf4f4c7a5a7872
Author: Ignacio Casal Quinteiro <qignacio amazon com>
Date:   Mon Jan 4 09:51:51 2021 +0100

    utils: add method to get if the session is locked

 wing/wingutils.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 wing/wingutils.h |  3 +++
 2 files changed, 53 insertions(+)
---
diff --git a/wing/wingutils.c b/wing/wingutils.c
index 974f118..66471cf 100644
--- a/wing/wingutils.c
+++ b/wing/wingutils.c
@@ -182,3 +182,53 @@ end:
 
   return result;
 }
+
+gboolean
+wing_get_windows_session_locked (void)
+{
+  DWORD console_sid;
+  WTS_INFO_CLASS wtsic = WTSSessionInfoEx;
+  LPTSTR wts_buffer = NULL;
+  WTSINFOEXW *wts_info = NULL;
+  DWORD wts_bytes_ret = 0;
+  gint major;
+  gint minor;
+  gint build;
+  gint product_type;
+  gboolean res = FALSE;
+
+  console_sid = WTSGetActiveConsoleSessionId ();
+
+  if (!WTSQuerySessionInformation (WTS_CURRENT_SERVER_HANDLE, console_sid, wtsic, &wts_buffer, 
&wts_bytes_ret))
+    {
+      g_warning ("WTSQuerySessionInformation failed (0x%X)", GetLastError ());
+      return res;
+    }
+
+  if (wts_bytes_ret == 0)
+    {
+      g_warning ("WTSQuerySessionInformation returned 0 bytes");
+      WTSFreeMemory (wts_buffer);
+      return res;
+    }
+
+  wts_info = (WTSINFOEXW *)wts_buffer;
+  if (wts_info->Level != 1)
+    {
+      g_warning ("Unexpected level for WTSInfoExLevel %u (expected: 1)", wts_info->Level);
+      WTSFreeMemory (wts_buffer);
+      return res;
+    }
+
+  /* On Windows 7/Server 2008 R2 the flag for the session state is swapped (WTS_SESSIONSTATE_UNLOCK means 
that session is locked)
+   * https://docs.microsoft.com/en-us/windows/desktop/api/wtsapi32/ns-wtsapi32-_wtsinfoex_level1_a
+   */
+  if (wing_get_version_number(&major, &minor, &build, &product_type) && (major == 6 && minor == 1))
+    res = (wts_info->Data.WTSInfoExLevel1.SessionFlags == WTS_SESSIONSTATE_UNLOCK);
+  else
+    res = (wts_info->Data.WTSInfoExLevel1.SessionFlags == WTS_SESSIONSTATE_LOCK);
+
+  WTSFreeMemory (wts_buffer);
+
+  return res;
+}
diff --git a/wing/wingutils.h b/wing/wingutils.h
index 213968c..75cbad0 100644
--- a/wing/wingutils.h
+++ b/wing/wingutils.h
@@ -67,6 +67,9 @@ gboolean     wing_overlap_wait_result  (HANDLE           hfile,
                                         DWORD           *transferred,
                                         GCancellable    *cancellable);
 
+WING_AVAILABLE_IN_ALL
+gboolean     wing_get_windows_session_locked (void);
+
 G_END_DECLS
 
 #endif /* WING_UTILS_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]