Exemple #1
0
 public void IndicateEvents(int length, IntPtr[] objects)
 {
     if (pSinkMTA == null)
     {
         return;
     }
     //
     // Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
     //
     if (MTAHelper.IsNoContextMTA())
     {
         // Sink lives in MTA
         int hresult = pSinkMTA.Indicate_(length, objects);
         if (hresult < 0)
         {
             if ((hresult & 0xfffff000) == 0x80041000)
             {
                 ManagementException.ThrowWithExtendedInfo((ManagementStatus)hresult);
             }
             else
             {
                 Marshal.ThrowExceptionForHR(hresult);
             }
         }
     }
     else
     {
         MTARequest myReq = new MTARequest(length, objects);
         int        ndx;
         lock (critSec)
         {
             // Do it from worker thread
             if (workerThreadInitialized == false)
             {
                 // We've never created worker thread
                 Thread thread = new Thread(new ThreadStart(MTAWorkerThread2));
                 thread.IsBackground = true;
                 thread.SetApartmentState(ApartmentState.MTA);
                 thread.Start();
                 workerThreadInitialized = true;
             }
             ndx = reqList.Add(myReq);
             if (doIndicate.Set() == false)  //SetEvent(hDoIndicate);
             {
                 reqList.RemoveAt(ndx);
                 throw new ManagementException(RC.GetString("WORKER_THREAD_WAKEUP_FAILED"));
             }
         }
         myReq.doneIndicate.WaitOne(); //WaitForSingleObject(hDoneIndicate, -1);
         if (myReq.exception != null)
         {
             throw myReq.exception;
         }
     }
     GC.KeepAlive(this);
 }
Exemple #2
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 #3
0
 public void IndicateEvents(int length, IntPtr[] objects)
 {
     if (this.pSinkMTA != null)
     {
         if (!MTAHelper.IsNoContextMTA())
         {
             EventSource.MTARequest mTARequest = new EventSource.MTARequest(length, objects);
             lock (this.critSec)
             {
                 if (!this.workerThreadInitialized)
                 {
                     Thread thread = new Thread(new ThreadStart(this.MTAWorkerThread2));
                     thread.IsBackground = true;
                     thread.SetApartmentState(ApartmentState.MTA);
                     thread.Start();
                     this.workerThreadInitialized = true;
                 }
                 int num = this.reqList.Add(mTARequest);
                 if (!this.doIndicate.Set())
                 {
                     this.reqList.RemoveAt(num);
                     throw new ManagementException(RC.GetString("WORKER_THREAD_WAKEUP_FAILED"));
                 }
             }
             mTARequest.doneIndicate.WaitOne();
             if (mTARequest.exception != null)
             {
                 throw mTARequest.exception;
             }
         }
         else
         {
             int num1 = this.pSinkMTA.Indicate_(length, objects);
             if (num1 < 0)
             {
                 if (((long)num1 & (long)-4096) != (long)-2147217408)
                 {
                     Marshal.ThrowExceptionForHR(num1);
                 }
                 else
                 {
                     ManagementException.ThrowWithExtendedInfo((ManagementStatus)num1);
                 }
             }
         }
         GC.KeepAlive(this);
         return;
     }
     else
     {
         return;
     }
 }
Exemple #4
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'
                // TODO: Make this more restrictive.  Only look at instrumented types.
                foreach (Type type in assembly.GetTypes())
                {
                    FindReferences(type, parameters);
                }

                CompilerResults results = compiler.CompileAssemblyFromSource(parameters, naming.Code);
                foreach (CompilerError err in results.Errors)
                {
                    Console.WriteLine(err.ToString());
                }
                compiledAssembly = results.CompiledAssembly;
            }
            Type dynType = compiledAssembly.GetType("WMINET_Converter");

            mapTypeToConverter = (Hashtable)dynType.GetField("mapTypeToConverter").GetValue(null);

            // TODO: Is STA/MTA all we have to worry about?
            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);
            }
        }
