Beispiel #1
0
        public static int Main(string[] args)
        {
            try
            {
                // **DESTINATION**
                // Create watcher and completionHandler

                ManagementOperationWatcher delete_res            = new ManagementOperationWatcher();
                ManagementOperationWatcher results               = new ManagementOperationWatcher();
                CompletionHandler          completionHandler     = new CompletionHandler();
                CompletionHandler          completionHandler_res = new CompletionHandler();
                delete_res.Completed += new CompletedEventHandler(completionHandler_res.Done);
                results.Completed    += new CompletedEventHandler(completionHandler.Done);
                PutHandler putHandler = new PutHandler();
                results.ObjectPut += new ObjectPutEventHandler(putHandler.JustPut);

                // Create the class TestDelClassasync for deletion later
                // **SOURCE **
                ManagementClass newclassObj = new ManagementClass("root/default", "", null);
                newclassObj.SystemProperties["__CLASS"].Value = "TestDelClassasync";
                PropertySet mykeyprop = newclassObj.Properties;
                mykeyprop.Add("MyKey", "Hello");

                Console.WriteLine("Thread is {0}", System.Threading.Thread.CurrentThread.ApartmentState);
                newclassObj.Put(results);
                while (!completionHandler.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }
                completionHandler.Reset();

                ManagementClass dummyClassCheck = new ManagementClass("root/default", "TestDelClassasync", null);
                //dummyClassCheck.Get();
                Console.WriteLine(dummyClassCheck.SystemProperties["__Class"].Value.ToString());

                // Delete the Class aync
                newclassObj.Delete(delete_res);
                while (!completionHandler_res.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }
                completionHandler_res.Reset();
                if ("System.Management.ManagementOperationWatcher" == completionHandler_res.Sender)
                {
                    Console.WriteLine("Test 10.2: Able to delete classes asynchronously.");
                }
                else
                {
                    Console.WriteLine("Test 10.2: Unable to delete classes asynchronously.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Test 10.2: " + e.GetType().ToString());
                Console.WriteLine(e.Message + e.StackTrace);
            }


            return(0);
        }
Beispiel #2
0
        public static int Main(string[] args)
        {
            ManagementObject disk = new ManagementObject("win32_logicaldisk");

            Console.WriteLine("Class name is " + disk.SystemProperties ["__CLASS"]);

            ManagementObject diskC = new ManagementObject("win32_logicaldisk='C:'");

            Console.WriteLine("Freespace is " + diskC["FreeSpace"]);

            ManagementObject           process0 = new ManagementObject("Win32_Process='0'");
            ManagementOperationWatcher watcher  = new ManagementOperationWatcher();

            CompletionHandler completionHandler = new CompletionHandler();

            watcher.Completed += new CompletedEventHandler(completionHandler.Done);

            process0.Get(watcher);

            while (!completionHandler.IsComplete)
            {
                System.Threading.Thread.Sleep(1000);
            }

            Console.WriteLine(process0["Name"]);

            return(0);
        }
Beispiel #3
0
        protected void buttonStart_Click(object sender, System.EventArgs e)
        {
            ManagementOperationWatcher watcher = new ManagementOperationWatcher();
            CompletionHandler          handler = new CompletionHandler(labelStatus);
            int opCount = 4;

            // Attach some event handlers for the object
            watcher.ObjectReady += new ObjectReadyEventHandler(handler.HandleObject);
            watcher.ObjectReady += new ObjectReadyEventHandler(handler.HandleObject);

            if (checkBoxIncludeThrowingDelegate.Checked)
            {
                watcher.ObjectReady += new ObjectReadyEventHandler(handler.HandleObjectWithException);
                opCount++;
            }

            watcher.ObjectReady += new ObjectReadyEventHandler(handler.HandleObject);
            watcher.ObjectReady += new ObjectReadyEventHandler(handler.HandleObject);

            // Add a completed event handler
            watcher.Completed += new CompletedEventHandler(handler.Done);

            // record the number of listeners
            handler.SetInitialOpCount(opCount);

            // go
            searcher.Get(watcher);
        }
Beispiel #4
0
        public static int Main(string[] args)
        {
            ManagementClass processClass = new ManagementClass("Win32_Process");

            // Create a watcher and completion handler
            ManagementOperationWatcher watcher    = new ManagementOperationWatcher();
            ObjectReadyHandler         objHandler = new ObjectReadyHandler();

            watcher.ObjectReady += new ObjectReadyEventHandler(objHandler.NewObject);

            // Invoke using parameter instances
            ManagementBaseObject inParams = processClass.GetMethodParameters("Create");

            inParams.Properties["CommandLine"].Value = "calc.exe";
            processClass.InvokeMethod(watcher, "Create", inParams, null);

            while (!objHandler.IsComplete)                              //Infinite loop here
            {
                System.Threading.Thread.Sleep(1000);
            }
            objHandler.Reset();

            ManagementBaseObject o = objHandler.ReturnObject;

            Console.WriteLine(o.GetText(TextFormat.MOF));
            Console.WriteLine("Creation of calculator process returned: " + o["returnValue"]);
            Console.WriteLine("Process id: " + o["processId"]);

            return(0);
        }
Beispiel #5
0
        public static int Main(string[] args)
        {
            Console.WriteLine("Main executing on thread " + System.Threading.Thread.CurrentThread.GetHashCode());
            // Sync

            try {
                ManagementObject diskC = new ManagementObject("Win32_logicaldisk='C:'");
                diskC.Get();
                Console.WriteLine(diskC["FreeSpace"]);
            } catch (ManagementException e) {
                Console.WriteLine("Caught WMI Exception: " + e.Message + e.ErrorCode);
            }

            // Now do it async
            ManagementOperationWatcher results  = new ManagementOperationWatcher();
            ObjectHandler     objHandler        = new ObjectHandler("LaLa");
            ObjectHandler     objHandler2       = new ObjectHandler("Po");
            CompletionHandler completionHandler = new CompletionHandler();

            results.ObjectReady += new ObjectReadyEventHandler(objHandler.NewObject);
            results.ObjectReady += new ObjectReadyEventHandler(objHandler2.NewObject);
            results.Completed   += new CompletedEventHandler(completionHandler.Done);

            // 1) Simple Get

            Console.WriteLine("\nObject Get\n");
            Console.WriteLine("==========\n\n");

            try {
                ManagementObject diskC = new ManagementObject("Win32_logicaldisk='C:'");
                diskC.Get(results);

                while (!completionHandler.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                completionHandler.Reset();
            } catch (ManagementException e) {
                Console.WriteLine("Caught WMI Exception (Get): " + e.Message + e.ErrorCode);
            }

            // 2) Enumeration

            Console.WriteLine("\nObject Enumeration\n");
            Console.WriteLine("==================\n\n");

            try {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(new SelectQuery("win32_logicaldisk"));
                searcher.Get(results);

                while (!completionHandler.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                completionHandler.Reset();
            }
            catch (ManagementException e) {
                Console.WriteLine("Caught WMI Exception (Query): " + e.Message + e.ErrorCode);
            }


            // 3) Object Put
            Console.WriteLine("\nObject Put\n");
            Console.WriteLine("==========\n\n");
            PutHandler putHandler = new PutHandler();

            results.ObjectPut += new ObjectPutEventHandler(putHandler.JustPut);

            try {
                ManagementObject newObj = new ManagementObject("root/default:__cimomidentification=@");
                newObj.Get();
                newObj.Put(results);

                while (!completionHandler.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                completionHandler.Reset();
            } catch (ManagementException e) {
                Console.WriteLine("Caught WMI Exception (Put): " + e.Message + e.ErrorCode);
            }

            // 4) Use a listener that throws an exception
            try {
                Console.WriteLine("\nCreating listener with exception\n");
                Console.WriteLine("================================\n\n");
                ManagementObjectSearcher objS = new ManagementObjectSearcher
                                                    ("root/default", "select * from __CIMOMIDentification");
                ManagementOperationWatcher watcher = new ManagementOperationWatcher();
                ObjectHandler objH = new ObjectHandler("Dipsy");
                watcher.ObjectReady += new ObjectReadyEventHandler(objH.NewObject);
                watcher.ObjectReady += new ObjectReadyEventHandler(objH.NewObjectWithException);
                watcher.ObjectReady += new ObjectReadyEventHandler(objH.NewObject);
                watcher.ObjectReady += new ObjectReadyEventHandler(objH.NewObject);
                watcher.Completed   += new CompletedEventHandler(completionHandler.Done);

                objS.Get(watcher);

                while (!completionHandler.IsComplete)
                {
                    System.Threading.Thread.Sleep(1000);
                }

                completionHandler.Reset();
            }
            catch (ManagementException e)
            {
                Console.WriteLine("Caught WMI Exception (Put): " + e.Message + e.ErrorCode);
            }

            return(0);
        }
Beispiel #6
0
        public static int Main(string[] args)
        {
            // Explicit scope + data query
            ManagementScope scope = new ManagementScope("root/cimv2");
            DataQuery       query = new DataQuery("select * from Win32_process");

            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(query);

            ManagementObjectCollection processes = searcher.Get();

            foreach (ManagementBaseObject process in processes)
            {
                Console.WriteLine(process["Name"]);
            }

            // Implicit scope + data query
            DataQuery query2 = new DataQuery("select * from win32_logicaldisk");
            ManagementObjectSearcher searcher2 =
                new ManagementObjectSearcher(query2);

            ManagementObjectCollection disks = searcher2.Get();

            foreach (ManagementObject disk in disks)
            {
                Console.WriteLine(disk["Freespace"]);
            }

            // Implicit scope + select query
            ManagementObjectSearcher searcher3 =
                new ManagementObjectSearcher(new SelectQuery("win32_service"));

            foreach (ManagementObject service in searcher3.Get())
            {
                Console.WriteLine(service["Name"]);
            }

            //Asynchronous query
            ManagementOperationWatcher l        = new ManagementOperationWatcher();
            ObjectHandler     objHandler        = new ObjectHandler("LaLa");
            ObjectHandler     objHandler2       = new ObjectHandler("Po");
            CompletionHandler completionHandler = new CompletionHandler();

            l.ObjectReady += new ObjectReadyEventHandler(objHandler.NewObject);
            l.ObjectReady += new ObjectReadyEventHandler(objHandler2.NewObject);
            l.Completed   += new CompletedEventHandler(completionHandler.Done);

            searcher2.Get(l);

            while (!completionHandler.IsComplete)
            {
                System.Threading.Thread.Sleep(1000);
            }

            //Event watcher
            ManagementEventWatcher w = new ManagementEventWatcher(
                "select * from __instancemodificationevent within 5 where targetinstance isa 'Win32_Process'");

            try {
                ManagementBaseObject o = w.WaitForNextEvent();
                Console.WriteLine(o["__CLASS"]);
            } catch (ManagementException e) {
                Console.WriteLine("Exception: " + e.Message);
            }
            return(0);
        }