From 165ffed9e6a4df8755be502044e50778ae49abc6 Mon Sep 17 00:00:00 2001
From: ryanbodrug-microsoft
<56318517+ryanbodrug-microsoft@users.noreply.github.com>
Date: Thu, 18 Jun 2020 12:56:12 -0700
Subject: [PATCH] Fix CA 1060 by moving PInvokes to a common NativeMethodsClass
Severity Code Description Project File Line Suppression State
Warning CA1060 Move pinvokes to native methods class PowerLauncher C:\Repos\PowerToys\src\modules\launcher\PowerLauncher\App.xaml.cs 24 Active
---
PowerToys.sln | 7 ++++
src/common/ManagedCommon/ManagedCommon.csproj | 22 ++++++++++++
src/common/ManagedCommon/NativeMethods.cs | 14 ++++++++
src/common/ManagedCommon/RunnerHelper.cs | 35 +++++++++++++++++++
.../Telemetry/Events/DebugEvent.cs | 14 ++++++++
...rosoft.PowerToys.Settings.UI.Runner.csproj | 1 +
.../Program.cs | 25 ++-----------
.../editor/FancyZonesEditor/App.xaml.cs | 25 ++-----------
.../FancyZonesEditor/FancyZonesEditor.csproj | 6 ++++
.../launcher/PowerLauncher/App.xaml.cs | 25 ++-----------
.../PowerLauncher/PowerLauncher.csproj | 1 +
11 files changed, 106 insertions(+), 69 deletions(-)
create mode 100644 src/common/ManagedCommon/ManagedCommon.csproj
create mode 100644 src/common/ManagedCommon/NativeMethods.cs
create mode 100644 src/common/ManagedCommon/RunnerHelper.cs
create mode 100644 src/common/ManagedTelemetry/Telemetry/Events/DebugEvent.cs
diff --git a/PowerToys.sln b/PowerToys.sln
index 222a1851bf..fe7c8222ce 100644
--- a/PowerToys.sln
+++ b/PowerToys.sln
@@ -253,6 +253,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "os-detection", "src\common\
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "KeyboardManagerTest", "src\modules\keyboardmanager\test\KeyboardManagerTest.vcxproj", "{62173D9A-6724-4C00-A1C8-FB646480A9EC}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ManagedCommon", "src\common\ManagedCommon\ManagedCommon.csproj", "{4AED67B6-55FD-486F-B917-E543DEE2CB3C}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@@ -495,6 +497,10 @@ Global
{62173D9A-6724-4C00-A1C8-FB646480A9EC}.Debug|x64.Build.0 = Debug|x64
{62173D9A-6724-4C00-A1C8-FB646480A9EC}.Release|x64.ActiveCfg = Release|x64
{62173D9A-6724-4C00-A1C8-FB646480A9EC}.Release|x64.Build.0 = Release|x64
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C}.Debug|x64.Build.0 = Debug|Any CPU
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C}.Release|x64.ActiveCfg = Release|Any CPU
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -564,6 +570,7 @@ Global
{5D00D290-4016-4CFE-9E41-1E7C724509BA} = {1AFB6476-670D-4E80-A464-657E01DFF482}
{E6410BFC-B341-498C-8C67-312C20CDD8D5} = {1AFB6476-670D-4E80-A464-657E01DFF482}
{62173D9A-6724-4C00-A1C8-FB646480A9EC} = {38BDB927-829B-4C65-9CD9-93FB05D66D65}
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C} = {1AFB6476-670D-4E80-A464-657E01DFF482}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}
diff --git a/src/common/ManagedCommon/ManagedCommon.csproj b/src/common/ManagedCommon/ManagedCommon.csproj
new file mode 100644
index 0000000000..d72c74a5af
--- /dev/null
+++ b/src/common/ManagedCommon/ManagedCommon.csproj
@@ -0,0 +1,22 @@
+
+
+
+ netstandard2.0
+
+
+
+ true
+
+
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
+
+
+
+
+
diff --git a/src/common/ManagedCommon/NativeMethods.cs b/src/common/ManagedCommon/NativeMethods.cs
new file mode 100644
index 0000000000..ff556351cb
--- /dev/null
+++ b/src/common/ManagedCommon/NativeMethods.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace ManagedCommon
+{
+ internal static class NativeMethods
+ {
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
+
+ [DllImport("kernel32.dll", SetLastError = true)]
+ internal static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
+ }
+}
diff --git a/src/common/ManagedCommon/RunnerHelper.cs b/src/common/ManagedCommon/RunnerHelper.cs
new file mode 100644
index 0000000000..eab9d0175d
--- /dev/null
+++ b/src/common/ManagedCommon/RunnerHelper.cs
@@ -0,0 +1,35 @@
+using Microsoft.PowerToys.Telemetry;
+using Microsoft.PowerToys.Telemetry.Events;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ManagedCommon
+{
+ public static class RunnerHelper
+ {
+ public static void WaitForPowerToysRunner(int powerToysPID)
+ {
+ var stackTrace = new StackTrace();
+ var assembly = Assembly.GetCallingAssembly().GetName();
+ var callingMethod = stackTrace.GetFrame(1).GetMethod().Name;
+ PowerToysTelemetry.Log.WriteEvent(new DebugEvent() { Message = $"[{assembly}][{callingMethod}]WaitForPowerToysRunner waiting for Event powerToysPID={powerToysPID}" });
+ Task.Run(() =>
+ {
+ const uint INFINITE = 0xFFFFFFFF;
+ const uint WAIT_OBJECT_0 = 0x00000000;
+ const uint SYNCHRONIZE = 0x00100000;
+
+ IntPtr powerToysProcHandle = NativeMethods.OpenProcess(SYNCHRONIZE, false, powerToysPID);
+ if (NativeMethods.WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
+ {
+ PowerToysTelemetry.Log.WriteEvent(new DebugEvent() { Message = $"[{assembly}][{callingMethod}]WaitForPowerToysRunner Event Notified powerToysPID={powerToysPID}" });
+ Environment.Exit(0);
+ }
+ });
+ }
+ }
+}
diff --git a/src/common/ManagedTelemetry/Telemetry/Events/DebugEvent.cs b/src/common/ManagedTelemetry/Telemetry/Events/DebugEvent.cs
new file mode 100644
index 0000000000..170670eeb6
--- /dev/null
+++ b/src/common/ManagedTelemetry/Telemetry/Events/DebugEvent.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.Tracing;
+using System.Text;
+
+namespace Microsoft.PowerToys.Telemetry.Events
+{
+ [EventData]
+ public class DebugEvent : EventBase, IEvent
+ {
+ public string Message { get; set; }
+ public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServicePerformance;
+ }
+}
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
index f9ced1d7c3..fb765d4ca9 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
@@ -72,6 +72,7 @@
+
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs
index 8449b0eba2..5701f06c2b 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs
@@ -8,6 +8,7 @@ using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using interop;
+using ManagedCommon;
using Windows.UI.Popups;
namespace Microsoft.PowerToys.Settings.UI.Runner
@@ -57,7 +58,7 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
IsUserAnAdmin = false;
}
- WaitForPowerToysRunner();
+ RunnerHelper.WaitForPowerToysRunner(PowerToysPID);
ipcmanager = new TwoWayPipeMessageIPCManaged(args[1], args[0], null);
ipcmanager.Start();
@@ -78,27 +79,5 @@ namespace Microsoft.PowerToys.Settings.UI.Runner
{
return ipcmanager;
}
-
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
-
- internal static void WaitForPowerToysRunner()
- {
- Task.Run(() =>
- {
- const uint INFINITE = 0xFFFFFFFF;
- const uint WAIT_OBJECT_0 = 0x00000000;
- const uint SYNCHRONIZE = 0x00100000;
-
- IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, PowerToysPID);
- if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
- {
- Environment.Exit(0);
- }
- });
- }
}
}
diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
index 95bd3b13bc..c3633800af 100644
--- a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
+++ b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
@@ -7,6 +7,7 @@ using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using FancyZonesEditor.Models;
+using ManagedCommon;
namespace FancyZonesEditor
{
@@ -24,7 +25,7 @@ namespace FancyZonesEditor
private void OnStartup(object sender, StartupEventArgs e)
{
- WaitForPowerToysRunner();
+ RunnerHelper.WaitForPowerToysRunner(Settings.PowerToysPID);
LayoutModel foundModel = null;
@@ -62,27 +63,5 @@ namespace FancyZonesEditor
overlay.Show();
overlay.DataContext = foundModel;
}
-
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
-
- private void WaitForPowerToysRunner()
- {
- Task.Run(() =>
- {
- const uint INFINITE = 0xFFFFFFFF;
- const uint WAIT_OBJECT_0 = 0x00000000;
- const uint SYNCHRONIZE = 0x00100000;
-
- IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, Settings.PowerToysPID);
- if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
- {
- Environment.Exit(0);
- }
- });
- }
}
}
diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj
index 416e76ec67..8572c7a470 100644
--- a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj
+++ b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj
@@ -258,5 +258,11 @@
+
+
+ {4AED67B6-55FD-486F-B917-E543DEE2CB3C}
+ ManagedCommon
+
+
\ No newline at end of file
diff --git a/src/modules/launcher/PowerLauncher/App.xaml.cs b/src/modules/launcher/PowerLauncher/App.xaml.cs
index 32cb27e337..082d114f60 100644
--- a/src/modules/launcher/PowerLauncher/App.xaml.cs
+++ b/src/modules/launcher/PowerLauncher/App.xaml.cs
@@ -1,3 +1,4 @@
+using ManagedCommon;
using Microsoft.PowerLauncher.Telemetry;
using Microsoft.PowerToys.Telemetry;
using System;
@@ -55,7 +56,7 @@ namespace PowerLauncher
private void OnStartup(object sender, StartupEventArgs e)
{
- WaitForPowerToysRunner();
+ RunnerHelper.WaitForPowerToysRunner(_powerToysPid);
var bootTime = new System.Diagnostics.Stopwatch();
bootTime.Start();
@@ -121,28 +122,6 @@ namespace PowerLauncher
Current.SessionEnding += (s, e) => Dispose();
}
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
-
- private static void WaitForPowerToysRunner()
- {
- Task.Run(() =>
- {
- const uint INFINITE = 0xFFFFFFFF;
- const uint WAIT_OBJECT_0 = 0x00000000;
- const uint SYNCHRONIZE = 0x00100000;
-
- IntPtr powerToysProcHandle = OpenProcess(SYNCHRONIZE, false, _powerToysPid);
- if (WaitForSingleObject(powerToysProcHandle, INFINITE) == WAIT_OBJECT_0)
- {
- Environment.Exit(0);
- }
- });
- }
-
///
/// let exception throw as normal is better for Debug
///
diff --git a/src/modules/launcher/PowerLauncher/PowerLauncher.csproj b/src/modules/launcher/PowerLauncher/PowerLauncher.csproj
index 7314cfa449..5d2f78749e 100644
--- a/src/modules/launcher/PowerLauncher/PowerLauncher.csproj
+++ b/src/modules/launcher/PowerLauncher/PowerLauncher.csproj
@@ -84,6 +84,7 @@
+