public static MI_Instance[] CloneMIArray(this MI_Instance[] arrayToClone)
        {
            if (arrayToClone == null)
            {
                throw new ArgumentNullException();
            }

            MI_Instance[] result = new MI_Instance[arrayToClone.Length];
            try
            {
                for (int i = 0; i < arrayToClone.Length; i++)
                {
                    MI_Instance origInstance = arrayToClone[i];
                    result[i] = origInstance == null ? null : origInstance.Clone();
                }
            }
            catch
            {
                // If we encounter an exception halfway through we need to rollback
                for (int i = 0; i < arrayToClone.Length; i++)
                {
                    if (arrayToClone[i] == null)
                    {
                        break;
                    }

                    arrayToClone[i].Delete();
                }

                throw;
            }

            return(result);
        }
예제 #2
0
 internal void WriteErrorCallbackInternal(
     CimOperationCallbackProcessingContext callbackProcessingContext,
     MI_Operation operationHandle,
     MI_Instance instanceHandle,
     out MI_OperationCallback_ResponseType response)
 {
     response = MI_OperationCallback_ResponseType.Yes;
     if (_writeErrorCallback != null)
     {
         Debug.Assert(instanceHandle != null, "Caller should verify instance != null");
         CimInstance cimInstance = null;
         try
         {
             if (!instanceHandle.IsNull)
             {
                 cimInstance = new CimInstance(instanceHandle.Clone());
                 var             callbacksReceiverBase = (CimAsyncCallbacksReceiverBase)callbackProcessingContext.ManagedOperationContext;
                 CimResponseType userResponse          = CimResponseType.None;
                 callbacksReceiverBase.CallIntoUserCallback(
                     callbackProcessingContext,
                     delegate { userResponse = _writeErrorCallback(cimInstance); });
                 response = (MI_OperationCallback_ResponseType)userResponse;
             }
         }
         finally
         {
             if (cimInstance != null)
             {
                 cimInstance.Dispose();
             }
         }
     }
 }
예제 #3
0
        public void SimpleEnumerateInstance()
        {
            MI_Operation operation = null;

            this.Session.EnumerateInstances(MI_OperationFlags.MI_OPERATIONFLAGS_DEFAULT_RTTI,
                                            MI_OperationOptions.Null,
                                            TestEnumerateInstanceNamespace,
                                            TestEnumerateInstanceClassName,
                                            false,
                                            null,
                                            out operation);

            bool        moreResults    = true;
            MI_Instance clonedInstance = null;

            MI_Result   secondaryResult;
            string      errorMessage = null;
            MI_Instance instanceOut  = null;
            MI_Instance errorDetails = null;
            var         res          = operation.GetInstance(out instanceOut, out moreResults, out secondaryResult, out errorMessage, out errorDetails);

            MIAssert.Succeeded(res, "Expect the first GetInstance call to succeed");
            MIAssert.Succeeded(secondaryResult, "Expect the logical result of the GetInstance call to succeed");

            if (!instanceOut.IsNull)
            {
                res = instanceOut.Clone(out clonedInstance);
                MIAssert.Succeeded(res, "Expect the clone to succeed");
            }

            while (moreResults)
            {
                res = operation.GetInstance(out instanceOut, out moreResults, out secondaryResult, out errorMessage, out errorDetails);
                MIAssert.Succeeded(res, "Expect GetInstance to succeed even if we don't want the result");
                MIAssert.Succeeded(secondaryResult, "Expect the logical result of the GetInstance call to succeed even if we don't want the result");
            }

            res = operation.Close();
            MIAssert.Succeeded(res, "Expect operation to close successfully");

            string className = null;

            res = clonedInstance.GetClassName(out className);
            MIAssert.Succeeded(res, "Expect GetClassName to succeed");
            Assert.Equal(TestEnumerateInstanceClassName, className, "Expect the class name to be the one we queried");

            MI_Value elementValue = null;
            MI_Type  elementType;
            MI_Flags elementFlags;
            UInt32   elementIndex;

            res = clonedInstance.GetElement(TestEnumerateInstanceStringPropertyName, out elementValue, out elementType, out elementFlags, out elementIndex);
            MIAssert.Succeeded(res, "Expect GetElement to succeed");

            Assert.Equal(MI_Type.MI_STRING, elementType, "Expect that the property is registered as a string");
            Assert.Equal(TestEnumerateInstanceStringPropertyFlags,
                         elementFlags, "Expect the element flags to also be properly available from the query");
            Assert.Equal(TestEnumerateInstanceStringPropertyValue, elementValue.String, "Expect the machine name to have survived the whole journey");
        }
