Rename to FEATURE_CIM

pull/366/head
NextTurn 2019-08-19 00:00:00 +08:00
parent 30c5994c42
commit ec6d9b0a69
No known key found for this signature in database
GPG Key ID: 17A0D50ADDE1A0C4
3 changed files with 21 additions and 20 deletions

View File

@ -5,7 +5,8 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> <PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<DefineConstants>$(DefineConstants);CIM</DefineConstants> <!-- https://docs.microsoft.com/windows/win32/wmisdk/common-information-model -->
<DefineConstants>$(DefineConstants);FEATURE_CIM</DefineConstants>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -1,12 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
#if !CIM #if !FEATURE_CIM
using System.Management; using System.Management;
#endif #endif
using System.Threading; using System.Threading;
using log4net; using log4net;
#if CIM #if FEATURE_CIM
using Microsoft.Management.Infrastructure; using Microsoft.Management.Infrastructure;
#endif #endif
@ -32,7 +32,7 @@ namespace winsw.Util
try try
{ {
string query = "SELECT * FROM Win32_Process WHERE ParentProcessID = " + pid; string query = "SELECT * FROM Win32_Process WHERE ParentProcessID = " + pid;
#if CIM #if FEATURE_CIM
using CimSession session = CimSession.Create(null); using CimSession session = CimSession.Create(null);
foreach (CimInstance instance in session.QueryInstances("root/cimv2", "WQL", query)) foreach (CimInstance instance in session.QueryInstances("root/cimv2", "WQL", query))
{ {

View File

@ -1,11 +1,11 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
#if !CIM #if !FEATURE_CIM
using System.Management; using System.Management;
#endif #endif
using System.Reflection; using System.Reflection;
using DynamicProxy; using DynamicProxy;
#if CIM #if FEATURE_CIM
using Microsoft.Management.Infrastructure; using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Generic; using Microsoft.Management.Infrastructure.Generic;
#endif #endif
@ -91,7 +91,7 @@ namespace WMI
public sealed class WmiRoot public sealed class WmiRoot
{ {
#if CIM #if FEATURE_CIM
private const string CimNamespace = "root/cimv2"; private const string CimNamespace = "root/cimv2";
private readonly CimSession cimSession; private readonly CimSession cimSession;
@ -101,7 +101,7 @@ namespace WMI
public WmiRoot(string? machineName = null) public WmiRoot(string? machineName = null)
{ {
#if CIM #if FEATURE_CIM
this.cimSession = CimSession.Create(machineName); this.cimSession = CimSession.Create(machineName);
#else #else
ConnectionOptions options = new ConnectionOptions ConnectionOptions options = new ConnectionOptions
@ -131,7 +131,7 @@ namespace WMI
{ {
public abstract object? Invoke(object proxy, MethodInfo method, object[] arguments); public abstract object? Invoke(object proxy, MethodInfo method, object[] arguments);
#if CIM #if FEATURE_CIM
protected void CheckError(CimMethodResult result) protected void CheckError(CimMethodResult result)
{ {
uint code = (uint)result.ReturnValue.Value; uint code = (uint)result.ReturnValue.Value;
@ -147,7 +147,7 @@ namespace WMI
} }
#endif #endif
#if CIM #if FEATURE_CIM
protected CimMethodParametersCollection GetMethodParameters(CimClass cimClass, string methodName, ParameterInfo[] methodParameters, object[] arguments) protected CimMethodParametersCollection GetMethodParameters(CimClass cimClass, string methodName, ParameterInfo[] methodParameters, object[] arguments)
{ {
CimMethodParametersCollection cimParameters = new CimMethodParametersCollection(); CimMethodParametersCollection cimParameters = new CimMethodParametersCollection();
@ -177,7 +177,7 @@ namespace WMI
private class InstanceHandler : BaseHandler, IWmiObject private class InstanceHandler : BaseHandler, IWmiObject
{ {
#if CIM #if FEATURE_CIM
private readonly CimSession cimSession; private readonly CimSession cimSession;
private readonly CimInstance cimInstance; private readonly CimInstance cimInstance;
@ -202,7 +202,7 @@ namespace WMI
// TODO: proper property support // TODO: proper property support
if (method.Name.StartsWith("set_")) if (method.Name.StartsWith("set_"))
{ {
#if CIM #if FEATURE_CIM
CimProperty cimProperty = this.cimInstance.CimInstanceProperties[method.Name.Substring(4)]; CimProperty cimProperty = this.cimInstance.CimInstanceProperties[method.Name.Substring(4)];
Debug.Assert((cimProperty.Flags & CimFlags.ReadOnly) == CimFlags.None); Debug.Assert((cimProperty.Flags & CimFlags.ReadOnly) == CimFlags.None);
cimProperty.Value = arguments[0]; cimProperty.Value = arguments[0];
@ -214,7 +214,7 @@ namespace WMI
if (method.Name.StartsWith("get_")) if (method.Name.StartsWith("get_"))
{ {
#if CIM #if FEATURE_CIM
return this.cimInstance.CimInstanceProperties[method.Name.Substring(4)].Value; return this.cimInstance.CimInstanceProperties[method.Name.Substring(4)].Value;
#else #else
return this.wmiObject[method.Name.Substring(4)]; return this.wmiObject[method.Name.Substring(4)];
@ -222,7 +222,7 @@ namespace WMI
} }
string methodName = method.Name; string methodName = method.Name;
#if CIM #if FEATURE_CIM
using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null : using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null :
this.GetMethodParameters(this.cimInstance.CimClass, methodName, method.GetParameters(), arguments); this.GetMethodParameters(this.cimInstance.CimClass, methodName, method.GetParameters(), arguments);
using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.cimInstance, methodName, cimParameters); using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.cimInstance, methodName, cimParameters);
@ -238,7 +238,7 @@ namespace WMI
public void Commit() public void Commit()
{ {
#if !CIM #if !FEATURE_CIM
this.wmiObject.Put(); this.wmiObject.Put();
#endif #endif
} }
@ -246,7 +246,7 @@ namespace WMI
private class ClassHandler : BaseHandler private class ClassHandler : BaseHandler
{ {
#if CIM #if FEATURE_CIM
private readonly CimSession cimSession; private readonly CimSession cimSession;
private readonly CimClass cimClass; private readonly CimClass cimClass;
#else #else
@ -254,7 +254,7 @@ namespace WMI
#endif #endif
private readonly string className; private readonly string className;
#if CIM #if FEATURE_CIM
public ClassHandler(CimSession cimSession, string className) public ClassHandler(CimSession cimSession, string className)
{ {
this.cimSession = cimSession; this.cimSession = cimSession;
@ -285,7 +285,7 @@ namespace WMI
query += ' ' + Capitalize(methodParameters[i].Name!) + " = '" + arguments[i] + "'"; query += ' ' + Capitalize(methodParameters[i].Name!) + " = '" + arguments[i] + "'";
} }
#if CIM #if FEATURE_CIM
// TODO: support collections // TODO: support collections
foreach (CimInstance cimInstance in this.cimSession.QueryInstances(CimNamespace, "WQL", query)) foreach (CimInstance cimInstance in this.cimSession.QueryInstances(CimNamespace, "WQL", query))
{ {
@ -305,7 +305,7 @@ namespace WMI
} }
string methodName = method.Name; string methodName = method.Name;
#if CIM #if FEATURE_CIM
using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null : using CimMethodParametersCollection? cimParameters = arguments.Length == 0 ? null :
this.GetMethodParameters(this.cimClass, methodName, methodParameters, arguments); this.GetMethodParameters(this.cimClass, methodName, methodParameters, arguments);
using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.className, methodName, cimParameters); using CimMethodResult result = this.cimSession.InvokeMethod(CimNamespace, this.className, methodName, cimParameters);
@ -328,7 +328,7 @@ namespace WMI
WmiClassName className = (WmiClassName)typeof(T).GetCustomAttributes(typeof(WmiClassName), false)[0]; WmiClassName className = (WmiClassName)typeof(T).GetCustomAttributes(typeof(WmiClassName), false)[0];
return (T)ProxyFactory.GetInstance().Create( return (T)ProxyFactory.GetInstance().Create(
#if CIM #if FEATURE_CIM
new ClassHandler(this.cimSession, className.Name), new ClassHandler(this.cimSession, className.Name),
#else #else
new ClassHandler(this.wmiScope, className.Name), new ClassHandler(this.wmiScope, className.Name),