public string GetText(TextFormat format)
        {
            int        objectText_;
            string     str        = null;
            TextFormat textFormat = format;

            switch (textFormat)
            {
            case TextFormat.Mof:
            {
                objectText_ = this.wbemObject.GetObjectText_(0, out str);
                if (objectText_ < 0)
                {
                    if (((long)objectText_ & (long)-4096) != (long)-2147217408)
                    {
                        Marshal.ThrowExceptionForHR(objectText_);
                    }
                    else
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)objectText_);
                    }
                }
                return(str);
            }

            case TextFormat.CimDtd20:
            case TextFormat.WmiDtd20:
            {
                IWbemObjectTextSrc wbemObjectTextSrc = (IWbemObjectTextSrc)(new WbemObjectTextSrc());
                IWbemContext       wbemContext       = (IWbemContext)(new WbemContext());
                object             obj = true;
                wbemContext.SetValue_("IncludeQualifiers", 0, ref obj);
                wbemContext.SetValue_("IncludeClassOrigin", 0, ref obj);
                if (wbemObjectTextSrc != null)
                {
                    objectText_ = wbemObjectTextSrc.GetText_(0, (IWbemClassObject_DoNotMarshal)Marshal.GetObjectForIUnknown(this.wbemObject), (uint)format, wbemContext, out str);
                    if (objectText_ < 0)
                    {
                        if (((long)objectText_ & (long)-4096) != (long)-2147217408)
                        {
                            Marshal.ThrowExceptionForHR(objectText_);
                        }
                        else
                        {
                            ManagementException.ThrowWithExtendedInfo((ManagementStatus)objectText_);
                        }
                    }
                }
                return(str);
            }
            }
            return(null);
        }
        /// <summary>
        ///    <para>Internal method to return an IWbemContext representation
        ///    of the named value collection.</para>
        /// </summary>
        internal IWbemContext GetContext()
        {
            IWbemContext wbemContext = null;

            // Only build a context if we have something to put in it
            if (0 < Count)
            {
                int status = (int)ManagementStatus.NoError;

                try {
                    wbemContext = (IWbemContext) new WbemContext();

                    foreach (string name in this)
                    {
                        object val = base.BaseGet(name);
                        status = wbemContext.SetValue_(name, 0, ref val);
                        if ((status & 0x80000000) != 0)
                        {
                            break;
                        }
                    }
                } catch {}                      //
            }

            return(wbemContext);
        }
示例#3
0
        internal IWbemContext GetContext()
        {
            IWbemContext context = null;

            if (0 < this.Count)
            {
                try
                {
                    context = (IWbemContext) new WbemContext();
                    foreach (string str in this)
                    {
                        object pValue = base.BaseGet(str);
                        if ((context.SetValue_(str, 0, ref pValue) & 0x80000000L) != 0L)
                        {
                            return(context);
                        }
                    }
                    return(context);
                }
                catch
                {
                }
            }
            return(context);
        }
        public string GetText(TextFormat format)
        {
            string pstrObjectText = null;
            int    errorCode      = 0;

            switch (format)
            {
            case TextFormat.Mof:
                errorCode = this.wbemObject.GetObjectText_(0, out pstrObjectText);
                if (errorCode < 0)
                {
                    if ((errorCode & 0xfffff000L) != 0x80041000L)
                    {
                        Marshal.ThrowExceptionForHR(errorCode);
                        return(pstrObjectText);
                    }
                    ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode);
                }
                return(pstrObjectText);

            case TextFormat.CimDtd20:
            case TextFormat.WmiDtd20:
            {
                IWbemObjectTextSrc src  = (IWbemObjectTextSrc) new WbemObjectTextSrc();
                IWbemContext       pCtx = (IWbemContext) new WbemContext();
                pCtx.SetValue_("IncludeQualifiers", 0, true);
                pCtx.SetValue_("IncludeClassOrigin", 0, ref pValue);
                if (src != null)
                {
                    errorCode = src.GetText_(0, (IWbemClassObject_DoNotMarshal)Marshal.GetObjectForIUnknown((IntPtr)this.wbemObject), (uint)format, pCtx, out pstrObjectText);
                    if (errorCode < 0)
                    {
                        if ((errorCode & 0xfffff000L) != 0x80041000L)
                        {
                            Marshal.ThrowExceptionForHR(errorCode);
                            return(pstrObjectText);
                        }
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode);
                    }
                }
                return(pstrObjectText);
            }
            }
            return(null);
        }
        internal IWbemContext GetContext()
        {
            IWbemContext wbemContext = null;

            if (0 < this.Count)
            {
                int num = 0;
                try
                {
                    wbemContext = (IWbemContext)(new WbemContext());
                    IEnumerator enumerator = this.GetEnumerator();
                    try
                    {
                        do
                        {
                            if (!enumerator.MoveNext())
                            {
                                break;
                            }
                            string current = (string)enumerator.Current;
                            object obj     = base.BaseGet(current);
                            num = wbemContext.SetValue_(current, 0, ref obj);
                        }while (((long)num & (long)-2147483648) == (long)0);
                    }
                    finally
                    {
                        IDisposable disposable = enumerator as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }
                    }
                }
                catch
                {
                }
            }
            return(wbemContext);
        }
        //******************************************************
        //GetText
        //******************************************************
        /// <summary>
        ///    <para>Returns a textual representation of the object in the specified format.</para>
        /// </summary>
        /// <param name='format'>The requested textual format. </param>
        /// <returns>
        ///    <para>The textual representation of the
        ///       object in the specified format.</para>
        /// </returns>
        public string GetText(TextFormat format)
        {
            string objText = null;
            int    status  = (int)ManagementStatus.NoError;

            //
            // Removed Initialize call since wbemObject is a property that will call Initialize ( true ) on
            // its getter.
            //
            switch (format)
            {
            case TextFormat.Mof:

                status = wbemObject.GetObjectText_(0, out objText);

                if (status < 0)
                {
                    if ((status & 0xfffff000) == 0x80041000)
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
                    }
                }

                return(objText);

            case TextFormat.CimDtd20:
            case TextFormat.WmiDtd20:

                //This may throw on non-XP platforms... - should we catch ?
                IWbemObjectTextSrc wbemTextSrc = (IWbemObjectTextSrc) new WbemObjectTextSrc();
                IWbemContext       ctx         = (IWbemContext) new WbemContext();
                object             v           = (bool)true;
                ctx.SetValue_("IncludeQualifiers", 0, ref v);
                ctx.SetValue_("IncludeClassOrigin", 0, ref v);

                if (wbemTextSrc != null)
                {
                    status = wbemTextSrc.GetText_(0,
                                                  (IWbemClassObject_DoNotMarshal)(Marshal.GetObjectForIUnknown(wbemObject)),
                                                  (uint)format, //note: this assumes the format enum has the same values as the underlying WMI enum !!
                                                  ctx,
                                                  out objText);
                    if (status < 0)
                    {
                        if ((status & 0xfffff000) == 0x80041000)
                        {
                            ManagementException.ThrowWithExtendedInfo((ManagementStatus)status);
                        }
                        else
                        {
                            Marshal.ThrowExceptionForHR(status, WmiNetUtilsHelper.GetErrorInfo_f());
                        }
                    }
                }

                return(objText);

            default:

                return(null);
            }
        }