Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Reentry.App/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ QueryFullProcessImageNameW
OpenProcess
CloseHandle
GetCurrentProcessId
SetWindowSubclass
RemoveWindowSubclass
DefSubclassProc
ShowWindow
UpdateWindow
3 changes: 3 additions & 0 deletions src/Reentry.App/Reentry.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<ApplicationIcon>Assets\reentry.ico</ApplicationIcon>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<!-- WASDK self-contained rejects AnyCPU unless a RID is set. `dotnet build`
defaults Platform to AnyCPU; win-x64 matches publish.ps1 / release. -->
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' and ('$(Platform)' == '' or '$(Platform)' == 'AnyCPU')">win-x64</RuntimeIdentifier>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
52 changes: 41 additions & 11 deletions src/Reentry.App/Services/EndSessionHook.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Controls;
using Windows.Win32.UI.WindowsAndMessaging;

namespace Reentry.App.Services;

Expand All @@ -14,11 +11,22 @@ public sealed class EndSessionHook : IDisposable
{
private const uint WmQueryEndSession = 0x0011;
private const uint WmEndSession = 0x0016;
private const nuint SubclassId = 1;

private readonly Action _onEndSession;
private HWND _hwnd;
private SUBCLASSPROC? _proc;
private nint _hwnd;
private SubclassProc? _proc;
private bool _attached;

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate nint SubclassProc(
nint hwnd,
uint msg,
nuint wParam,
nint lParam,
nuint idSubclass,
nuint refData);

public EndSessionHook(Action onEndSession)
{
_onEndSession = onEndSession;
Expand All @@ -38,30 +46,52 @@ public void Attach(Window window)
public void Attach(nint hwnd)
{
Detach();
_hwnd = new HWND(hwnd);
_hwnd = hwnd;
_proc = WndProc;
_attached = PInvoke.SetWindowSubclass(_hwnd, _proc, 1, 0);
_attached = SetWindowSubclass(_hwnd, _proc, SubclassId, 0);
}

private LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam, nuint id, nuint data)
private nint WndProc(nint hwnd, uint msg, nuint wParam, nint lParam, nuint id, nuint data)
{
if (msg is WmQueryEndSession or WmEndSession)
{
try { _onEndSession(); }
catch { /* never block shutdown */ }
}

return PInvoke.DefSubclassProc(hwnd, msg, wParam, lParam);
return DefSubclassProc(hwnd, msg, wParam, lParam);
}

private void Detach()
{
if (_attached && _proc is not null)
{
PInvoke.RemoveWindowSubclass(_hwnd, _proc, 1);
RemoveWindowSubclass(_hwnd, _proc, SubclassId);
_attached = false;
}
}

public void Dispose() => Detach();

[DllImport("comctl32.dll", ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowSubclass(
nint hWnd,
SubclassProc pfnSubclass,
nuint uIdSubclass,
nuint dwRefData);

[DllImport("comctl32.dll", ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RemoveWindowSubclass(
nint hWnd,
SubclassProc pfnSubclass,
nuint uIdSubclass);

[DllImport("comctl32.dll", ExactSpelling = true)]
private static extern nint DefSubclassProc(
nint hWnd,
uint uMsg,
nuint wParam,
nint lParam);
}
1 change: 1 addition & 0 deletions src/Reentry.App/Services/Win32EventLogReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.Eventing.Reader;
using Reentry.Core.Abstractions;
using Reentry.Core.Models;
using EventLogRecord = Reentry.Core.Models.EventLogRecord;

namespace Reentry.App.Services;

Expand Down
6 changes: 3 additions & 3 deletions src/Reentry.App/Services/Win32ProcessProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public IReadOnlyList<LiveProcess> GetProcesses()
try { started = process.StartTime.ToUniversalTime(); }
catch { started = DateTimeOffset.UtcNow; }

list.Add(new LiveProcess(process.Id, exe, commandLine: null, started));
list.Add(new LiveProcess(process.Id, exe, CommandLine: null, StartedUtc: started));
}
catch
{
Expand All @@ -44,10 +44,10 @@ public IReadOnlyList<LiveWindow> GetVisibleWindows()
return true;

uint pid = 0;
_ = PInvoke.GetWindowThreadProcessId(hwnd, &pid);
PInvoke.GetWindowThreadProcessId(hwnd, &pid);
var title = ReadTitle(hwnd);
var exe = QueryImage((int)pid) ?? "";
list.Add(new LiveWindow(hwnd.Value, (int)pid, title, exe, IsVisible: true));
list.Add(new LiveWindow((long)hwnd.Value, (int)pid, title, exe, IsVisible: true));
return true;
}, 0);
return list;
Expand Down
Loading