Results 1 to 3 of 3

Thread: Detect route table change

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Question 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:
    1. strComputer = "."
    2.  
    3. Set SINK = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
    4. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    5. objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
    6.  
    7. Wscript.Echo "Waiting for routing table change..."
    8.  
    9. Do
    10.    WScript.Sleep 1000
    11. Loop
    12.  
    13.  
    14. Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
    15.  
    16.     WScript.Echo "Routing table changed"
    17.  
    18. 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:
    1. Option Explicit
    2. Dim SINK
    3. Dim objWMIService
    4.  
    5. Private Sub Form_Load()
    6.     Dim strComputer
    7.    
    8.     strComputer = "."
    9.    
    10.     Set SINK = CreateObject("WbemScripting.SWbemSink")
    11.     Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    12.     objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
    13. End Sub
    14.  
    15.  
    16. Public Sub SINK_OnObjectReady(objLatestEvent, objAsyncContext)
    17.     MsgBox ("Routing table changed")
    18. End Sub

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Detect route table change

    Add a reference to Microsoft WMI scripting v1.1 library and try this code.

    VB Code:
    1. Option Explicit
    2. Private WithEvents SINK As WbemScripting.SWbemSink
    3. Private objWMIService
    4.  
    5. Private Sub Form_Load()
    6.     Dim strComputer As String
    7.    
    8.    
    9.     strComputer = "."
    10.    
    11.     Set SINK = CreateObject("WbemScripting.SWbemSink")
    12.     Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    13.     objWMIService.ExecNotificationQueryAsync SINK, "SELECT * FROM Win32_IP4RouteTableEvent"
    14. End Sub
    15.  
    16. Private Sub SINK_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    17.     MsgBox ("Routing table changed")
    18. End Sub
    Frans

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: Detect route table change

    Thanks Frans! Worked beautfiully!
    For anyone that doesn't know how to add references to a VB project:

    http://msdn.microsoft.com/library/de...l_interdev.asp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width