public SinkForEventQuery(ManagementEventWatcher eventWatcher,
                                 object context,
                                 IWbemServices services)
        {
            this.services     = services;
            this.context      = context;
            this.eventWatcher = eventWatcher;
            this.status       = 0;
            this.isLocal      = false;

            // determine if the server is local, and if so don't create a real stub using unsecap
            if ((0 == String.Compare(eventWatcher.Scope.Path.Server, ".", StringComparison.OrdinalIgnoreCase)) ||
                (0 == String.Compare(eventWatcher.Scope.Path.Server, System.Environment.MachineName, StringComparison.OrdinalIgnoreCase)))
            {
                this.isLocal = true;
            }

            if (MTAHelper.IsNoContextMTA())
            {
                HackToCreateStubInMTA(this);
            }
            else
            {
                //
                // Ensure we are able to trap exceptions from worker thread.
                //
                ThreadDispatch disp = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(HackToCreateStubInMTA));
                disp.Parameter = this;
                disp.Start( );
            }
        }
Example #2
0
        internal static WmiEventSink GetWmiEventSink(
            ManagementOperationObserver watcher,
            object context,
            ManagementScope scope,
            string path,
            string className)
        {
            if (MTAHelper.IsNoContextMTA())
            {
                return(new WmiEventSink(watcher, context, scope, path, className));
            }

            watcherParameter   = watcher;
            contextParameter   = context;
            scopeParameter     = scope;
            pathParameter      = path;
            classNameParameter = className;

            //
            // Ensure we are able to trap exceptions from worker thread.
            //
            ThreadDispatch disp = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(HackToCreateWmiEventSink));

            disp.Start( );

            return(wmiEventSinkNew);
        }
Example #3
0
 public static object CreateInMTA(Type type)
 {
     if (!MTAHelper.IsNoContextMTA())
     {
         MTAHelper.MTARequest mTARequest = new MTAHelper.MTARequest(type);
         lock (MTAHelper.critSec)
         {
             if (!MTAHelper.workerThreadInitialized)
             {
                 MTAHelper.InitWorkerThread();
                 MTAHelper.workerThreadInitialized = true;
             }
             int num = MTAHelper.reqList.Add(mTARequest);
             if (!MTAHelper.evtGo.Set())
             {
                 MTAHelper.reqList.RemoveAt(num);
                 throw new ManagementException(RC.GetString("WORKER_THREAD_WAKEUP_FAILED"));
             }
         }
         mTARequest.evtDone.WaitOne();
         if (mTARequest.exception == null)
         {
             return(mTARequest.createdObject);
         }
         else
         {
             throw mTARequest.exception;
         }
     }
     else
     {
         return(Activator.CreateInstance(type));
     }
 }
Example #4
0
 internal void Initialize()
 {
     if (this.prvpath == null)
     {
         throw new InvalidOperationException();
     }
     if (!this.IsConnected)
     {
         lock (this)
         {
             if (!this.IsConnected)
             {
                 if (!MTAHelper.IsNoContextMTA())
                 {
                     new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.InitializeGuts))
                     {
                         Parameter = this
                     }.Start();
                 }
                 else
                 {
                     this.InitializeGuts(this);
                 }
             }
         }
     }
 }
        private IWbemObjectSink stub;                           // The secured IWbemObjectSink

        public SinkForEventQuery(ManagementEventWatcher eventWatcher,
                                 object context,
                                 IWbemServices services)
        {
            this.services     = services;
            this.context      = context;
            this.eventWatcher = eventWatcher;
            if (MTAHelper.IsNoContextMTA())  // Bug#110141 - Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
            {
                HackToCreateStubInMTA(this);
            }
            else
            {
                //
                // [marioh, RAID: 111108]
                // Ensure we are able to trap exceptions from worker thread.
                //
                ThreadDispatch disp = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(HackToCreateStubInMTA));
                disp.Parameter = this;
                disp.Start( );
//				Thread thread = new Thread(new ThreadStart(HackToCreateStubInMTA));
//                thread.ApartmentState = ApartmentState.MTA;
//                thread.Start();
//                thread.Join();
            }
        }