예제 #4
0
        internal CimException(MI_Result errorCode, string errorMessage, MI_Instance errorDetailsHandle, string exceptionMessage)
            : base(exceptionMessage ?? CimException.GetExceptionMessage(errorCode, errorMessage, errorDetailsHandle))
        {
            this.NativeErrorCode = errorCode.ToNativeErrorCode();

            if (errorDetailsHandle != null)
            {
                this._errorData = new CimInstance(errorDetailsHandle.Clone());
            }
        }
        public static MI_Instance Clone(this MI_Instance handleToClone)
        {
            if (handleToClone == null || handleToClone.IsNull)
            {
                throw new ArgumentNullException();
            }

            MI_Instance clonedHandle;
            MI_Result   result = handleToClone.Clone(out clonedHandle);

            CimException.ThrowIfMiResultFailure(result);
            return(clonedHandle);
        }
        internal void InstanceResultCallback(
            CimOperationCallbackProcessingContext callbackProcessingContext,
            MI_Operation operationHandle,
            MI_Instance instanceHandle,
            bool moreResults,
            MI_Result operationResult,
            String errorMessage,
            MI_Instance errorDetailsHandle)
        {
            CimMethodResult currentItem = null;

            if ((instanceHandle != null) && (!instanceHandle.IsNull))
            {
                if (!_shortenLifetimeOfResults)
                {
                    instanceHandle = instanceHandle.Clone();
                }
                var backingInstance = new CimInstance(instanceHandle);
                backingInstance.SetCimSessionComputerName(this._CimSessionComputerName);
                backingInstance.SetCimSessionInstanceId(this._CimSessionInstanceID);
                currentItem = new CimMethodResult(backingInstance);
            }

            try
            {
                this.ProcessNativeCallback(callbackProcessingContext, currentItem, moreResults, operationResult, errorMessage, errorDetailsHandle);
            }
            finally
            {
                if (_shortenLifetimeOfResults)
                {
                    if (currentItem != null)
                    {
                        currentItem.Dispose();
                    }
                }
            }
        }
        internal void IndicationResultCallback(
            CimOperationCallbackProcessingContext callbackProcessingContext,
            MI_Operation operationHandle,
            MI_Instance instanceHandle,
            String bookMark,
            String machineID,
            bool moreResults,
            MI_Result operationResult,
            String errorMessage,
            MI_Instance errorDetailsHandle)
        {
            CimSubscriptionResult currentItem = null;

            if ((instanceHandle != null) && (!instanceHandle.IsNull))
            {
                if (!_shortenLifetimeOfResults)
                {
                    instanceHandle = instanceHandle.Clone();
                }
                currentItem = new CimSubscriptionResult(instanceHandle, bookMark, machineID);
            }

            try
            {
                this.ProcessNativeCallback(callbackProcessingContext, currentItem, moreResults, operationResult, errorMessage, errorDetailsHandle);
            }
            finally
            {
                if (_shortenLifetimeOfResults)
                {
                    if (currentItem != null)
                    {
                        currentItem.Dispose();
                    }
                }
            }
        }
예제 #8
0
        internal static object ConvertFromNativeLayer(
            MI_Value value,
            MI_Type type,
            MI_Flags flags,
            CimInstance parent = null,
            bool clone         = false)
        {
            if ((flags & MI_Flags.MI_FLAG_NULL) == MI_Flags.MI_FLAG_NULL)
            {
                return(null);
            }

            if (type == MI_Type.MI_INSTANCE || type == MI_Type.MI_REFERENCE)
            {
                CimInstance instance = new CimInstance(
                    clone ? value.Instance.Clone() : value.Instance);
                if (parent != null)
                {
                    instance.SetCimSessionComputerName(parent.GetCimSessionComputerName());
                    instance.SetCimSessionInstanceId(parent.GetCimSessionInstanceId());
                }
                return(instance);
            }
            else if (type == MI_Type.MI_INSTANCEA || type == MI_Type.MI_REFERENCEA)
            {
                CimInstance[] arrayOfInstances = new CimInstance[value.InstanceA.Length];
                for (int i = 0; i < value.InstanceA.Length; i++)
                {
                    MI_Instance h = value.InstanceA[i];
                    if (h == null)
                    {
                        arrayOfInstances[i] = null;
                    }
                    else
                    {
                        arrayOfInstances[i] = new CimInstance(
                            clone ? h.Clone() : h);
                        if (parent != null)
                        {
                            arrayOfInstances[i].SetCimSessionComputerName(parent.GetCimSessionComputerName());
                            arrayOfInstances[i].SetCimSessionInstanceId(parent.GetCimSessionInstanceId());
                        }
                    }
                }
                return(arrayOfInstances);
            }
            else if (type == MI_Type.MI_DATETIME)
            {
                return(value.Datetime.ConvertFromNativeLayer());
            }
            else if (type == MI_Type.MI_DATETIMEA)
            {
                int      length           = value.DatetimeA.Length;
                object[] arrayOfDatetimes = new object[length];
                for (int i = 0; i < length; i++)
                {
                    arrayOfDatetimes[i] = value.DatetimeA[i].ConvertFromNativeLayer();
                }
                return(arrayOfDatetimes);
            }
            else
            {
                return(value.GetValue(type));
            }
        }