Skip to content

Commit

Permalink
UACWindowFocusHelper thread properly set as background thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardog committed May 28, 2024
1 parent 7e7dc33 commit 5e380bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/gsudo/Helpers/ServiceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,19 @@ internal static SafeProcessHandle StartService(int? allowedPid, TimeSpan? cacheD
if (SecurityHelper.IsMemberOfLocalAdmins() && InputArguments.GetIntegrityLevel() >= IntegrityLevel.High)
{
// UAC Popup doesnt always have focus, so we try to bring it to the front.
new Thread(UACWindowFocusHelper.FocusUacWindow).Start();

UACWindowFocusHelper.StartBackgroundThreadToFocusUacWindow();
ret = ProcessFactory.StartElevatedDetached(ownExe, commandLine, !InputArguments.Debug).GetSafeProcessHandle();
}
else
{
ret = ProcessFactory.StartDetached(ownExe, commandLine, null, !InputArguments.Debug).GetSafeProcessHandle();
}
}
}
else
{
// UAC Popup doesnt always have focus, so we try to bring it to the front.
new Thread(UACWindowFocusHelper.FocusUacWindow).Start();

UACWindowFocusHelper.StartBackgroundThreadToFocusUacWindow();
ret = ProcessFactory.StartElevatedDetached(ownExe, commandLine, !InputArguments.Debug).GetSafeProcessHandle();
}

Expand Down
8 changes: 8 additions & 0 deletions src/gsudo/Helpers/UACWindowFocusHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using gsudo.Native;
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace gsudo.Helpers
{
Expand All @@ -9,6 +10,13 @@ internal class UACWindowFocusHelper
[DllImport("user32.dll", SetLastError = true)]

Check warning on line 10 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / Test

Specify marshaling for P/Invoke string arguments (https://docs.microsoft.com/visualstudio/code-quality/ca2101-specify-marshaling-for-p-invoke-string-arguments)

Check warning on line 10 in src/gsudo/Helpers/UACWindowFocusHelper.cs

View workflow job for this annotation

GitHub Actions / build / Test

Specify marshaling for P/Invoke string arguments (https://docs.microsoft.com/visualstudio/code-quality/ca2101-specify-marshaling-for-p-invoke-string-arguments)
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

internal static void StartBackgroundThreadToFocusUacWindow()
{
var focusThread = new Thread(UACWindowFocusHelper.FocusUacWindow);
focusThread.IsBackground = true;
focusThread.Start();
}

internal static void FocusUacWindow()
{
try
Expand Down

0 comments on commit 5e380bd

Please sign in to comment.