Exemple #1
0
 internal void Initialize()
 {
     if (this.prvpath != null)
     {
         if (!this.IsConnected)
         {
             lock (this)
             {
                 if (!this.IsConnected)
                 {
                     if (MTAHelper.IsNoContextMTA())
                     {
                         this.InitializeGuts(this);
                     }
                     else
                     {
                         ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.InitializeGuts));
                         threadDispatch.Parameter = this;
                         threadDispatch.Start();
                     }
                 }
             }
         }
         return;
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
        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();
            }
        }
Exemple #3
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);
        }
        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( );
            }
        }
Exemple #5
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);
                        }
                    }
                }
            }
        }
 internal static WmiGetEventSink GetWmiGetEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, ManagementObject managementObject)
 {
     if (!MTAHelper.IsNoContextMTA())
     {
         WmiGetEventSink.watcherParameter          = watcher;
         WmiGetEventSink.contextParameter          = context;
         WmiGetEventSink.scopeParameter            = scope;
         WmiGetEventSink.managementObjectParameter = managementObject;
         ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiGetEventSink.HackToCreateWmiGetEventSink));
         threadDispatch.Start();
         return(WmiGetEventSink.wmiGetEventSinkNew);
     }
     else
     {
         return(new WmiGetEventSink(watcher, context, scope, managementObject));
     }
 }
Exemple #7
0
		internal static WmiGetEventSink GetWmiGetEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, ManagementObject managementObject)
		{
			if (!MTAHelper.IsNoContextMTA())
			{
				WmiGetEventSink.watcherParameter = watcher;
				WmiGetEventSink.contextParameter = context;
				WmiGetEventSink.scopeParameter = scope;
				WmiGetEventSink.managementObjectParameter = managementObject;
				ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiGetEventSink.HackToCreateWmiGetEventSink));
				threadDispatch.Start();
				return WmiGetEventSink.wmiGetEventSinkNew;
			}
			else
			{
				return new WmiGetEventSink(watcher, context, scope, managementObject);
			}
		}
Exemple #8
0
 internal static WmiEventSink GetWmiEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, string path, string className)
 {
     if (!MTAHelper.IsNoContextMTA())
     {
         WmiEventSink.watcherParameter   = watcher;
         WmiEventSink.contextParameter   = context;
         WmiEventSink.scopeParameter     = scope;
         WmiEventSink.pathParameter      = path;
         WmiEventSink.classNameParameter = className;
         ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiEventSink.HackToCreateWmiEventSink));
         threadDispatch.Start();
         return(WmiEventSink.wmiEventSinkNew);
     }
     else
     {
         return(new WmiEventSink(watcher, context, scope, path, className));
     }
 }
 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())
     {
         ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.HackToCreateStubInMTA));
         threadDispatch.Parameter = this;
         threadDispatch.Start();
         return;
     }
     else
     {
         this.HackToCreateStubInMTA(this);
         return;
     }
 }
Exemple #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;
			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())
			{
				ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.HackToCreateStubInMTA));
				threadDispatch.Parameter = this;
				threadDispatch.Start();
				return;
			}
			else
			{
				this.HackToCreateStubInMTA(this);
				return;
			}
		}
Exemple #11
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();
            }
        }
Exemple #12
0
		public InstrumentedAssembly(Assembly assembly, SchemaNaming naming)
		{
			this.mapTypeToTypeInfo = new Hashtable();
			SecurityHelper.UnmanagedCode.Demand();
			this.naming = naming;
			Assembly precompiledAssembly = naming.PrecompiledAssembly;
			if (null == precompiledAssembly)
			{
				CSharpCodeProvider cSharpCodeProvider = new CSharpCodeProvider();
				CompilerParameters compilerParameter = new CompilerParameters();
				compilerParameter.GenerateInMemory = true;
				compilerParameter.ReferencedAssemblies.Add(assembly.Location);
				compilerParameter.ReferencedAssemblies.Add(typeof(BaseEvent).Assembly.Location);
				compilerParameter.ReferencedAssemblies.Add(typeof(Component).Assembly.Location);
				Type[] types = assembly.GetTypes();
				for (int i = 0; i < (int)types.Length; i++)
				{
					Type type = types[i];
					if (this.IsInstrumentedType(type))
					{
						this.FindReferences(type, compilerParameter);
					}
				}
				string[] code = new string[1];
				code[0] = naming.Code;
				CompilerResults compilerResult = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameter, code);
				foreach (CompilerError error in compilerResult.Errors)
				{
					Console.WriteLine(error.ToString());
				}
				if (!compilerResult.Errors.HasErrors)
				{
					precompiledAssembly = compilerResult.CompiledAssembly;
				}
				else
				{
					throw new Exception(RC.GetString("FAILED_TO_BUILD_GENERATED_ASSEMBLY"));
				}
			}
			Type type1 = precompiledAssembly.GetType("WMINET_Converter");
			this.mapTypeToConverter = (Hashtable)type1.GetField("mapTypeToConverter").GetValue(null);
			if (MTAHelper.IsNoContextMTA())
			{
				this.InitEventSource(this);
				return;
			}
			else
			{
				ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.InitEventSource));
				threadDispatch.Parameter = this;
				threadDispatch.Start();
				return;
			}
		}
