Board logo

标题: [问题求助] VBS异步查询事件Wscript.Quit怎么不结束Wscript.exe进程?[已解决] [打印本页]

作者: czjt1234    时间: 2013-3-13 09:07     标题: VBS异步查询事件Wscript.Quit怎么不结束Wscript.exe进程?[已解决]

本帖最后由 czjt1234 于 2013-3-13 10:27 编辑
  1. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  2. Set objSWbemServices = objSWbemLocator.ConnectServer()
  3. Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
  4. Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
  5. objSWbemNamedValueSet01.Add "Process.Name", "svchost.exe"
  6. Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
  7. objSWbemNamedValueSet02.Add "Process.Name", "System"
  8. strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  9. strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
  10. strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
  11. 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
  12. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
  13. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
  14. Do
  15.     Wscript.Sleep 1000
  16.     Msgbox "??"
  17. Loop
  18. Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
  19.     Msgbox "Sink_OnObjectReady" & vbCrLf & _
  20.            objSWbemObject.Handle & vbCrLf & _
  21.            objSWbemNamedValueSet.Item("Process.Name").Value
  22.     objSWbemSink.Cancel()
  23.     Set objSWbemSink = Nothing
  24.     Wscript.Quit
  25.     Msgbox "?"
  26. End Sub  
  27. Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
  28.     Msgbox "Sink_OnCompleted" & vbCrLf & _
  29.            "iHResult:" & iHResult & vbCrLf & _
  30.            objSWbemNamedValueSet.Item("Process.Name").Value
  31.     'Msgbox IsObject(objSWbemLastError)    '.GetObjectText_
  32. End Sub
复制代码
Wscript.Quit这句执行后,进程里的Wscript.exe怎么还有?
作者: czjt1234    时间: 2013-3-13 10:08

本帖最后由 czjt1234 于 2013-3-13 10:14 编辑

MSDN里的代码是这样的
  1. ' Create an object sink
  2. set oSink = WScript.CreateObject("wbemscripting.swbemsink","sink_")
  3. ' Connect to WMI and the cimv2 namespace, and obtain
  4. ' an SWbemServices object
  5. set oSvc = GetObject("winmgmts:root\cimv2")
  6. bdone = false
  7. ' Query for all Win32_Process objects
  8. osvc.ExecQueryAsync oSink, "SELECT Name FROM Win32_Process"
  9. ' Wait until all instances are returned.
  10. ' The bdone flag prevents the script from exiting until
  11. ' the sink.OnCompleted subroutine is executed when
  12. ' all the objects are returned.
  13. while not bdone   
  14.     wscript.sleep 1000
  15. wend
  16. ' The sink subroutine to handle the OnObjectReady
  17. ' event. This is called as each object returns.
  18. sub sink_OnObjectReady(oInst, octx)
  19.     WScript.Echo "Got Instance: " & oInst.Name
  20. end sub
  21. ' The sink subroutine to handle the OnCompleted event.
  22. ' This is called when all the objects are returned.
  23. ' The oErr parameter obtains an SWbemLastError object,
  24. ' if available from the provider.
  25. sub sink_OnCompleted(HResult, oErr, oCtx)
  26.     WScript.Echo "ExecQueryAsync completed"
  27.     bdone = true
  28. end sub
复制代码


改了这样也不行,估计是个Bug

  1. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  2. Set objSWbemServices = objSWbemLocator.ConnectServer()
  3. Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
  4. Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
  5. objSWbemNamedValueSet01.Add "ExecQueryAsync", "strWQL01"
  6. Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
  7. objSWbemNamedValueSet02.Add "ExecQueryAsync", "strWQL02"
  8. strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  9. strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
  10. strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
  11. 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
  12. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
  13. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
  14. bdone = True
  15. Do While bdone
  16.     Wscript.Sleep 1000
  17. Loop
  18. Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
  19.     Msgbox "Sink_OnObjectReady" & vbCrLf & _
  20.            objSWbemObject.Handle & vbCrLf & _
  21.            objSWbemNamedValueSet.Item("ExecQueryAsync").Value
  22.     objSWbemSink.Cancel()
  23.     Set objSWbemSink = Nothing
  24.     bdone = False
  25. End Sub  
  26. Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
  27.     Msgbox "Sink_OnCompleted" & vbCrLf & _
  28.            "iHResult:" & iHResult & vbCrLf & _
  29.            objSWbemNamedValueSet.Item("Process.Name").Value
  30.     'Msgbox objSWbemLastError.GetObjectText_
  31. End Sub
复制代码

作者: czjt1234    时间: 2013-3-13 10:27

  1. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  2. Set objSWbemServices = objSWbemLocator.ConnectServer()
  3. Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
  4. Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
  5. objSWbemNamedValueSet01.Add "ExecQueryAsync", "strWQL01"
  6. Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
  7. objSWbemNamedValueSet02.Add "ExecQueryAsync", "strWQL02"
  8. strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  9. strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
  10. strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
  11. 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
  12. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
  13. objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
  14. bdone = True
  15. Do While bdone
  16.     Wscript.Sleep 1000
  17. Loop
  18. Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
  19.     Msgbox "Sink_OnObjectReady" & vbCrLf & _
  20.            objSWbemObject.Handle & vbCrLf & _
  21.            objSWbemNamedValueSet.Item("ExecQueryAsync").Value
  22.     'objSWbemSink.Cancel()
  23.     'Set objSWbemSink = Nothing
  24.     bdone = False
  25. End Sub  
  26. Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
  27.     Msgbox "Sink_OnCompleted" & vbCrLf & _
  28.            "iHResult:" & iHResult & vbCrLf & _
  29.            objSWbemNamedValueSet.Item("ExecQueryAsync").Value
  30.     'Msgbox objSWbemLastError.GetObjectText_
  31. End Sub
复制代码
晕的,这2行取消就可以了,画蛇添足
    'objSWbemSink.Cancel()
    'Set objSWbemSink = Nothing




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2