1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Runtime.InteropServices;
6 
7 namespace System.Management
8 {
9     //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
10     /// <summary>
11     ///    <para> Contains information about a WMI method.</para>
12     /// </summary>
13     /// <example>
14     ///    <code lang='C#'>using System;
15     /// using System.Management;
16     ///
17     /// // This example shows how to obtain meta data
18     /// // about a WMI method with a given name in a given WMI class
19     ///
20     /// class Sample_MethodData
21     /// {
22     ///     public static int Main(string[] args) {
23     ///
24     ///         // Get the "SetPowerState" method in the Win32_LogicalDisk class
25     ///         ManagementClass diskClass = new ManagementClass("win32_logicaldisk");
26     ///         MethodData m = diskClass.Methods["SetPowerState"];
27     ///
28     ///         // Get method name (albeit we already know it)
29     ///         Console.WriteLine("Name: " + m.Name);
30     ///
31     ///         // Get the name of the top-most class where this specific method was defined
32     ///         Console.WriteLine("Origin: " + m.Origin);
33     ///
34     ///         // List names and types of input parameters
35     ///         ManagementBaseObject inParams = m.InParameters;
36     ///         foreach(PropertyData pdata in inParams.Properties) {
37     ///             Console.WriteLine();
38     ///             Console.WriteLine("InParam_Name: " + pdata.Name);
39     ///             Console.WriteLine("InParam_Type: " + pdata.Type);
40     ///         }
41     ///
42     ///         // List names and types of output parameters
43     ///         ManagementBaseObject outParams = m.OutParameters;
44     ///         foreach(PropertyData pdata in outParams.Properties) {
45     ///             Console.WriteLine();
46     ///             Console.WriteLine("OutParam_Name: " + pdata.Name);
47     ///             Console.WriteLine("OutParam_Type: " + pdata.Type);
48     ///         }
49     ///
50     ///         return 0;
51     ///     }
52     /// }
53     ///    </code>
54     ///    <code lang='VB'>Imports System
55     /// Imports System.Management
56     ///
57     /// ' This example shows how to obtain meta data
58     /// ' about a WMI method with a given name in a given WMI class
59     ///
60     /// Class Sample_ManagementClass
61     ///     Overloads Public Shared Function Main(args() As String) As Integer
62     ///
63     ///         ' Get the "SetPowerState" method in the Win32_LogicalDisk class
64     ///         Dim diskClass As New ManagementClass("Win32_LogicalDisk")
65     ///         Dim m As MethodData = diskClass.Methods("SetPowerState")
66     ///
67     ///         ' Get method name (albeit we already know it)
68     ///         Console.WriteLine("Name: " &amp; m.Name)
69     ///
70     ///         ' Get the name of the top-most class where
71     ///         ' this specific method was defined
72     ///         Console.WriteLine("Origin: " &amp; m.Origin)
73     ///
74     ///         ' List names and types of input parameters
75     ///         Dim inParams As ManagementBaseObject
76     ///         inParams = m.InParameters
77     ///         Dim pdata As PropertyData
78     ///         For Each pdata In inParams.Properties
79     ///             Console.WriteLine()
80     ///             Console.WriteLine("InParam_Name: " &amp; pdata.Name)
81     ///             Console.WriteLine("InParam_Type: " &amp; pdata.Type)
82     ///         Next pdata
83     ///
84     ///         ' List names and types of output parameters
85     ///         Dim outParams As ManagementBaseObject
86     ///         outParams = m.OutParameters
87     ///         For Each pdata in outParams.Properties
88     ///             Console.WriteLine()
89     ///             Console.WriteLine("OutParam_Name: " &amp; pdata.Name)
90     ///             Console.WriteLine("OutParam_Type: " &amp; pdata.Type)
91     ///         Next pdata
92     ///
93     ///         Return 0
94     ///     End Function
95     /// End Class
96     ///    </code>
97     /// </example>
98     //CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
99     public class MethodData
100     {
101         private ManagementObject parent; //needed to be able to get method qualifiers
102         private string methodName;
103         private IWbemClassObjectFreeThreaded wmiInParams;
104         private IWbemClassObjectFreeThreaded wmiOutParams;
105         private QualifierDataCollection qualifiers;
106 
MethodData(ManagementObject parent, string methodName)107         internal MethodData(ManagementObject parent, string methodName)
108         {
109             this.parent = parent;
110             this.methodName = methodName;
111             RefreshMethodInfo();
112             qualifiers = null;
113         }
114 
115 
116         //This private function is used to refresh the information from the Wmi object before returning the requested data
RefreshMethodInfo()117         private void RefreshMethodInfo()
118         {
119             int status = (int)ManagementStatus.Failed;
120 
121             try
122             {
123                 status = parent.wbemObject.GetMethod_(methodName, 0, out wmiInParams, out wmiOutParams);
124             }
125             catch (COMException e)
126             {
127                 ManagementException.ThrowWithExtendedInfo(e);
128             }
129             if ((status & 0xfffff000) == 0x80041000)
130             {
131                 ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
132             }
133             else if ((status & 0x80000000) != 0)
134             {
135                 Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
136             }
137         }
138 
139 
140         /// <summary>
141         ///    <para>Gets or sets the name of the method.</para>
142         /// </summary>
143         /// <value>
144         ///    <para>The name of the method.</para>
145         /// </value>
146         public string Name
147         {
148             get { return methodName != null ? methodName : ""; }
149         }
150 
151         /// <summary>
152         ///    <para> Gets or sets the input parameters to the method. Each
153         ///       parameter is described as a property in the object. If a parameter is both in
154         ///       and out, it appears in both the <see cref='System.Management.MethodData.InParameters'/> and <see cref='System.Management.MethodData.OutParameters'/>
155         ///       properties.</para>
156         /// </summary>
157         /// <value>
158         ///    <para>
159         ///       A <see cref='System.Management.ManagementBaseObject'/>
160         ///       containing all the input parameters to the
161         ///       method.</para>
162         /// </value>
163         /// <remarks>
164         ///    <para>Each parameter in the object should have an
165         ///    <see langword='ID'/>
166         ///    qualifier, identifying the order of the parameters in the method call.</para>
167         /// </remarks>
168         public ManagementBaseObject InParameters
169         {
170             get
171             {
172                 RefreshMethodInfo();
173                 return (null == wmiInParams) ? null : new ManagementBaseObject(wmiInParams); }
174         }
175 
176         /// <summary>
177         ///    <para> Gets or sets the output parameters to the method. Each
178         ///       parameter is described as a property in the object. If a parameter is both in
179         ///       and out, it will appear in both the <see cref='System.Management.MethodData.InParameters'/> and <see cref='System.Management.MethodData.OutParameters'/>
180         ///       properties.</para>
181         /// </summary>
182         /// <value>
183         /// <para>A <see cref='System.Management.ManagementBaseObject'/> containing all the output parameters to the method. </para>
184         /// </value>
185         /// <remarks>
186         ///    <para>Each parameter in this object should have an
187         ///    <see langword='ID'/> qualifier to identify the
188         ///       order of the parameters in the method call.</para>
189         ///    <para>The ReturnValue property is a special property of
190         ///       the <see cref='System.Management.MethodData.OutParameters'/>
191         ///       object and
192         ///       holds the return value of the method.</para>
193         /// </remarks>
194         public ManagementBaseObject OutParameters
195         {
196             get
197             {
198                 RefreshMethodInfo();
199                 return (null == wmiOutParams) ? null : new ManagementBaseObject(wmiOutParams); }
200         }
201 
202         /// <summary>
203         ///    <para>Gets the name of the management class in which the method was first
204         ///       introduced in the class inheritance hierarchy.</para>
205         /// </summary>
206         /// <value>
207         ///    A string representing the originating
208         ///    management class name.
209         /// </value>
210         public string Origin
211         {
212             get
213             {
214                 string className = null;
215                 int status = parent.wbemObject.GetMethodOrigin_(methodName, out className);
216 
217                 if (status < 0)
218                 {
219                     if (status == (int)tag_WBEMSTATUS.WBEM_E_INVALID_OBJECT)
220                         className = String.Empty;   // Interpret as an unspecified property - return ""
221                     else if ((status & 0xfffff000) == 0x80041000)
222                         ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
223                     else
224                         Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
225                 }
226 
227                 return className;
228             }
229         }
230 
231         /// <summary>
232         ///    <para>Gets a collection of qualifiers defined in the
233         ///       method. Each element is of type <see cref='System.Management.QualifierData'/>
234         ///       and contains information such as the qualifier name, value, and
235         ///       flavor.</para>
236         /// </summary>
237         /// <value>
238         ///    A <see cref='System.Management.QualifierDataCollection'/> containing the
239         ///    qualifiers for this method.
240         /// </value>
241         /// <seealso cref='System.Management.QualifierData'/>
242         public QualifierDataCollection Qualifiers
243         {
244             get
245             {
246                 if (qualifiers == null)
247                     qualifiers = new QualifierDataCollection(parent, methodName, QualifierType.MethodQualifier);
248                 return qualifiers;
249             }
250         }
251 
252     }//MethodData
253 }
254