static public String GetPropertyDescription(String propName, ISWbemObject curObj) { try { if (curObj == null) { throw new ArgumentNullException("curObj"); } ISWbemProperty verboseProp = null; if (!curObj.Path_.IsClass) { ISWbemObject classObj = WmiHelper.GetClassObject(curObj); verboseProp = classObj.Properties_.Item(propName, 0); } else { verboseProp = curObj.Properties_.Item(propName, 0); } string descr = string.Empty; ISWbemQualifier descrQual = verboseProp.Qualifiers_.Item("Description", 0); descr = descrQual.get_Value().ToString(); return(descr); } catch (Exception) { return(""); } }
private void Initialize(ISWbemObject wbemObj) { try { ISWbemObjectPath path = (ISWbemObjectPath)wbemObj.Path_; //get the property ISWbemPropertySet props = (ISWbemPropertySet)wbemObj.Properties_; prop = props.Item(propName, 0); if (path.IsClass) { genus = 1; classProp = prop; } else //instance { genus = 2; classProp = ((ISWbemPropertySet)wmiClassObj.Properties_).Item(propName, 0); } mgmtProp = mgmtObj.Properties[propName]; mgmtClassProp = mgmtClassObj.Properties[propName]; } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
/// <summary> /// Given an instance of an association object and a path to one of its /// endpoints (the "source"), this method returns the role of the other endpoint /// (the "target") in this association. /// </summary> /// <param name="assocInstance"> </param> /// <param name="sourcePath"> </param> static public String GetAssocTargetRole(ISWbemObject assocInstance, ISWbemObjectPath sourcePath) { try { IEnumerator enumAssocProps = ((IEnumerable)(assocInstance.Properties_)).GetEnumerator(); while (enumAssocProps.MoveNext()) { ISWbemProperty curProp = (ISWbemProperty)enumAssocProps.Current; if (curProp.CIMType != WbemCimtypeEnum.wbemCimtypeReference) { continue; } else { //confirm that this is not the source if ((String.Compare(curProp.get_Value().ToString(), sourcePath.Path, true)) != 0) { return(curProp.Name); } } } return(String.Empty); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(String.Empty); } }
/// <summary> /// CheckPropertyQualifierExistence /// </summary> /// <param name="prop"> </param> /// <param name="qualName"> </param> static private bool CheckPropertyQualifierExistence(ISWbemProperty prop, String qualName) { ISWbemQualifierSet qualSet = prop.Qualifiers_; try { ISWbemQualifier qual = (ISWbemQualifier)qualSet.Item(qualName, 0); return(true); //previous statement didn't throw, so qualifier must be present } catch (Exception) { return(false); } }
/// <summary> /// Given an instance of an association object and a path to one of its /// endpoints (the "source"), this method returns "path" to the other endpoint /// (the "target"). /// </summary> /// <param name="assocInstance"> </param> /// <param name="sourcePath"> </param> static public String GetAssocTargetClass(ISWbemObject assocInstance, ISWbemObjectPath sourcePath) { try { IEnumerator enumAssocProps = ((IEnumerable)(assocInstance.Properties_)).GetEnumerator(); while (enumAssocProps.MoveNext()) { ISWbemProperty curProp = (ISWbemProperty)enumAssocProps.Current; if (curProp.CIMType != WbemCimtypeEnum.wbemCimtypeReference) { continue; } else { //get CIMTYPE property qualifier value String refValue = curProp.Qualifiers_.Item("CIMTYPE", 0).get_Value().ToString(); //get rid of "ref:" prefix: refValue = refValue.Substring(4); //confirm that this is not the source if ((String.Compare(refValue, sourcePath.Class, true) != 0) && (String.Compare(refValue, sourcePath.Path, true) != 0)) { //if this is a path, leave only the class name, //which is the part after the last backslash char[] separ = new char[] { '\\' }; string[] pathParts = refValue.Split(separ); refValue = pathParts[pathParts.Length - 1]; return(refValue); } } } return(String.Empty); } catch (Exception e) { MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace)); return(String.Empty); } }
internal void SetPropertyValue(int rowNum, String val) { try { DataRow row = this.Rows.All[rowNum]; ISWbemProperty propAffected = wmiObj.Properties_.Item(row[propNameColumn].ToString(), 0); Object newValue = WmiHelper.GetTypedObjectFromString(propAffected.CIMType, val); propAffected.set_Value(ref newValue); } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw (exc); } }
/// <summary> /// Property is not writeable only if "read" qualifier is present and its value is "true" /// Also, for dynamic classes, absence of "write" qualifier means that the property is read-only. /// </summary> /// <param name="obj"> </param> /// <param name="prop"> </param> static public bool IsPropertyWriteable(ISWbemObject obj, ISWbemProperty prop) { //collect all the info: bool isDynamic = CheckObjectBoolQualifier(obj, "dynamic"); bool hasWrite = CheckPropertyQualifierExistence(prop, "write"); bool writeValue = CheckPropertyBoolQualifier(prop, "write"); bool hasRead = CheckPropertyQualifierExistence(prop, "read"); bool readValue = CheckPropertyBoolQualifier(prop, "read"); if ((!isDynamic && !hasWrite && !hasRead) || (!isDynamic && hasWrite && writeValue) || (isDynamic && hasWrite && writeValue)) { return(true); } return(false); }
/// <summary> /// CheckPropertyBoolQualifier /// </summary> /// <param name="prop"> </param> /// <param name="qualName"> </param> static private bool CheckPropertyBoolQualifier(ISWbemProperty prop, String qualName) { try { ISWbemQualifierSet qualSet = prop.Qualifiers_; ISWbemQualifier qual = (ISWbemQualifier)qualSet.Item(qualName, 0); return(Convert.ToBoolean(qual.get_Value())); } catch (Exception) { //NOTE that if the qualifier is not present, "Not found" will be returned //Return false in this case return(false); } }
static public bool IsValueMap(ISWbemProperty prop) { try { IEnumerator enumQuals = ((IEnumerable)(prop.Qualifiers_)).GetEnumerator(); while (enumQuals.MoveNext()) { ISWbemQualifier qual = (ISWbemQualifier)enumQuals.Current; if (qual.Name == "Values") { return(true); } } return(false); } catch (Exception) { return(false); } }
static public bool IsPropertyWriteableWithTest(ISWbemObject obj, ISWbemProperty prop) { Object curValue = prop.get_Value(); prop.set_Value(ref curValue); int flags = (int)((int)WbemChangeFlagEnum.wbemChangeFlagUpdateSafeMode | (int)WbemChangeFlagEnum.wbemChangeFlagUpdateOnly | (int)WbemFlagEnum.wbemFlagUseAmendedQualifiers); try { obj.Put_(flags, null); return(true); } catch (Exception) { return(false); } }
/// <summary> /// Retrieves an array of properties that the given component instance /// provides. This may differ from the set of properties the class /// provides. If the component is sited, the site may add or remove /// additional properties. /// </summary> /// <returns> /// an array of properties this component surfaces. /// </returns> PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { //NOTE: this re-initializing of mgmtObj and mgmtClassObj is a workaround: //apparently, the cached pointers that were created on a different thread //don't work: Interop marshalling problems? mgmtObj = null; mgmtObj = new ManagementObject(wmiObj.Path_.Path); mgmtClassObj = null; mgmtClassObj = new ManagementObject(wmiClassObj.Path_.Path); ISWbemPropertySet props = (ISWbemPropertySet)wmiObj.Properties_; IEnumerator eProps = ((IEnumerable)props).GetEnumerator(); PropertyDescriptor[] propDescrArray = new PropertyDescriptor[((ISWbemPropertySet)props).Count + NUM_OF_SYSTEM_PROPS]; if (propDescrArray.Length != 0) { int counter = 0; while (eProps.MoveNext()) { ISWbemProperty curProp = (ISWbemProperty)eProps.Current; if (GENUS == 2) { //get the property on the class object: to get to "Values" qualifier curProp = wmiClassObj.Properties_.Item(curProp.Name, 0); } /* * if (WmiHelper.IsValueMap(curProp)) * { * //MessageBox.Show("Value map property " + ((ISWbemProperty)eProps.Current).Name); * propDescrArray[counter++] = new WMIEnumPropertyDescriptor(mgmtObj, * mgmtClassObj, * wmiObj, * wmiClassObj, * curProp.Name, * !IsNewInstance); * * } * else */ { propDescrArray[counter++] = new WMIPropertyDescriptor(mgmtObj, mgmtClassObj, wmiObj, wmiClassObj, curProp.Name, !IsNewInstance); } } //add system properties IDictionaryEnumerator enumSys = (IDictionaryEnumerator)((IEnumerable)SystemPropertyDictionary).GetEnumerator(); while (enumSys.MoveNext()) { propDescrArray[counter++] = new WMISystemPropertyDescriptor(wmiObj, enumSys.Key.ToString(), enumSys.Value); } } return(new PropertyDescriptorCollection(propDescrArray)); }
private void Initialize() { try { this.TableName = wmiObj.Path_.Class; propNameColumn = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnName")); propNameColumn.AllowNull = false; propNameColumn.Unique = true; propNameColumn.DataType = typeof(string); propTypeColumn = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnType")); propTypeColumn.AllowNull = false; propTypeColumn.DataType = typeof(string); propValueColumn = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnValue")); propValueColumn.DefaultValue = null; propDescrColumn = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnDescription")); propDescrColumn.ReadOnly = true; propDescrColumn.DefaultValue = string.Empty; propDescrColumn.DataType = typeof(string); if (showOperators) { operatorColumn = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnComparison"), typeof(ComparisonOperators)); } //operatorColumn.AllowNull = false; /* * ValueEditor dropDownEditor = new ValueEditor(); * dropDownEditor.Style = ValueEditorStyles.DropdownArrow; * operatorColumn.DataValueEditorType = dropDownEditor.GetType(); */ if (showKeys) { propIsKey = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnIsKey"), typeof(Boolean)); propIsKey.AllowNull = false; propIsKey.DefaultValue = false; } if (showOrigin) { propOrigin = new DataColumn(WMISys.GetString("WMISE_PropTable_ColumnIsLocal"), typeof(Boolean)); propOrigin.AllowNull = false; propOrigin.DefaultValue = true; } //set read/write permissions on columns //Note that DefaultView takes care of the rest of the restrictions //(see below) if (gridMode == GridMode.EditMode) { propNameColumn.ReadOnly = true; propTypeColumn.ReadOnly = true; propValueColumn.ReadOnly = false; if (showOrigin) { propOrigin.ReadOnly = true; } if (showKeys) { propIsKey.ReadOnly = true; } } if (gridMode == GridMode.DesignMode && showOrigin) { propOrigin.ReadOnly = true; } Columns.Add(propNameColumn); Columns.Add(propTypeColumn); if (showOperators) { //operatorColumn.DataValueEditorType = typeof(OperatroDataColumnEditor); operatorColumn.ReadOnly = false; Columns.Add(operatorColumn); } Columns.Add(propValueColumn); //Columns.Add(propDescrColumn); if (showOrigin) { Columns.Add(propOrigin); } if (showKeys) { Columns.Add(propIsKey); } ISWbemPropertySet props = wmiObj.Properties_; IEnumerator propEnum = ((IEnumerable)props).GetEnumerator(); while (propEnum.MoveNext()) { ISWbemProperty prop = (ISWbemProperty)propEnum.Current; if (propFilters == PropertyFilters.NoInherited && !prop.IsLocal) { continue; //skip this property } DataRow propRow = NewRow(); propRow[propNameColumn] = prop.Name; propRow[propTypeColumn] = CIMTypeMapper.ToString(prop.CIMType); propRow[propValueColumn] = prop.get_Value(); //propRow[propDescrColumn] = WmiHelper.GetPropertyDescription(prop, wmiObj); if (showOperators) { propRow[operatorColumn] = "="; } //set property origin column if (showOrigin) { if (prop.IsLocal) { propRow[propOrigin] = true; } else { propRow[propOrigin] = false; } } if (showKeys) { propRow[propIsKey] = WmiHelper.IsKeyProperty(prop); } Rows.Add(propRow); } //TODO: add system properties here (if PropertyFilters.ShowAll) this.CaseSensitive = false; this.RowChanged += new DataRowChangeEventHandler(this.RowChangedEventHandler); this.RowChanging += new DataRowChangeEventHandler(this.RowChangingEventHandler); DataView view = this.DefaultView; switch (gridMode) { case (GridMode.ViewMode): { view.AllowEdit = false; //wmi raid 2866? view.AllowDelete = false; view.AllowNew = false; break; } case (GridMode.EditMode): { view.AllowEdit = true; view.AllowDelete = false; view.AllowNew = false; break; } case (GridMode.DesignMode): { view.AllowEdit = true; view.AllowDelete = true; view.AllowNew = true; break; } } } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
protected void RowChangingEventHandler(object sender, DataRowChangeEventArgs e) { try { DataRow row = e.Row; switch (e.Action) { case (DataRowAction.Add): { //throw (new Exception("Cannot add rows")); break; } case (DataRowAction.Change): { //if this is an object, datetime, enum or ref property changing, //bring up custom UI for editing these!!!! if (gridMode == GridMode.EditMode) { //this can only be value change ISWbemProperty propAffected = wmiObj.Properties_.Item(row[propNameColumn].ToString(), 0); if (propAffected.CIMType == WbemCimtypeEnum.wbemCimtypeObject) { MessageBox.Show("should bring up custom type editor for objects"); } if (propAffected.CIMType == WbemCimtypeEnum.wbemCimtypeReference) { MessageBox.Show("should bring up custom type editor for refs"); } if (propAffected.CIMType == WbemCimtypeEnum.wbemCimtypeDatetime) { MessageBox.Show("should bring up custom type editor for datetime"); } if (WmiHelper.IsValueMap(propAffected)) { MessageBox.Show("should bring up custom type editor for enums"); } } break; } case (DataRowAction.Commit): { MessageBox.Show("DataRowAction.Commit"); break; } case (DataRowAction.Delete): { throw (new Exception("Cannot delete rows")); } case (DataRowAction.Nothing): { MessageBox.Show("DataRowAction.Nothing"); break; } case (DataRowAction.Rollback): { MessageBox.Show("DataRowAction.Rollback"); break; } } } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); throw(exc); } }
protected void RowChangedEventHandler(object sender, DataRowChangeEventArgs e) { try { //MessageBox.Show("RowChangedEventHandler"); DataRow RowAffected = e.Row; switch (e.Action) { case (DataRowAction.Add): { MessageBox.Show("Cannot Add Rows. Deleting..."); RowAffected.Delete(); break; } case (DataRowAction.Change): { DataRow row = e.Row; if (gridMode == GridMode.EditMode) { //this can only be value change ISWbemProperty propAffected = wmiObj.Properties_.Item(row[propNameColumn].ToString(), 0); Object newValue = WmiHelper.GetTypedObjectFromString(propAffected.CIMType, row[propValueColumn].ToString()); propAffected.set_Value(ref newValue); } //TODO: handle other modes here, too!!! break; } case (DataRowAction.Commit): { MessageBox.Show("DataRowAction.Commit"); break; } case (DataRowAction.Delete): { MessageBox.Show("Cannot delete nodes.Adding back..."); RowAffected.CancelEdit(); break; } case (DataRowAction.Nothing): { MessageBox.Show("DataRowAction.Nothing"); break; } case (DataRowAction.Rollback): { MessageBox.Show("DataRowAction.Rollback"); break; } } } catch (Exception exc) { MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace)); } }
/// <summary> /// IsKeyProperty /// </summary> /// <param name="prop"> </param> static public bool IsKeyProperty(ISWbemProperty prop) { return(CheckPropertyBoolQualifier(prop, "Key")); }