From 31af6fe11c75f6f3d952446023abb484b717a792 Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Sat, 7 Sep 2019 00:00:00 +0800 Subject: [PATCH] Remove dead codes --- Directory.Build.targets | 6 -- src/Core/WinSWCore/Util/ProcessHelper.cs | 15 --- src/Core/WinSWCore/WinSWCore.csproj | 2 - src/Core/WinSWCore/Wmi.cs | 130 +---------------------- 4 files changed, 4 insertions(+), 149 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index de3f613..3d3afd7 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -4,10 +4,4 @@ VNEXT - - - - - - diff --git a/src/Core/WinSWCore/Util/ProcessHelper.cs b/src/Core/WinSWCore/Util/ProcessHelper.cs index b1b71fb..51b4ce7 100644 --- a/src/Core/WinSWCore/Util/ProcessHelper.cs +++ b/src/Core/WinSWCore/Util/ProcessHelper.cs @@ -1,14 +1,9 @@ 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 { @@ -32,15 +27,6 @@ namespace winsw.Util try { 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)) - { - 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) @@ -49,7 +35,6 @@ namespace winsw.Util Logger.Info("Found child process: " + childProcessId + " Name: " + wmiObject["Name"]); childPids.Add(Convert.ToInt32(childProcessId)); } -#endif } catch (Exception ex) { diff --git a/src/Core/WinSWCore/WinSWCore.csproj b/src/Core/WinSWCore/WinSWCore.csproj index b51c4e8..1c6edc6 100644 --- a/src/Core/WinSWCore/WinSWCore.csproj +++ b/src/Core/WinSWCore/WinSWCore.csproj @@ -14,8 +14,6 @@ - - diff --git a/src/Core/WinSWCore/Wmi.cs b/src/Core/WinSWCore/Wmi.cs index 27cf415..045f30d 100755 --- a/src/Core/WinSWCore/Wmi.cs +++ b/src/Core/WinSWCore/Wmi.cs @@ -1,15 +1,8 @@ using System; -using System.Diagnostics; -#if !FEATURE_CIM using System.Management; -#endif using System.Reflection; using System.Text; using DynamicProxy; -#if FEATURE_CIM -using Microsoft.Management.Infrastructure; -using Microsoft.Management.Infrastructure.Generic; -#endif namespace WMI { @@ -84,27 +77,14 @@ namespace WMI /// public interface IWmiObject { - /// - /// Reflect updates made to this object to the WMI provider. - /// - void Commit(); } public sealed class WmiRoot { -#if FEATURE_CIM - private const string CimNamespace = "root/cimv2"; - - private readonly CimSession cimSession; -#else private readonly ManagementScope wmiScope; -#endif - public WmiRoot(string? machineName = null) + public WmiRoot() { -#if FEATURE_CIM - this.cimSession = CimSession.Create(machineName); -#else ConnectionOptions options = new ConnectionOptions { EnablePrivileges = true, @@ -112,15 +92,8 @@ namespace WMI Authentication = AuthenticationLevel.PacketPrivacy, }; - string path; - - if (machineName != null) - path = $@"\\{machineName}\root\cimv2"; - else - path = @"\root\cimv2"; - wmiScope = new ManagementScope(path, options); - wmiScope.Connect(); -#endif + this.wmiScope = new ManagementScope(@"\\.\root\cimv2", options); + this.wmiScope.Connect(); } private static string Capitalize(string s) @@ -132,36 +105,13 @@ namespace WMI { public abstract object? Invoke(object proxy, MethodInfo method, object[] arguments); -#if FEATURE_CIM - protected void CheckError(CimMethodResult result) - { - uint code = (uint)result.ReturnValue.Value; - if (code != 0) - throw new WmiException((ReturnValue)code); - } -#else protected void CheckError(ManagementBaseObject result) { uint code = (uint)result["returnValue"]; if (code != 0) throw new WmiException((ReturnValue)code); } -#endif -#if FEATURE_CIM - protected CimMethodParametersCollection GetMethodParameters(CimClass cimClass, string methodName, ParameterInfo[] methodParameters, object[] arguments) - { - CimMethodParametersCollection cimParameters = new CimMethodParametersCollection(); - CimReadOnlyKeyedCollection cimParameterDeclarations = cimClass.CimClassMethods[methodName].Parameters; - for (int i = 0; i < arguments.Length; i++) - { - string capitalizedName = Capitalize(methodParameters[i].Name!); - cimParameters.Add(CimMethodParameter.Create(capitalizedName, arguments[i], cimParameterDeclarations[capitalizedName].CimType, CimFlags.None)); - } - - return cimParameters; - } -#else protected ManagementBaseObject GetMethodParameters(ManagementObject wmiObject, string methodName, ParameterInfo[] methodParameters, object[] arguments) { ManagementBaseObject wmiParameters = wmiObject.GetMethodParameters(methodName); @@ -173,25 +123,13 @@ namespace WMI return wmiParameters; } -#endif } private class InstanceHandler : BaseHandler, IWmiObject { -#if FEATURE_CIM - private readonly CimSession cimSession; - private readonly CimInstance cimInstance; - - public InstanceHandler(CimSession cimSession, CimInstance cimInstance) - { - this.cimSession = cimSession; - this.cimInstance = cimInstance; - } -#else private readonly ManagementObject wmiObject; public InstanceHandler(ManagementObject wmiObject) => this.wmiObject = wmiObject; -#endif public override object? Invoke(object proxy, MethodInfo method, object[] arguments) { @@ -203,72 +141,34 @@ namespace WMI // TODO: proper property support if (method.Name.StartsWith("set_")) { -#if FEATURE_CIM - CimProperty cimProperty = this.cimInstance.CimInstanceProperties[method.Name.Substring(4)]; - Debug.Assert((cimProperty.Flags & CimFlags.ReadOnly) == CimFlags.None); - cimProperty.Value = arguments[0]; -#else this.wmiObject[method.Name.Substring(4)] = arguments[0]; -#endif return null; } if (method.Name.StartsWith("get_")) { -#if FEATURE_CIM - return this.cimInstance.CimInstanceProperties[method.Name.Substring(4)].Value; -#else return this.wmiObject[method.Name.Substring(4)]; -#endif } string methodName = method.Name; -#if FEATURE_CIM - using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null : - this.GetMethodParameters(this.cimInstance.CimClass, methodName, method.GetParameters(), arguments); - using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.cimInstance, methodName, cimParameters); - this.CheckError(result); -#else using ManagementBaseObject? wmiParameters = arguments.Length == 0 ? null : this.GetMethodParameters(this.wmiObject, methodName, method.GetParameters(), arguments); using ManagementBaseObject result = this.wmiObject.InvokeMethod(methodName, wmiParameters, null); this.CheckError(result); -#endif return null; } - - public void Commit() - { -#if !FEATURE_CIM - this.wmiObject.Put(); -#endif - } } private class ClassHandler : BaseHandler { -#if FEATURE_CIM - private readonly CimSession cimSession; - private readonly CimClass cimClass; -#else private readonly ManagementClass wmiClass; -#endif private readonly string className; -#if FEATURE_CIM - public ClassHandler(CimSession cimSession, string className) - { - this.cimSession = cimSession; - this.cimClass = cimSession.GetClass(CimNamespace, className); - this.className = className; - } -#else public ClassHandler(ManagementScope wmiScope, string className) { this.wmiClass = new ManagementClass(wmiScope, new ManagementPath(className), null); this.className = className; } -#endif public override object? Invoke(object proxy, MethodInfo method, object[] arguments) { @@ -286,13 +186,6 @@ namespace WMI query.Append(' ').Append(Capitalize(methodParameters[i].Name!)).Append(" = '").Append(arguments[i]).Append('\''); } -#if FEATURE_CIM - // TODO: support collections - foreach (CimInstance cimInstance in this.cimSession.QueryInstances(CimNamespace, "WQL", query.ToString())) - { - return ProxyFactory.Create(new InstanceHandler(this.cimSession, cimInstance), method.ReturnType, true); - } -#else using ManagementObjectSearcher searcher = new ManagementObjectSearcher(this.wmiClass.Scope, new ObjectQuery(query.ToString())); using ManagementObjectCollection results = searcher.Get(); // TODO: support collections @@ -300,23 +193,15 @@ namespace WMI { return ProxyFactory.Create(new InstanceHandler(wmiObject), method.ReturnType, true); } -#endif return null; } string methodName = method.Name; -#if FEATURE_CIM - using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null : - this.GetMethodParameters(this.cimClass, methodName, methodParameters, arguments); - using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.className, methodName, cimParameters); - this.CheckError(result); -#else using ManagementBaseObject? wmiParameters = arguments.Length == 0 ? null : this.GetMethodParameters(this.wmiClass, methodName, methodParameters, arguments); using ManagementBaseObject result = this.wmiClass.InvokeMethod(methodName, wmiParameters, null); this.CheckError(result); -#endif return null; } } @@ -328,14 +213,7 @@ namespace WMI { WmiClassName className = (WmiClassName)typeof(T).GetCustomAttributes(typeof(WmiClassName), false)[0]; - return (T)ProxyFactory.Create( -#if FEATURE_CIM - new ClassHandler(this.cimSession, className.Name), -#else - new ClassHandler(this.wmiScope, className.Name), -#endif - typeof(T), - true); + return (T)ProxyFactory.Create(new ClassHandler(this.wmiScope, className.Name), typeof(T), true); } } }