Skip to content

Commit

Permalink
Merge pull request #366 from NextTurn/mmi
Browse files Browse the repository at this point in the history
Migrate from `System.Management` to MMI
  • Loading branch information
oleg-nenashev committed Feb 6, 2020
2 parents b36aff0 + ec6d9b0 commit eb96c4a
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 68 deletions.
5 changes: 5 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
<DefineConstants>VNEXT</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<!-- https://docs.microsoft.com/windows/win32/wmisdk/common-information-model -->
<DefineConstants>$(DefineConstants);FEATURE_CIM</DefineConstants>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Core/ServiceWrapper/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public static void Run(string[] _args, ServiceDescriptor? descriptor = null)
"\"" + descriptor.ExecutablePath + "\"",
ServiceType.OwnProcess,
ErrorControl.UserNotified,
descriptor.StartMode,
descriptor.StartMode.ToString(),
descriptor.Interactive,
username,
password,
Expand Down
1 change: 0 additions & 1 deletion src/Core/ServiceWrapper/winsw.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.1'">
<PackageReference Include="ilmerge" Version="3.0.29" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" />
<Reference Include="System.Management" />
<Reference Include="System.ServiceProcess" />
</ItemGroup>

Expand Down
25 changes: 21 additions & 4 deletions src/Core/WinSWCore/Util/ProcessHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
#if !FEATURE_CIM
using System.Management;
#endif
using System.Threading;
using log4net;
#if FEATURE_CIM
using Microsoft.Management.Infrastructure;
#endif

namespace winsw.Util
{
Expand All @@ -26,13 +31,25 @@ public static List<int> GetChildPids(int pid)

try
{
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
foreach (var mo in searcher.Get())
string query = "SELECT * FROM Win32_Process WHERE ParentProcessID = " + pid;
#if FEATURE_CIM
using CimSession session = CimSession.Create(null);
foreach (CimInstance instance in session.QueryInstances("root/cimv2", "WQL", query))
{
var childProcessId = mo["ProcessID"];
Logger.Info("Found child process: " + childProcessId + " Name: " + mo["Name"]);
object childProcessId = instance.CimInstanceProperties["ProcessID"].Value;
Logger.Info("Found child process: " + childProcessId + " Name: " + instance.CimInstanceProperties["Name"].Value);
childPids.Add(Convert.ToInt32(childProcessId));
}
#else
using ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
using ManagementObjectCollection results = searcher.Get();
foreach (ManagementBaseObject wmiObject in results)
{
var childProcessId = wmiObject["ProcessID"];
Logger.Info("Found child process: " + childProcessId + " Name: " + wmiObject["Name"]);
childPids.Add(Convert.ToInt32(childProcessId));
}
#endif
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/WinSWCore/WinSWCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageReference Include="System.Diagnostics.EventLog" Version="4.7.0" />
<PackageReference Include="System.Management" Version="4.7.0" />
</ItemGroup>

<!-- error NU1605: Detected package downgrade: log4net 2.0.8 -->
Expand Down

0 comments on commit eb96c4a

Please sign in to comment.