Exemple #1
0
        /// <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(ManagementObject assocInstance,
                                                ManagementPath sourcePath)
        {
            try
            {
                PropertyCollection.PropertyEnumerator enumAssocProps = assocInstance.Properties.GetEnumerator();
                while (enumAssocProps.MoveNext())
                {
                    Property curProp = enumAssocProps.Current;
                    if (curProp.Type != CimType.Reference)
                    {
                        continue;
                    }
                    else
                    {
                        //confirm that this is not the source
                        if ((String.Compare(curProp.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>
        ///     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()
        {
            PropertyCollection props = mgmtObj.Properties;

            PropertyCollection.PropertyEnumerator eProps = props.GetEnumerator();

            PropertyDescriptor[] propDescrArray = new PropertyDescriptor[props.Count + NUM_OF_SYSTEM_PROPS];
            if (propDescrArray.Length != 0)
            {
                int counter = 0;
                while (eProps.MoveNext())
                {
                    Property curProp = eProps.Current;

                    if (GENUS == 2)
                    {
                        //get the property on the class object: to get to "Values" qualifier
                        curProp = mgmtClassObj.Properties[curProp.Name];
                    }

                    /*
                     * if (WmiHelper.IsValueMap(curProp))
                     * {
                     *      //MessageBox.Show("Value map property " + ((ISWbemProperty)eProps.Current).Name);
                     *      propDescrArray[counter++] = new WMIEnumPropertyDescriptor(mgmtObj,
                     *                                                                                                                      mgmtClassObj,
                     *                                                                                                                      mgmtObj,
                     *                                                                                                                      mgmtClassObj,
                     *                                                                                                                      curProp.Name,
                     *                                                                                                                      !IsNewInstance);
                     *
                     * }
                     * else
                     */
                    {
                        propDescrArray[counter++] = new WMIPropertyDescriptor(mgmtObj,
                                                                              mgmtClassObj,
                                                                              curProp.Name,
                                                                              !IsNewInstance);
                    }
                }

                //add system properties
                IDictionaryEnumerator enumSys = (IDictionaryEnumerator)((IEnumerable)SystemPropertyDictionary).GetEnumerator();

                while (enumSys.MoveNext())
                {
                    propDescrArray[counter++] = new WMISystemPropertyDescriptor(mgmtObj,
                                                                                enumSys.Key.ToString(),
                                                                                enumSys.Value);
                }
            }

            return(new PropertyDescriptorCollection(propDescrArray));
        }
Exemple #3
0
        /// <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 GetAssocTargetNamespacePath(ManagementObject assocInstance,
                                                         ManagementPath sourcePath)
        {
            try
            {
                PropertyCollection.PropertyEnumerator enumAssocProps = assocInstance.Properties.GetEnumerator();
                while (enumAssocProps.MoveNext())
                {
                    Property curProp = enumAssocProps.Current;
                    if (curProp.Type != CimType.Reference)
                    {
                        continue;
                    }
                    else
                    {
                        //get CimType property qualifier value
                        String refValue = curProp.Qualifiers["CimType"].Value.ToString();

                        //get rid of "ref:" prefix:
                        refValue = refValue.Substring(4);

                        //confirm that this is not the source
                        if ((String.Compare(refValue, sourcePath.ClassName, true) != 0) &&
                            (String.Compare(refValue, sourcePath.Path, true) != 0))
                        {
                            return(refValue);
                        }
                    }
                }

                return(String.Empty);
            }
            catch (Exception e)
            {
                MessageBox.Show(WMISys.GetString("WMISE_Exception", e.Message, e.StackTrace));
                return(String.Empty);
            }
        }
        void SetLabel()
        {
            ManagementPath mgmtPath = mgmtObj.Path;

            if (mgmtPath.IsSingleton)
            {
                label = mgmtPath.ClassName;
            }
            else
            {
                //this is not a singleton.  Construct label to consist of comma-separated
                //key property values and caption value.
                bool   needCaption = true;
                string keyVals     = string.Empty;
                string caption     = string.Empty;

                PropertyCollection props = mgmtObj.Properties;
                try
                {
                    caption = props["Caption"].Value.ToString();
                }
                catch (Exception)
                {
                    //no "Caption" property
                    needCaption = false;
                }

                if (caption == string.Empty)
                {
                    needCaption = false;
                }

                //get key property values
                PropertyCollection.PropertyEnumerator propEnum = mgmtObj.Properties.GetEnumerator();

                while (propEnum.MoveNext())
                {
                    if (WmiHelper.IsKeyProperty(propEnum.Current))
                    {
                        string keyval = propEnum.Current.Value.ToString();
                        if (needCaption && (string.Compare(keyval, caption, true) == 0))
                        {
                            needCaption = false;
                        }
                        keyVals += keyval + ",";
                    }
                }


                if (keyVals != string.Empty)
                {
                    //get rid of last character (a comma)
                    keyVals = keyVals.Substring(0, keyVals.Length - 1);
                }

                if (needCaption)
                {
                    label = caption + "(" + keyVals + ")";
                }
                else
                {
                    label = keyVals;
                }

                if (label == string.Empty)
                {
                    label = className;
                }
            }
        }
        /// <summary>
        /// note that Properties_ on ISWbemObject doesn't return system properties,
        /// so need to reconstruct them:  __SERVER, __PATH, __GENUS are available through
        /// SWbemObjectPath; __DYNASTY, __DERIVATION can be accessed through SWbemObjct.Derivation_
        /// </summary>
        private void Initialize()
        {
            try
            {
                ManagementPath path = mgmtObj.Path;
                if (path.IsClass)
                {
                    GENUS = 1;
                }
                else                    //instance
                {
                    GENUS = 2;
                }


                SystemPropertyDictionary.Add("__GENUS", GENUS);

                CLASS = path.ClassName;
                SystemPropertyDictionary.Add("__CLASS", CLASS);

                NAMESPACE = path.NamespacePath;
                SystemPropertyDictionary.Add("__NAMESPACE", NAMESPACE);

                PATH = path.Path;
                SystemPropertyDictionary.Add("__PATH", PATH);

                RELPATH = path.RelativePath;
                SystemPropertyDictionary.Add("__RELPATH", RELPATH);

                SERVER = path.Server;
                SystemPropertyDictionary.Add("__SERVER", SERVER);

                //get PROPERTY_COUNT
                PropertyCollection props = mgmtObj.Properties;
                PropertyCollection.PropertyEnumerator eProps = props.GetEnumerator();
                while (eProps.MoveNext())
                {
                    PROPERTY_COUNT++;
                }
                SystemPropertyDictionary.Add("__PROPERTY_COUNT", PROPERTY_COUNT);


                //get inheritance-related properties

                Object[] oaDerivation = (Object[])mgmtObj.SystemProperties["__DERIVATION"].Value;
                if (oaDerivation.Length == 0)
                {
                    DYNASTY = CLASS;
                }
                else
                {
                    SUPERCLASS = oaDerivation[0].ToString();
                    DYNASTY    = oaDerivation[oaDerivation.Length - 1].ToString();
                }

                SystemPropertyDictionary.Add("__SUPERCLASS", SUPERCLASS);
                SystemPropertyDictionary.Add("__DYNASTY", DYNASTY);

                DERIVATION = new string[oaDerivation.Length];
                Array.Copy(oaDerivation, DERIVATION, oaDerivation.Length);
                SystemPropertyDictionary.Add("__DERIVATION", DERIVATION);

                IsNewInstance = ((GENUS == 2) && (PATH == string.Empty));
            }
            catch (Exception exc)
            {
                MessageBox.Show(WMISys.GetString("WMISE_Exception", exc.Message, exc.StackTrace));
            }
        }