Exemple #13
0
		private void InitializeThreadState(object threadParams, ThreadDispatch.ThreadWorkerMethodWithParam workerMethod, ApartmentState aptState, bool background)
		{
			this.threadParams = threadParams;
			this.threadWorkerMethodWithParam = workerMethod;
			this.thread = new Thread(new ThreadStart(this.ThreadEntryPointMethodWithParam));
			this.thread.SetApartmentState(aptState);
			this.backgroundThread = background;
		}
Exemple #14
0
		public ThreadDispatch(ThreadDispatch.ThreadWorkerMethod workerMethod) : this()
		{
			this.InitializeThreadState(null, workerMethod, ApartmentState.MTA, false);
		}
Exemple #15
0
		internal static WmiEventSink GetWmiEventSink(ManagementOperationObserver watcher, object context, ManagementScope scope, string path, string className)
		{
			if (!MTAHelper.IsNoContextMTA())
			{
				WmiEventSink.watcherParameter = watcher;
				WmiEventSink.contextParameter = context;
				WmiEventSink.scopeParameter = scope;
				WmiEventSink.pathParameter = path;
				WmiEventSink.classNameParameter = className;
				ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethod(WmiEventSink.HackToCreateWmiEventSink));
				threadDispatch.Start();
				return WmiEventSink.wmiEventSinkNew;
			}
			else
			{
				return new WmiEventSink(watcher, context, scope, path, className);
			}
		}
Exemple #16
0
		internal void Initialize()
		{
			if (this.prvpath != null)
			{
				if (!this.IsConnected)
				{
					lock (this)
					{
						if (!this.IsConnected)
						{
							if (MTAHelper.IsNoContextMTA())
							{
								this.InitializeGuts(this);
							}
							else
							{
								ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.InitializeGuts));
								threadDispatch.Parameter = this;
								threadDispatch.Start();
							}
						}
					}
				}
				return;
			}
			else
			{
				throw new InvalidOperationException();
			}
		}
Exemple #17
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);
                    }
                }
            }
        }
        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();
            }

        }
Exemple #19
0
		private void RelocateSinkRCWToMTA()
		{
			ThreadDispatch threadDispatch = new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.RelocateSinkRCWToMTA_ThreadFuncion));
			threadDispatch.Parameter = this;
			threadDispatch.Start();
		}
Exemple #20
0
       public InstrumentedAssembly(Assembly assembly, SchemaNaming naming)
        {
            SecurityHelper.UnmanagedCode.Demand(); // Bug#112640 - Close off any potential use from anything but fully trusted code
            this.naming = naming;

            Assembly compiledAssembly = naming.PrecompiledAssembly;
            if(null == compiledAssembly)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
//              ICodeCompiler compiler = provider.CreateCompiler();
                CompilerParameters parameters = new CompilerParameters();
                parameters.GenerateInMemory = true;
                parameters.ReferencedAssemblies.Add(assembly.Location);
                parameters.ReferencedAssemblies.Add(typeof(BaseEvent).Assembly.Location);
                parameters.ReferencedAssemblies.Add(typeof(System.ComponentModel.Component).Assembly.Location);

                // Must reference any base types in 'assembly'
                // 
                foreach(Type type in assembly.GetTypes())
                {
                    // Only interested in instrumented types (VSQFE#2469)
                    if (IsInstrumentedType(type))    
                        FindReferences(type, parameters);
                }

                CompilerResults results = provider.CompileAssemblyFromSource(parameters, naming.Code);
                foreach(CompilerError err in results.Errors)
                {
                    Console.WriteLine(err.ToString());
                }
                if (results.Errors.HasErrors)
                {
                    // we failed to compile the generated code - the compile error shows up on console but we need to throw
                    throw new Exception(RC.GetString("FAILED_TO_BUILD_GENERATED_ASSEMBLY"));
                }
                compiledAssembly = results.CompiledAssembly;
            }

            Type dynType = compiledAssembly.GetType("WMINET_Converter");
            mapTypeToConverter = (Hashtable)dynType.GetField("mapTypeToConverter").GetValue(null);

            // 
            if(!MTAHelper.IsNoContextMTA())  // Bug#110141 - Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
            {
                ThreadDispatch disp = new ThreadDispatch ( new ThreadDispatch.ThreadWorkerMethodWithParam ( InitEventSource ) ) ;
                disp.Parameter = this ;
                disp.Start ( ) ;
			
		 // We are on an STA thread.  Create the event source on an MTA
//                          Thread thread = new Thread(new ThreadStart(InitEventSource));
//                          thread.ApartmentState = ApartmentState.MTA;
//                          thread.Start();
//                          thread.Join();			
             }
                 else
             {
               InitEventSource( this ) ;
              }

        }