Exemplo n.º 1
0
        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);
        }
        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);
        }
        //******************************************************
        //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);
            }
        }