Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Wmi / managed / System / Management / Qualifier.cs / 1305376 / Qualifier.cs
using System;
using System.Runtime.InteropServices;
using WbemClient_v1;
using System.Globalization;
namespace System.Management
{
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
///
/// Contains information about a WMI qualifier.
///
///
/// using System;
/// using System.Management;
///
/// // This sample demonstrates how to enumerate qualifiers
/// // of a ManagementClass object.
/// class Sample_QualifierData
/// {
/// public static int Main(string[] args) {
/// ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
/// diskClass.Options.UseAmendedQualifiers = true;
/// QualifierData diskQualifier = diskClass.Qualifiers["Description"];
/// Console.WriteLine(diskQualifier.Name + " = " + diskQualifier.Value);
/// return 0;
/// }
/// }
///
/// Imports System
/// Imports System.Management
///
/// ' This sample demonstrates how to enumerate qualifiers
/// ' of a ManagementClass object.
/// Class Sample_QualifierData
/// Overloads Public Shared Function Main(args() As String) As Integer
/// Dim diskClass As New ManagementClass("win32_logicaldisk")
/// diskClass.Options.UseAmendedQualifiers = True
/// Dim diskQualifier As QualifierData = diskClass.Qualifiers("Description")
/// Console.WriteLine(diskQualifier.Name + " = " + diskQualifier.Value)
/// Return 0
/// End Function
/// End Class
///
///
//CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC//
public class QualifierData
{
private ManagementBaseObject parent; //need access to IWbemClassObject pointer to be able to refresh qualifiers
private string propertyOrMethodName; //remains null for object qualifiers
private string qualifierName;
private QualifierType qualifierType;
private Object qualifierValue;
private int qualifierFlavor;
private IWbemQualifierSetFreeThreaded qualifierSet;
internal QualifierData(ManagementBaseObject parent, string propName, string qualName, QualifierType type)
{
this.parent = parent;
this.propertyOrMethodName = propName;
this.qualifierName = qualName;
this.qualifierType = type;
RefreshQualifierInfo();
}
//This private function is used to refresh the information from the Wmi object before returning the requested data
private void RefreshQualifierInfo()
{
int status = (int)ManagementStatus.Failed;
qualifierSet = null;
switch (qualifierType) {
case QualifierType.ObjectQualifier :
status = parent.wbemObject.GetQualifierSet_(out qualifierSet);
break;
case QualifierType.PropertyQualifier :
status = parent.wbemObject.GetPropertyQualifierSet_(propertyOrMethodName, out qualifierSet);
break;
case QualifierType.MethodQualifier :
status = parent.wbemObject.GetMethodQualifierSet_(propertyOrMethodName, out qualifierSet);
break;
default :
throw new ManagementException(ManagementStatus.Unexpected, null, null); //is this the best fit error ??
}
if ((status & 0x80000000) == 0) //success
{
qualifierValue = null; //Make sure it's null so that we don't leak !
if (qualifierSet != null)
status = qualifierSet.Get_(qualifierName, 0, ref qualifierValue, ref qualifierFlavor);
}
if ((status & 0xfffff000) == 0x80041000) //WMI error
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0) //any failure
Marshal.ThrowExceptionForHR(status);
}
private static object MapQualValueToWmiValue(object qualVal)
{
object wmiValue = System.DBNull.Value;
if (null != qualVal)
{
if (qualVal is Array)
{
if ((qualVal is Int32[]) || (qualVal is Double[]) || (qualVal is String[]) || (qualVal is Boolean[]))
wmiValue = qualVal;
else
{
Array valArray = (Array)qualVal;
int length = valArray.Length;
//If this is an object array, we cannot use GetElementType() to determine the type
//of the actual content. So we'll use the first element to type the array
//Type elementType = valArray.GetType().GetElementType();
Type elementType = (length > 0 ? valArray.GetValue(0).GetType() : null);
if (elementType == typeof(Int32))
{
wmiValue = new Int32 [length];
for (int i = 0; i < length; i++)
((Int32[])(wmiValue))[i] = Convert.ToInt32(valArray.GetValue(i),(IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Int32)));
}
else if (elementType == typeof(Double))
{
wmiValue = new Double [length];
for (int i = 0; i < length; i++)
((Double[])(wmiValue))[i] = Convert.ToDouble(valArray.GetValue(i),(IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Double)));
}
else if (elementType == typeof(String))
{
wmiValue = new String [length];
for (int i = 0; i < length; i++)
((String[])(wmiValue))[i] = (valArray.GetValue(i)).ToString();
}
else if (elementType == typeof(Boolean))
{
wmiValue = new Boolean [length];
for (int i = 0; i < length; i++)
((Boolean[])(wmiValue))[i] = Convert.ToBoolean(valArray.GetValue(i),(IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(System.Boolean)));
}
else
wmiValue = valArray; //should this throw ?
}
}
else
wmiValue = qualVal;
}
return wmiValue;
}
///
/// Represents the name of the qualifier.
///
///
/// The name of the qualifier.
///
public string Name
{
get { return qualifierName != null ? qualifierName : ""; }
}
///
/// Gets or sets the value of the qualifier.
///
///
/// The value of the qualifier.
///
///
/// Qualifiers can only be of the following subset of CIM
/// types: , ,
/// , , ,
/// , , ,
/// .
///
///
public Object Value
{
get {
RefreshQualifierInfo();
return ValueTypeSafety.GetSafeObject(qualifierValue);
}
set {
int status = (int)ManagementStatus.NoError;
RefreshQualifierInfo();
object newValue = MapQualValueToWmiValue(value);
status = qualifierSet.Put_(qualifierName, ref newValue,
qualifierFlavor & ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN);
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0)
Marshal.ThrowExceptionForHR(status);
}
}
///
/// Gets or sets a value indicating whether the qualifier is amended.
///
///
/// if this qualifier is amended;
/// otherwise, .
///
///
/// Amended qualifiers are
/// qualifiers whose value can be localized through WMI. Localized qualifiers
/// reside in separate namespaces in WMI and can be merged into the basic class
/// definition when retrieved.
///
public bool IsAmended
{
get {
RefreshQualifierInfo();
return ((int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_AMENDED ==
(qualifierFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_AMENDED));
}
set
{
int status = (int)ManagementStatus.NoError;
RefreshQualifierInfo ();
// Mask out origin bits
int flavor = qualifierFlavor & ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN;
if (value)
flavor |= (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_AMENDED;
else
flavor &= ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_AMENDED;
status = qualifierSet.Put_(qualifierName, ref qualifierValue, flavor);
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0)
Marshal.ThrowExceptionForHR(status);
}
}
///
/// Gets or sets a value indicating whether the qualifier has been defined locally on
/// this class or has been propagated from a base class.
///
///
/// if the qualifier has been defined
/// locally on this class; otherwise, .
///
public bool IsLocal
{
get {
RefreshQualifierInfo();
return ((int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_ORIGIN_LOCAL ==
(qualifierFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN));
}
}
///
/// Gets or sets a value indicating whether the qualifier should be propagated to instances of the
/// class.
///
///
/// if this qualifier should be
/// propagated to instances of the class; otherwise, .
///
public bool PropagatesToInstance
{
get {
RefreshQualifierInfo();
return ((int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE ==
(qualifierFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE));
}
set {
int status = (int)ManagementStatus.NoError;
RefreshQualifierInfo ();
// Mask out origin bits
int flavor = qualifierFlavor & ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN;
if (value)
flavor |= (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE;
else
flavor &= ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE;
status = qualifierSet.Put_(qualifierName, ref qualifierValue, flavor);
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0)
Marshal.ThrowExceptionForHR(status);
}
}
///
/// Gets or sets a value indicating whether the qualifier should be propagated to
/// subclasses of the class.
///
///
/// if the qualifier should be
/// propagated to subclasses of this class; otherwise, .
///
public bool PropagatesToSubclass
{
get {
RefreshQualifierInfo();
return ((int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS ==
(qualifierFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS));
}
set {
int status = (int)ManagementStatus.NoError;
RefreshQualifierInfo ();
// Mask out origin bits
int flavor = qualifierFlavor & ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN;
if (value)
flavor |= (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
else
flavor &= ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS;
status = qualifierSet.Put_(qualifierName, ref qualifierValue, flavor);
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0)
Marshal.ThrowExceptionForHR(status);
}
}
///
/// Gets or sets a value indicating whether the value of the qualifier can be
/// overridden when propagated.
///
///
/// if the value of the qualifier
/// can be overridden when propagated; otherwise, .
///
public bool IsOverridable
{
get {
RefreshQualifierInfo();
return ((int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_OVERRIDABLE ==
(qualifierFlavor & (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_PERMISSIONS));
}
set {
int status = (int)ManagementStatus.NoError;
RefreshQualifierInfo ();
// Mask out origin bits
int flavor = qualifierFlavor & ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_MASK_ORIGIN;
if (value)
flavor &= ~(int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_NOT_OVERRIDABLE;
else
flavor |= (int)tag_WBEM_FLAVOR_TYPE.WBEM_FLAVOR_NOT_OVERRIDABLE;
status = qualifierSet.Put_(qualifierName, ref qualifierValue, flavor);
if ((status & 0xfffff000) == 0x80041000)
ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
else if ((status & 0x80000000) != 0)
Marshal.ThrowExceptionForHR(status);
}
}
}//QualifierData
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SchemaElementLookUpTable.cs
- SchemaMapping.cs
- Matrix.cs
- UriTemplateMatchException.cs
- WebControlAdapter.cs
- XmlDataImplementation.cs
- Literal.cs
- WebPartEditorApplyVerb.cs
- DiscoveryEndpoint.cs
- RadioButtonPopupAdapter.cs
- PeerNameRecordCollection.cs
- FixedDocumentPaginator.cs
- RIPEMD160Managed.cs
- KeyedCollection.cs
- TextBox.cs
- SafeCoTaskMem.cs
- SQLMembershipProvider.cs
- StringKeyFrameCollection.cs
- DataControlFieldCollection.cs
- TableLayoutCellPaintEventArgs.cs
- GrowingArray.cs
- _Rfc2616CacheValidators.cs
- FileInfo.cs
- DefaultConfirmation.cs
- WindowsAuthenticationModule.cs
- FixedPage.cs
- MessageProperties.cs
- TableNameAttribute.cs
- autovalidator.cs
- ControlEvent.cs
- XmlDesigner.cs
- Debug.cs
- bidPrivateBase.cs
- TextTreeExtractElementUndoUnit.cs
- coordinatorfactory.cs
- ValidatorCompatibilityHelper.cs
- CompiledQuery.cs
- EpmCustomContentDeSerializer.cs
- Overlapped.cs
- OLEDB_Enum.cs
- XmlTextEncoder.cs
- FontUnitConverter.cs
- ScriptManagerProxy.cs
- SingleObjectCollection.cs
- SynchronizingStream.cs
- HostProtectionPermission.cs
- BulletChrome.cs
- DynamicValidatorEventArgs.cs
- XmlReturnReader.cs
- activationcontext.cs
- AdornerDecorator.cs
- OSFeature.cs
- CheckBoxFlatAdapter.cs
- SqlClientFactory.cs
- BufferedReadStream.cs
- TableParaClient.cs
- VerticalAlignConverter.cs
- XmlSerializerNamespaces.cs
- StringExpressionSet.cs
- SqlNodeAnnotation.cs
- Type.cs
- PropertyGroupDescription.cs
- HttpDictionary.cs
- ViewEvent.cs
- Converter.cs
- XmlSerializationGeneratedCode.cs
- WindowsToolbarItemAsMenuItem.cs
- SetIterators.cs
- XmlArrayItemAttribute.cs
- AccessDataSource.cs
- StorageBasedPackageProperties.cs
- TransactionScope.cs
- ColorConvertedBitmap.cs
- WindowsListViewItem.cs
- Part.cs
- AsyncStreamReader.cs
- CompModSwitches.cs
- XomlCompilerResults.cs
- OleDbDataReader.cs
- WebPartDisplayModeCancelEventArgs.cs
- DataBoundControlAdapter.cs
- GridSplitter.cs
- WindowsUpDown.cs
- TypefaceCollection.cs
- StoreItemCollection.Loader.cs
- NameSpaceEvent.cs
- ColumnTypeConverter.cs
- CustomErrorCollection.cs
- XslUrlEditor.cs
- PartialCachingControl.cs
- ContextItemManager.cs
- InvalidOperationException.cs
- DataGridViewTopLeftHeaderCell.cs
- RegionIterator.cs
- TemplatePartAttribute.cs
- HandleTable.cs
- MetadataArtifactLoaderComposite.cs
- XPathDocumentBuilder.cs
- HashAlgorithm.cs
- MethodBody.cs