|
| 1 | +using System; |
| 2 | +using System.Data.SQLite; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.ServiceProcess; |
| 7 | + |
| 8 | +namespace ShutdownTimeUpdaterService |
| 9 | +{ |
| 10 | + public partial class ShutdownTimeService : ServiceBase |
| 11 | + { |
| 12 | + private string _cachedUsername; |
| 13 | + |
| 14 | + public ShutdownTimeService() |
| 15 | + { |
| 16 | + InitializeComponent(); |
| 17 | + } |
| 18 | + |
| 19 | + protected override void OnStart(string[] args) |
| 20 | + { |
| 21 | + _cachedUsername = GetLoggedInUsername(); |
| 22 | + |
| 23 | + if (string.IsNullOrEmpty(_cachedUsername)) |
| 24 | + { |
| 25 | + EventLog.WriteEntry("ShutdownTimeService", "Could not detect logged-in user at service start.", EventLogEntryType.Warning); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + EventLog.WriteEntry("ShutdownTimeService", $"Detected user at start: {_cachedUsername}", EventLogEntryType.Information); |
| 30 | + } |
| 31 | + |
| 32 | + EventLog.WriteEntry("ShutdownTimeService", "Service started.", EventLogEntryType.Information); |
| 33 | + } |
| 34 | + |
| 35 | + protected override void OnStop() |
| 36 | + { |
| 37 | + EventLog.WriteEntry("ShutdownTimeService", "Service stopped.", EventLogEntryType.Information); |
| 38 | + } |
| 39 | + |
| 40 | + protected override void OnShutdown() |
| 41 | + { |
| 42 | + EventLog.WriteEntry("ShutdownTimeService", "System is shutting down. Attempting to update logout time.", EventLogEntryType.Information); |
| 43 | + UpdateLogoutTime(); |
| 44 | + } |
| 45 | + |
| 46 | + private void UpdateLogoutTime() |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + string dbPath = GetUserAppDataPath(); |
| 51 | + |
| 52 | + if (string.IsNullOrEmpty(dbPath) || !File.Exists(dbPath)) |
| 53 | + { |
| 54 | + EventLog.WriteEntry("ShutdownTimeService", $"Database file not found or invalid path: {dbPath}", EventLogEntryType.Error); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + using (var conn = new SQLiteConnection($"Data Source={dbPath};Version=3;")) |
| 59 | + { |
| 60 | + conn.Open(); |
| 61 | + |
| 62 | + string updateQuery = "UPDATE sessions SET logout_time = @LogoutTime WHERE logout_time IS NULL LIMIT 1"; |
| 63 | + |
| 64 | + using (var cmd = new SQLiteCommand(updateQuery, conn)) |
| 65 | + { |
| 66 | + cmd.Parameters.AddWithValue("@LogoutTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); |
| 67 | + |
| 68 | + int rowsAffected = cmd.ExecuteNonQuery(); |
| 69 | + |
| 70 | + if (rowsAffected > 0) |
| 71 | + { |
| 72 | + EventLog.WriteEntry("ShutdownTimeService", "Logout time updated for active session.", EventLogEntryType.Information); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + EventLog.WriteEntry("ShutdownTimeService", "No active session found to update logout time.", EventLogEntryType.Warning); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + catch (Exception ex) |
| 82 | + { |
| 83 | + EventLog.WriteEntry("ShutdownTimeService", $"Error updating logout time: {ex.Message}\nStackTrace: {ex.StackTrace}", EventLogEntryType.Error); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private string GetUserAppDataPath() |
| 88 | + { |
| 89 | + if (string.IsNullOrEmpty(_cachedUsername)) |
| 90 | + { |
| 91 | + EventLog.WriteEntry("ShutdownTimeService", "Cached username is null or empty.", EventLogEntryType.Error); |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + string userAppDataPath = Path.Combine(@"C:\Users", _cachedUsername, "AppData", "Roaming", "SessionTracker", "user_sessions.db"); |
| 96 | + return userAppDataPath; |
| 97 | + } |
| 98 | + |
| 99 | + // Get the username of the currently logged-in user |
| 100 | + [DllImport("Wtsapi32.dll")] |
| 101 | + static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out IntPtr ppBuffer, out int pBytesReturned); |
| 102 | + |
| 103 | + [DllImport("kernel32.dll")] |
| 104 | + static extern int WTSGetActiveConsoleSessionId(); |
| 105 | + |
| 106 | + [DllImport("Wtsapi32.dll")] |
| 107 | + static extern void WTSFreeMemory(IntPtr pointer); |
| 108 | + |
| 109 | + enum WTS_INFO_CLASS |
| 110 | + { |
| 111 | + WTSUserName = 5, |
| 112 | + WTSDomainName = 7, |
| 113 | + } |
| 114 | + |
| 115 | + private string GetLoggedInUsername() |
| 116 | + { |
| 117 | + int sessionId = WTSGetActiveConsoleSessionId(); |
| 118 | + IntPtr buffer; |
| 119 | + int strLen; |
| 120 | + |
| 121 | + if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WTS_INFO_CLASS.WTSUserName, out buffer, out strLen)) |
| 122 | + { |
| 123 | + string userName = Marshal.PtrToStringAnsi(buffer); |
| 124 | + WTSFreeMemory(buffer); |
| 125 | + return userName; |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
0 commit comments