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
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,12 @@ public async Task LoadPreviewAsync(CancellationToken cancellationToken)
unsafe
{
// This runs the preview handler in a separate process (prevhost.exe)
// TODO: Figure out how to get it to run in a low integrity level
if (!HandlerFactories.TryGetValue(clsid, out var factory))
{
var hr = PInvoke_FilePreviewer.CoGetClassObject(clsid, CLSCTX.CLSCTX_LOCAL_SERVER, null, typeof(IClassFactory).GUID, out object pFactory);
Marshal.ThrowExceptionForHR(hr);

// Storing the factory in memory helps makes the handlers load faster
// TODO: Maybe free them after some inactivity or when Peek quits?
factory = (IClassFactory)pFactory;
factory.LockServer(true);
HandlerFactories.AddOrUpdate(clsid, factory, (_, _) => factory);
Expand Down Expand Up @@ -213,6 +211,20 @@ public static bool IsItemSupported(IFileSystemItem item)
return !string.IsNullOrEmpty(GetPreviewHandlerGuid(item.Extension));
}

public static void ReleaseHandlerFactories()
{
foreach (var factory in HandlerFactories.Values)
{
try
{
Marshal.FinalReleaseComObject(factory);
}
catch
{
}
}
}

private static string? GetPreviewHandlerGuid(string fileExt)
{
const string PreviewHandlerKeyPath = "shellex\\{8895b1c6-b41f-4c1c-a562-0d564250836f}";
Expand Down
2 changes: 2 additions & 0 deletions src/modules/peek/Peek.UI/PeekXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Peek.Common;
using Peek.FilePreviewer;
using Peek.FilePreviewer.Models;
using Peek.FilePreviewer.Previewers;
using Peek.UI.Native;
using Peek.UI.Telemetry.Events;
using Peek.UI.Views;
Expand Down Expand Up @@ -111,6 +112,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
NativeEventWaiter.WaitForEventLoop(Constants.ShowPeekEvent(), OnPeekHotkey);
NativeEventWaiter.WaitForEventLoop(Constants.TerminatePeekEvent(), () =>
{
ShellPreviewHandlerPreviewer.ReleaseHandlerFactories();
EtwTrace?.Dispose();
Environment.Exit(0);
});
Expand Down
3 changes: 3 additions & 0 deletions src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Peek.Common.Constants;
using Peek.Common.Extensions;
using Peek.FilePreviewer.Models;
using Peek.FilePreviewer.Previewers;
using Peek.UI.Extensions;
using Peek.UI.Helpers;
using Peek.UI.Telemetry.Events;
Expand Down Expand Up @@ -204,6 +205,8 @@ private void Uninitialize()
ViewModel.ScalingFactor = 1;

this.Content.KeyUp -= Content_KeyUp;

ShellPreviewHandlerPreviewer.ReleaseHandlerFactories();
}

/// <summary>
Expand Down
16 changes: 11 additions & 5 deletions src/modules/peek/peek/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,19 @@ class Peek : public PowertoyModuleIface
{
ResetEvent(m_hInvokeEvent);
SetEvent(m_hTerminateEvent);
WaitForSingleObject(m_hProcess, 1500);
auto result = TerminateProcess(m_hProcess, 1);
if (result == 0)

HANDLE hProcess = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, m_processPid);
if (WaitForSingleObject(hProcess, 1500) == WAIT_TIMEOUT)
{
int error = GetLastError();
Logger::trace("Couldn't terminate the process. Last error: {}", error);
auto result = TerminateProcess(hProcess, 1);
if (result == 0)
{
int error = GetLastError();
Logger::trace("Couldn't terminate the process. Last error: {}", error);
}
}

CloseHandle(hProcess);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m_hProcess should be closed too

CloseHandle(m_hProcess);
m_hProcess = 0;
m_processPid = 0;
Expand Down
Loading