Skip to content

Commit 2f29345

Browse files
authored
Add files via upload
1 parent b268f4f commit 2f29345

4 files changed

Lines changed: 292 additions & 0 deletions

File tree

SessionTracker.iss

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[Setup]
2+
AppName=SessionTracker
3+
AppVersion=1.0
4+
DefaultDirName={pf}\SessionTracker
5+
OutputDir=.
6+
OutputBaseFilename=SessionTrackerInstaller
7+
Compression=lzma
8+
SolidCompression=yes
9+
PrivilegesRequired=admin
10+
ArchitecturesInstallIn64BitMode=x64
11+
12+
[Files]
13+
Source: "gui\app-gui.jar"; DestDir: "{app}\gui"; Flags: ignoreversion
14+
Source: "gui\config.properties"; DestDir: "{app}\gui"; Flags: ignoreversion
15+
Source: "jre\*"; DestDir: "{app}\jre"; Flags: recursesubdirs ignoreversion
16+
Source: "service\*"; DestDir: "{app}\service"; Flags: recursesubdirs createallsubdirs ignoreversion
17+
Source: "install.bat"; DestDir: "{app}"; Flags: ignoreversion
18+
Source: "uninstall.bat"; DestDir: "{app}"; Flags: ignoreversion
19+
20+
[Run]
21+
Filename: "cmd.exe"; Parameters: "/c install.bat"; WorkingDir: "{app}"; Flags: runhidden waituntilterminated
22+
23+
[UninstallRun]
24+
Filename: "cmd.exe"; Parameters: "/c uninstall.bat"; WorkingDir: "{app}"; Flags: runhidden waituntilterminated runascurrentuser
25+
26+
[Code]
27+
28+
var
29+
SystemNoPage: TInputQueryWizardPage;
30+
31+
procedure InitializeWizard();
32+
begin
33+
SystemNoPage := CreateInputQueryPage(
34+
wpSelectDir,
35+
'System Configuration',
36+
'Configure System Number',
37+
'Please enter the SYSTEM_NO value for this installation.'
38+
);
39+
40+
SystemNoPage.Add('SYSTEM_NO:', False);
41+
42+
{ Default shown in installer }
43+
SystemNoPage.Values[0] := 'LAB-M1';
44+
end;
45+
46+
47+
procedure ReplaceSystemNoInFile(FileName, NewValue: string);
48+
var
49+
Lines: TArrayOfString;
50+
I: Integer;
51+
begin
52+
if LoadStringsFromFile(FileName, Lines) then
53+
begin
54+
for I := 0 to GetArrayLength(Lines) - 1 do
55+
begin
56+
if Pos('SYSTEM_NO=', Lines[I]) = 1 then
57+
Lines[I] := 'SYSTEM_NO=' + NewValue;
58+
end;
59+
60+
SaveStringsToFile(FileName, Lines, False);
61+
end
62+
else
63+
begin
64+
MsgBox('Could not open config.properties file.', mbError, MB_OK);
65+
end;
66+
end;
67+
68+
69+
procedure CurStepChanged(CurStep: TSetupStep);
70+
var
71+
ConfigPath: string;
72+
SystemNo: string;
73+
begin
74+
if CurStep = ssPostInstall then
75+
begin
76+
SystemNo := SystemNoPage.Values[0];
77+
ConfigPath := ExpandConstant('{app}\gui\config.properties');
78+
79+
ReplaceSystemNoInFile(ConfigPath, SystemNo);
80+
end;
81+
end;

Windows Service.txt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+

install.bat

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@echo off
2+
setlocal EnableDelayedExpansion
3+
4+
:: Save the current directory
5+
set "OriginalDir=%CD%"
6+
7+
:: Define service name
8+
set "ServiceName=SessionTrackerService"
9+
10+
:: Get base directory (where install.bat is located)
11+
set "BaseDir=%~dp0"
12+
13+
:: -------------------------------
14+
:: 1. Create and Start Service
15+
:: -------------------------------
16+
17+
cd /d "%BaseDir%service" || (
18+
echo Failed to change directory to service folder: "%BaseDir%service"
19+
exit /b 1
20+
)
21+
22+
sc query "%ServiceName%" >nul 2>&1
23+
if %errorlevel%==0 (
24+
echo Service already exists.
25+
) else (
26+
sc create "%ServiceName%" binPath= "\"%BaseDir%service\ShutdownTimeUpdaterService.exe\"" start= auto
27+
if errorlevel 1 (
28+
echo Failed to create service.
29+
exit /b 1
30+
)
31+
echo Service created successfully.
32+
)
33+
34+
sc start "%ServiceName%" >nul 2>&1
35+
echo Service started (or already running).
36+
37+
:: -------------------------------
38+
:: 2. Register GUI at Startup
39+
:: FIXED working directory
40+
:: -------------------------------
41+
42+
set "JavawPath=%BaseDir%jre\bin\javaw.exe"
43+
set "GuiDir=%BaseDir%gui"
44+
set "JarName=app-gui.jar"
45+
46+
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" ^
47+
/v SessionTracker ^
48+
/t REG_SZ ^
49+
/d "cmd /c start \"\" /D \"%GuiDir%\" \"%JavawPath%\" -jar \"%GuiDir%\%JarName%\"" ^
50+
/f
51+
52+
if errorlevel 1 (
53+
echo Failed to register GUI app to run at login.
54+
exit /b 1
55+
) else (
56+
echo GUI app successfully registered to run at login.
57+
)
58+
59+
:: -------------------------------
60+
:: 3. Restore original directory
61+
:: -------------------------------
62+
63+
cd /d "%OriginalDir%"
64+
echo Returned to original directory: %OriginalDir%
65+
66+
echo.
67+
echo =====================================
68+
echo Installation complete.
69+
echo =====================================
70+
71+
endlocal
72+
pause

uninstall.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@echo off
2+
echo Removing service...
3+
sc stop SessionTrackerService
4+
sc delete SessionTrackerService
5+
6+
echo Removing GUI app from startup...
7+
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v SessionTracker /f

0 commit comments

Comments
 (0)