Exemple #5
0
 public void IndicateEvents(int length, IntPtr[] objects)
 {
     if (this.pSinkMTA != null)
     {
         if (MTAHelper.IsNoContextMTA())
         {
             int errorCode = this.pSinkMTA.Indicate_(length, objects);
             if (errorCode < 0)
             {
                 if ((errorCode & 0xfffff000L) == 0x80041000L)
                 {
                     ManagementException.ThrowWithExtendedInfo((ManagementStatus)errorCode);
                 }
                 else
                 {
                     Marshal.ThrowExceptionForHR(errorCode);
                 }
             }
         }
         else
         {
             MTARequest request = new MTARequest(length, objects);
             lock (this.critSec)
             {
                 if (!this.workerThreadInitialized)
                 {
                     Thread thread = new Thread(new ThreadStart(this.MTAWorkerThread2))
                     {
                         IsBackground = true
                     };
                     thread.SetApartmentState(ApartmentState.MTA);
                     thread.Start();
                     this.workerThreadInitialized = true;
                 }
                 int index = this.reqList.Add(request);
                 if (!this.doIndicate.Set())
                 {
                     this.reqList.RemoveAt(index);
                     throw new ManagementException(RC.GetString("WORKER_THREAD_WAKEUP_FAILED"));
                 }
             }
             request.doneIndicate.WaitOne();
             if (request.exception != null)
             {
                 throw request.exception;
             }
         }
         GC.KeepAlive(this);
     }
 }
Exemple #6
0
        public InstrumentedAssembly(Assembly assembly, SchemaNaming naming)
        {
            SecurityHelper.UnmanagedCode.Demand();
            this.naming = naming;
            Assembly precompiledAssembly = naming.PrecompiledAssembly;

            if (null == precompiledAssembly)
            {
                CSharpCodeProvider provider   = new CSharpCodeProvider();
                CompilerParameters parameters = new CompilerParameters {
                    GenerateInMemory = true
                };
                parameters.ReferencedAssemblies.Add(assembly.Location);
                parameters.ReferencedAssemblies.Add(typeof(BaseEvent).Assembly.Location);
                parameters.ReferencedAssemblies.Add(typeof(Component).Assembly.Location);
                foreach (Type type in assembly.GetTypes())
                {
                    if (this.IsInstrumentedType(type))
                    {
                        this.FindReferences(type, parameters);
                    }
                }
                CompilerResults results = provider.CompileAssemblyFromSource(parameters, new string[] { naming.Code });
                foreach (CompilerError error in results.Errors)
                {
                    Console.WriteLine(error.ToString());
                }
                if (results.Errors.HasErrors)
                {
                    throw new Exception(RC.GetString("FAILED_TO_BUILD_GENERATED_ASSEMBLY"));
                }
                precompiledAssembly = results.CompiledAssembly;
            }
            Type type2 = precompiledAssembly.GetType("WMINET_Converter");

            this.mapTypeToConverter = (Hashtable)type2.GetField("mapTypeToConverter").GetValue(null);
            if (!MTAHelper.IsNoContextMTA())
            {
                new ThreadDispatch(new ThreadDispatch.ThreadWorkerMethodWithParam(this.InitEventSource))
                {
                    Parameter = this
                }.Start();
            }
            else
            {
                this.InitEventSource(this);
            }
        }
Exemple #7
0
        public void IndicateEvents(int length, IntPtr[] objects)
        {
            if (pSinkMTA == null)
            {
                return;
            }
            if (MTAHelper.IsNoContextMTA()) // Bug#110141 - Checking for MTA is not enough.  We need to make sure we are not in a COM+ Context
            {
                // Sink lives in MTA
                int hresult = pSinkMTA.Indicate_(length, objects);
                if (hresult < 0)
                {
                    if ((hresult & 0xfffff000) == 0x80041000)
                    {
                        ManagementException.ThrowWithExtendedInfo((ManagementStatus)hresult);
                    }
                    else
                    {
                        Marshal.ThrowExceptionForHR(hresult);
                    }
                }
            }
            else
            {
                // Do it from worker thread
                if (lengthFromSTA == -1)
                {
                    // We've never created worker thread
                    Thread thread = new Thread(new ThreadStart(MTAWorkerThread2));
                    thread.IsBackground   = true; // bug#92154 - worker thread must be allowed to die on process shutdown
                    thread.ApartmentState = ApartmentState.MTA;
                    thread.Start();
                }
                lengthFromSTA  = length;
                objectsFromSTA = objects;
                SetEvent(hDoIndicate);
                WaitForSingleObject(hDoneIndicate, -1);
                if (exception != null)
                {
                    throw exception;
                }

//                doIndicate.Set();
//                doneIndicate.WaitOne();
            }
        }
Exemple #8
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);
            }
        }