Detect route table change
I have written some VBScript (.vbs) to detect routing table changes on the local computer that I want to use in a VB6 exe project.
The VBScript code is below and I have confirmed that it is working:
VB Code:
strComputer = "."
Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
Wscript.Echo "Waiting for routing table change..."
Do
WScript.Sleep 1000
Loop
Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
WScript.Echo "Routing table changed"
End Sub
Now this is the VB6 code below. Everything from the VBScript has just been moved into the form code. The program never reacts to a routing table change and the msgbox is never displayed. I cannot figure out why this is not working. Would appreciate any help. Thanks
VB Code:
Option Explicit
Dim SINK
Dim objWMIService
Private Sub Form_Load()
Dim strComputer
strComputer = "."
Set SINK = CreateObject("WbemScripting.SWbemSink")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
End Sub
Public Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
MsgBox ("Routing table changed")
End Sub
Re: Detect route table change
Add a reference to Microsoft WMI scripting v1.1 library and try this code.
VB Code:
Option Explicit
Private WithEvents SINK As WbemScripting.SWbemSink
Private objWMIService
Private Sub Form_Load()
Dim strComputer As String
strComputer = "."
Set SINK = CreateObject("WbemScripting.SWbemSink")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
End Sub
Private Sub SINK_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
MsgBox ("Routing table changed")
End Sub
Re: Detect route table change
Thanks Frans! Worked beautfiully! :bigyello:
For anyone that doesn't know how to add references to a VB project:
http://msdn.microsoft.com/library/de...l_interdev.asp