Solved

Exception thrown when start listening to window event

Posted on 2009-07-15
1
1,545 Views
Last Modified: 2013-12-17
Hi Experts,

I am trying to get the event everytime user invokes a window application.  For example, my application should get the event when user runs notepad.exe

I am using WMI to capture the windows event asynchronously, however, I got exception thrown (Exception from HRESULT: 0x80042002) everytime I starts to listen to the window event.

I would like to seek for your advice and guidence on solving this issue.
Thank you!



I am using C# 2005 and .Net Framework 2.0.

The following are the exception detail:
System.Runtime.InteropServices.COMException was unhandled
  Message="Exception from HRESULT: 0x80042002"
  Source="mscorlib"
  ErrorCode=-2147213310
  StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at System.Management.ManagementEventWatcher.Start()
       at EventMonitoring.WindowEventWatcher.Start() in C:\Projects\AppMonitoring\WindowEventWatcher.cs:line 54
       at EventMonitoring.FrmAppSelect..ctor() in C:\Projects\AppMonitoring\FrmAppSelect.cs:line 29
       at EventMonitoring.Program.Main() in C:\Projects\AppMonitoring\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

WqlEventQuery m_eventQuery = new WqlEventQuery("__InstanceCreationEvent", "TargetInstance isa \"Win32_Process\"");
private ManagementEventWatcher m_eventWatcher = new ManagementEventWatcher(m_eventQuery);
m_eventWatcher.EventArrived += new EventArrivedEventHandler(HandleEvent);
 
m_eventWatcher.Start();            // Exception thrown!!! (Exception from HRESULT: 0x80042002)

Open in new window

0
Comment
Question by:cskiong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24866704
Not sure...this works for me (I'm definitely no WMI expert!).  Note the extra parameter of a TimeSpan to the constructor of WqlEventQuery.
See: http://msdn.microsoft.com/en-us/library/6s90836x(VS.80).aspx
    public partial class Form1 : Form
    {
 
        private delegate void CreatedDelegate(EventArrivedEventArgs e);
        private ManagementEventWatcher watcherCreation;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            WqlEventQuery queryCreation = new WqlEventQuery("__InstanceCreationEvent",
                new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
            watcherCreation = new ManagementEventWatcher(queryCreation);
            watcherCreation.EventArrived += new EventArrivedEventHandler(watcherCreation_EventArrived);
            watcherCreation.Start();
        }
 
        void watcherCreation_EventArrived(object sender, EventArrivedEventArgs e)
        {
            if (this.InvokeRequired)
                this.Invoke(new CreatedDelegate(ProcessStarted), new Object[] { e });
            else
                ProcessStarted(e);
        }
 
        private void ProcessStarted(EventArrivedEventArgs e)
        {
            // do something in here...
            Console.WriteLine("Process Started @ " + DateTime.Now.ToString());
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            watcherCreation.Stop();
        }
 
    }

Open in new window

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 47
Activating .NET Framework 3.5 and which option to choose 5 60
SQL Server Serialization error 8 49
Shared Service Environment 2 55
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question