Пример #1
0
        private void STAComCreateThreadProc(object createstruct)
        {
            ComCreateInfo info = (ComCreateInfo)createstruct;

            try
            {
                Type type = null;
                PSArgumentException mshArgE = null;

                type = Type.GetTypeFromCLSID(_comObjectClsId);
                if (type == null)
                {
                    mshArgE = PSTraceSource.NewArgumentException(
                        "ComObject",
                        NewObjectStrings.CannotLoadComObjectType,
                        ComObject);

                    info.e       = mshArgE;
                    info.success = false;
                    return;
                }

                info.objectCreated = SafeCreateInstance(type, ArgumentList);
                info.success       = true;
            }
            catch (Exception e)
            {
                info.e       = e;
                info.success = false;
            }
        }
Пример #2
0
        private void STAComCreateThreadProc(object createstruct)
        {
            ComCreateInfo info = (ComCreateInfo)createstruct;

            try
            {
                Type t = null;
                PSArgumentException exception = null;
                t = Type.GetTypeFromCLSID(this.comObjectClsId);
                if (t == null)
                {
                    exception    = PSTraceSource.NewArgumentException("ComObject", "NewObjectStrings", "CannotLoadComObjectType", new object[] { this.comObject });
                    info.e       = exception;
                    info.success = false;
                }
                else
                {
                    info.objectCreated = this.SafeCreateInstance(t, this.arguments);
                    info.success       = true;
                }
            }
            catch (Exception exception2)
            {
                CommandProcessorBase.CheckForSevereException(exception2);
                info.e       = exception2;
                info.success = false;
            }
        }
Пример #3
0
        private object CreateComObject()
        {
            Type t = null;
            PSArgumentException exception = null;

            try
            {
                t = Type.GetTypeFromCLSID(this.comObjectClsId);
                if (t == null)
                {
                    exception = PSTraceSource.NewArgumentException("ComObject", "NewObjectStrings", "CannotLoadComObjectType", new object[] { this.comObject });
                    base.ThrowTerminatingError(new ErrorRecord(exception, "CannotLoadComObjectType", ErrorCategory.InvalidType, null));
                }
                return(this.SafeCreateInstance(t, this.arguments));
            }
            catch (COMException exception2)
            {
                if (exception2.ErrorCode == -2147417850)
                {
                    this.createInfo = new ComCreateInfo();
                    Thread thread = new Thread(new ParameterizedThreadStart(this.STAComCreateThreadProc));
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start(this.createInfo);
                    thread.Join();
                    if (this.createInfo.success)
                    {
                        return(this.createInfo.objectCreated);
                    }
                    base.ThrowTerminatingError(new ErrorRecord(this.createInfo.e, "NoCOMClassIdentified", ErrorCategory.ResourceUnavailable, null));
                    return(null);
                }
                base.ThrowTerminatingError(new ErrorRecord(exception2, "NoCOMClassIdentified", ErrorCategory.ResourceUnavailable, null));
                return(null);
            }
        }
        private object CreateComObject()
        {
            Type type = null;
            PSArgumentException mshArgE = null;

            try
            {
                type = Marshal.GetTypeFromCLSID(_comObjectClsId);
                if (type == null)
                {
                    mshArgE = PSTraceSource.NewArgumentException("ComObject", NewObjectStrings.CannotLoadComObjectType, ComObject);
                    ThrowTerminatingError(
                        new ErrorRecord(mshArgE, "CannotLoadComObjectType", ErrorCategory.InvalidType, null));
                }

                return(SafeCreateInstance(type, ArgumentList));
            }
            catch (COMException e)
            {
                // Check Error Code to see if Error is because of Com apartment Mismatch.
                if (e.HResult == RPC_E_CHANGED_MODE)
                {
#if CORECLR
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new COMException(StringUtil.Format(NewObjectStrings.ApartmentNotSupported, e.Message), e),
                            "NoCOMClassIdentified",
                            ErrorCategory.ResourceUnavailable, null));
#else
                    createInfo = new ComCreateInfo();

                    Thread thread = new Thread(new ParameterizedThreadStart(STAComCreateThreadProc));
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start(createInfo);

                    thread.Join();

                    if (createInfo.success == true)
                    {
                        return(createInfo.objectCreated);
                    }

                    ThrowTerminatingError(
                        new ErrorRecord(createInfo.e, "NoCOMClassIdentified",
                                        ErrorCategory.ResourceUnavailable, null));
#endif
                }
                else
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            e,
                            "NoCOMClassIdentified",
                            ErrorCategory.ResourceUnavailable, null));
                }

                return(null);
            }
        }
Пример #5
0
        private object CreateComObject()
        {
            Type type = null;
            PSArgumentException mshArgE = null;

            try
            {
                type = Marshal.GetTypeFromCLSID(_comObjectClsId);
                if (type == null)
                {
                    mshArgE = PSTraceSource.NewArgumentException("ComObject", NewObjectStrings.CannotLoadComObjectType, ComObject);
                    ThrowTerminatingError(
                        new ErrorRecord(mshArgE, "CannotLoadComObjectType", ErrorCategory.InvalidType, null));
                }
                return SafeCreateInstance(type, ArgumentList);
            }
            catch (COMException e)
            {
                //Check Error Code to see if Error is because of Com apartment Mismatch.
                if (e.HResult == RPC_E_CHANGED_MODE)
                {
#if CORECLR
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new COMException(StringUtil.Format(NewObjectStrings.ApartmentNotSupported, e.Message), e),
                            "NoCOMClassIdentified",
                            ErrorCategory.ResourceUnavailable, null));
#else
                    createInfo = new ComCreateInfo();

                    Thread thread = new Thread(new ParameterizedThreadStart(STAComCreateThreadProc));
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start(createInfo);

                    thread.Join();

                    if (createInfo.success == true)
                    {
                        return createInfo.objectCreated;
                    }

                    ThrowTerminatingError(
                             new ErrorRecord(createInfo.e, "NoCOMClassIdentified",
                                                    ErrorCategory.ResourceUnavailable, null));
#endif
                }
                else
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                        e,
                        "NoCOMClassIdentified",
                        ErrorCategory.ResourceUnavailable, null));
                }

                return null;
            }
        }
Пример #6
0
 private object CreateComObject()
 {
     Type t = null;
     PSArgumentException exception = null;
     try
     {
         t = Type.GetTypeFromCLSID(this.comObjectClsId);
         if (t == null)
         {
             exception = PSTraceSource.NewArgumentException("ComObject", "NewObjectStrings", "CannotLoadComObjectType", new object[] { this.comObject });
             base.ThrowTerminatingError(new ErrorRecord(exception, "CannotLoadComObjectType", ErrorCategory.InvalidType, null));
         }
         return this.SafeCreateInstance(t, this.arguments);
     }
     catch (COMException exception2)
     {
         if (exception2.ErrorCode == -2147417850)
         {
             this.createInfo = new ComCreateInfo();
             Thread thread = new Thread(new ParameterizedThreadStart(this.STAComCreateThreadProc));
             thread.SetApartmentState(ApartmentState.STA);
             thread.Start(this.createInfo);
             thread.Join();
             if (this.createInfo.success)
             {
                 return this.createInfo.objectCreated;
             }
             base.ThrowTerminatingError(new ErrorRecord(this.createInfo.e, "NoCOMClassIdentified", ErrorCategory.ResourceUnavailable, null));
             return null;
         }
         base.ThrowTerminatingError(new ErrorRecord(exception2, "NoCOMClassIdentified", ErrorCategory.ResourceUnavailable, null));
         return null;
     }
 }