-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve UAC prompt handling by focusing the UAC window, useful …
…when UAC set as Do Not Dim. Fixes #85
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using gsudo.Native; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace gsudo.Helpers | ||
{ | ||
internal class UACWindowFocusHelper | ||
{ | ||
[DllImport("user32.dll", SetLastError = true)] | ||
Check warning on line 9 in src/gsudo/Helpers/UACWindowFocusHelper.cs GitHub Actions / Test
|
||
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | ||
|
||
internal static void FocusUacWindow() | ||
{ | ||
try | ||
{ | ||
for (int i = 0; i < 10; i++) | ||
{ | ||
// Wait a moment to allow the UAC prompt to appear | ||
System.Threading.Thread.Sleep(100); | ||
|
||
// Find the UAC window | ||
string classname = "Credential Dialog Xaml Host"; // Found using Visual Studio spyxx_amd64.exe, this is the value for Windows 10 & 11. | ||
IntPtr uacWindow = FindWindow(classname, null); | ||
if (uacWindow != IntPtr.Zero) | ||
{ | ||
// Set focus to the UAC window | ||
WindowApi.SetForegroundWindow(uacWindow); | ||
Check warning on line 27 in src/gsudo/Helpers/UACWindowFocusHelper.cs GitHub Actions / Test
Check warning on line 27 in src/gsudo/Helpers/UACWindowFocusHelper.cs GitHub Actions / Test
|
||
return; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.Instance.Log("Error searching for UAC Window: " + ex.ToString(), LogLevel.Debug); | ||
} | ||
} | ||
} | ||
} |