Reputation: 135
I've converted a process creation watcher from C# to VB.Net and am having problems converting a certian line of code that's giving me trouble.
Here's the original C# code:
using System;
using System.ComponentModel;
using System.Collections;
using System.Globalization;
using System.Management;
namespace WMI.Win32
{
public delegate void ProcessEventHandler(Win32_Process proc);
public class ProcessWatcher : ManagementEventWatcher
{
// Process Events
public event ProcessEventHandler ProcessCreated;
public event ProcessEventHandler ProcessDeleted;
public event ProcessEventHandler ProcessModified;
// WMI WQL process query strings
static readonly string WMI_OPER_EVENT_QUERY = @"SELECT * FROM
__InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA'Win32_Process'";
static readonly string WMI_OPER_EVENT_QUERY_WITH_PROC =
WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'";
public ProcessWatcher()
{
Init(string.Empty);
}
public ProcessWatcher(string processName)
{
Init(processName);
}
private void Init(string processName)
{
this.Query.QueryLanguage = "WQL";
if (string.IsNullOrEmpty(processName))
{
this.Query.QueryString = WMI_OPER_EVENT_QUERY;
}
else
{
this.Query.QueryString =
string.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName);
}
this.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
}
private void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
string eventType = e.NewEvent.ClassPath.ClassName;
Win32_Process proc = new
Win32_Process(e.NewEvent["TargetInstance"] as ManagementBaseObject);
switch (eventType)
{
case "__InstanceCreationEvent":
if (ProcessCreated != null) ProcessCreated(proc); break;
case "__InstanceDeletionEvent":
if (ProcessDeleted != null) ProcessDeleted(proc); break;
case "__InstanceModificationEvent":
if (ProcessModified != null) ProcessModified(proc); break;
}
}
}
// Auto-Generated running: mgmtclassgen Win32_Process /n root\cimv2 /o WMI.Win32
// Renaming the class from Process to Win32_Process
public class Win32_Process { ... }
}
And here's the VB.Net conversion:
Imports System.ComponentModel
Imports System.Collections
Imports System.Globalization
Imports System.Management
Namespace WMI.Win32
Public Delegate Sub ProcessEventHandler(proc As Win32_Process)
Public Class ProcessWatcher
Inherits ManagementEventWatcher
' Process Events
Public Event ProcessCreated As ProcessEventHandler
Public Event ProcessDeleted As ProcessEventHandler
Public Event ProcessModified As ProcessEventHandler
' WMI WQL process query strings
Shared ReadOnly WMI_OPER_EVENT_QUERY As String = "SELECT * FROM " & vbCr & vbLf & "__InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
Shared ReadOnly WMI_OPER_EVENT_QUERY_WITH_PROC As String = WMI_OPER_EVENT_QUERY + " and TargetInstance.Name = '{0}'"
Public Sub New()
Init(String.Empty)
End Sub
Public Sub New(processName As String)
Init(processName)
End Sub
Private Sub Init(processName As String)
Me.Query.QueryLanguage = "WQL"
If String.IsNullOrEmpty(processName) Then
Me.Query.QueryString = WMI_OPER_EVENT_QUERY
Else
Me.Query.QueryString = String.Format(WMI_OPER_EVENT_QUERY_WITH_PROC, processName)
End If
Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)
End Sub
Private Sub watcher_EventArrived(sender As Object, e As EventArrivedEventArgs)
Dim eventType As String = e.NewEvent.ClassPath.ClassName
Dim proc As New Win32_Process(TryCast(e.NewEvent("TargetInstance"), ManagementBaseObject))
Select Case eventType
Case "__InstanceCreationEvent"
RaiseEvent ProcessCreated(proc)
Exit Select
Case "__InstanceDeletionEvent"
RaiseEvent ProcessDeleted(proc)
Exit Select
Case "__InstanceModificationEvent"
RaiseEvent ProcessModified(proc)
Exit Select
End Select
End Sub
End Class
End Namespace
I'm having problems with this line of code:
Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)
I can't seem to figure out how to convert it correctly, I appreciate any help!
Upvotes: 2
Views: 323
Reputation: 29746
This line:
Me.EventArrived += New EventArrivedEventHandler(AddressOf watcher_EventArrived)
should be:
AddHandler Me.EventArrived , AddressOf Me.watcher_EventArrived
Upvotes: 5