Example #6
0
        internal void Initialize()
        {
            //If the path is not set yet we can't do it
            if (null == prvpath)
            {
                throw new InvalidOperationException();
            }


            /*
             * If we're not connected yet, this is the time to do it... We lock
             * the state to prevent 2 threads simultaneously doing the same
             * connection. To avoid taking the lock unnecessarily we examine
             * isConnected first
             */
            if (!IsConnected)
            {
                lock (this)
                {
                    if (!IsConnected)
                    {
                        // The locator cannot be marshalled accross apartments, so we must create the locator
                        // and get the IWbemServices from an MTA thread
                        if (!MTAHelper.IsNoContextMTA())  // Bug#110141 - Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
                        {
                            //
                            // [marioh, RAID: 111108]
                            // Ensure we are able to trap exceptions from worker thread.
                            //
                            ThreadDispatch disp = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(InitializeGuts));
                            disp.Parameter = this;
                            disp.Start( );


//                            statusFromMTA = 0;
//                            Thread thread = new Thread(new ThreadStart(InitializeGuts));
//                            thread.ApartmentState = ApartmentState.MTA;
//                            thread.Start();
//                            thread.Join();
//                            if ((statusFromMTA & 0xfffff000) == 0x80041000)
//                                ManagementException.ThrowWithExtendedInfo((ManagementStatus)statusFromMTA);
//                            else if ((statusFromMTA & 0x80000000) != 0)
//                                Marshal.ThrowExceptionForHR(statusFromMTA);
                        }
                        else
                        {
                            InitializeGuts(this);
                        }
                    }
                }
            }
        }
Example #7
0
 internal static WmiGetEventSink GetWmiGetEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, ManagementObject managementObject)
 {
     if (MTAHelper.IsNoContextMTA())
     {
         return(new WmiGetEventSink(watcher, context, scope, managementObject));
     }
     watcherParameter          = watcher;
     contextParameter          = context;
     scopeParameter            = scope;
     managementObjectParameter = managementObject;
     new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiGetEventSink.HackToCreateWmiGetEventSink)).Start();
     return(wmiGetEventSinkNew);
 }
Example #8
0
 internal static WmiEventSink GetWmiEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, string path, string className)
 {
     if (MTAHelper.IsNoContextMTA())
     {
         return(new WmiEventSink(watcher, context, scope, path, className));
     }
     watcherParameter   = watcher;
     contextParameter   = context;
     scopeParameter     = scope;
     pathParameter      = path;
     classNameParameter = className;
     new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiEventSink.HackToCreateWmiEventSink)).Start();
     return(wmiEventSinkNew);
 }
Example #9
0
 public SinkForEventQuery(ManagementEventWatcher eventWatcher, object context, IWbemServices services)
 {
     this.services     = services;
     this.context      = context;
     this.eventWatcher = eventWatcher;
     this.status       = 0;
     this.isLocal      = false;
     if ((string.Compare(eventWatcher.Scope.Path.Server, ".", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(eventWatcher.Scope.Path.Server, Environment.MachineName, StringComparison.OrdinalIgnoreCase) == 0))
     {
         this.isLocal = true;
     }
     if (MTAHelper.IsNoContextMTA())
     {
         this.HackToCreateStubInMTA(this);
     }
     else
     {
         new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.HackToCreateStubInMTA))
         {
             Parameter = this
         }.Start();
     }
 }
Example #10
0
        public SinkForEventQuery(ManagementEventWatcher eventWatcher,
                                 object context,
                                 IWbemServices services)
        {
            this.services     = services;
            this.context      = context;
            this.eventWatcher = eventWatcher;
            this.status       = 0;
            this.isLocal      = false;

            // determine if the server is local, and if so don't create a real stub using unsecap
            if ((0 == String.Compare(eventWatcher.Scope.Path.Server, ".", StringComparison.OrdinalIgnoreCase)) ||
                (0 == String.Compare(eventWatcher.Scope.Path.Server, System.Environment.MachineName, StringComparison.OrdinalIgnoreCase)))
            {
                this.isLocal = true;
            }

            if (MTAHelper.IsNoContextMTA())  // Bug#110141 - Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
            {
                HackToCreateStubInMTA(this);
            }
            else
            {
                //
                // [marioh, RAID: 111108]
                // Ensure we are able to trap exceptions from worker thread.
                //
                ThreadDispatch disp = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(HackToCreateStubInMTA));
                disp.Parameter = this;
                disp.Start( );
                //            Thread thread = new Thread(new ThreadStart(HackToCreateStubInMTA));
                //            thread.ApartmentState = ApartmentState.MTA;
                //            thread.Start();
                //            thread.Join();
            }
        }