[New Utility]Mouse Without Borders

* Integrate Mouse Without Borders into PowerToys

---------

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
This commit is contained in:
Andrey Nekrasov
2023-05-15 23:32:26 +01:00
committed by Jaime Bernardo
parent a0b9af039d
commit 29eebe16a4
304 changed files with 37234 additions and 133 deletions

View File

@@ -14,5 +14,17 @@ namespace ManagedCommon
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("psapi.dll", SetLastError = true)]
internal static extern bool EnumProcesses(int[] processIds, uint arraySizeBytes, out uint bytesCopied);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool QueryFullProcessImageName(IntPtr hProcess, uint dwFlags, System.Text.StringBuilder lpExeName, ref uint lpdwSize);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool GetExitCodeProcess(IntPtr hProcess, out uint lpExitCode);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool CloseHandle(IntPtr hObject);
}
}

View File

@@ -33,5 +33,59 @@ namespace ManagedCommon
}
});
}
private static readonly string PowerToysRunnerProcessName = "PowerToys.exe";
// In case we don't have a permission to open user's processes with a SYNCHRONIZE access right, e.g. LocalSystem processes, we could use GetExitCodeProcess to check the process' exit code periodically.
public static void WaitForPowerToysRunnerExitFallback(Action act)
{
int[] processIds = new int[1024];
uint bytesCopied;
NativeMethods.EnumProcesses(processIds, (uint)processIds.Length * sizeof(uint), out bytesCopied);
const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
var handleAccess = PROCESS_QUERY_LIMITED_INFORMATION;
IntPtr runnerHandle = IntPtr.Zero;
foreach (var processId in processIds)
{
IntPtr hProcess = NativeMethods.OpenProcess(handleAccess, false, processId);
System.Text.StringBuilder name = new System.Text.StringBuilder(1024);
uint length = 1024;
if (hProcess == IntPtr.Zero || !NativeMethods.QueryFullProcessImageName(hProcess, 0, name, ref length))
{
continue;
}
if (System.IO.Path.GetFileName(name.ToString()) == PowerToysRunnerProcessName)
{
runnerHandle = hProcess;
break;
}
}
if (runnerHandle == IntPtr.Zero)
{
Logger.LogError("Couldn't determine PowerToys.exe pid");
return;
}
Task.Run(() =>
{
const int STILL_ACTIVE = 0x103;
uint exit_status;
do
{
System.Threading.Thread.Sleep(1000);
NativeMethods.GetExitCodeProcess(runnerHandle, out exit_status);
}
while (exit_status == STILL_ACTIVE);
NativeMethods.CloseHandle(runnerHandle);
act.Invoke();
});
}
}
}