标题: [问题求助] VBS异步查询事件Wscript.Quit怎么不结束Wscript.exe进程?[已解决] [打印本页]
作者: czjt1234 时间: 2013-3-13 09:07 标题: VBS异步查询事件Wscript.Quit怎么不结束Wscript.exe进程?[已解决]
本帖最后由 czjt1234 于 2013-3-13 10:27 编辑
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objSWbemServices = objSWbemLocator.ConnectServer()
- Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
-
- Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet01.Add "Process.Name", "svchost.exe"
-
- Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet02.Add "Process.Name", "System"
-
- strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
- strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
- strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
-
- 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
-
- Do
- Wscript.Sleep 1000
- Msgbox "??"
- Loop
-
- Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
- Msgbox "Sink_OnObjectReady" & vbCrLf & _
- objSWbemObject.Handle & vbCrLf & _
- objSWbemNamedValueSet.Item("Process.Name").Value
- objSWbemSink.Cancel()
- Set objSWbemSink = Nothing
- Wscript.Quit
- Msgbox "?"
- End Sub
-
- Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
- Msgbox "Sink_OnCompleted" & vbCrLf & _
- "iHResult:" & iHResult & vbCrLf & _
- objSWbemNamedValueSet.Item("Process.Name").Value
- 'Msgbox IsObject(objSWbemLastError) '.GetObjectText_
- End Sub
复制代码
Wscript.Quit这句执行后,进程里的Wscript.exe怎么还有?
作者: czjt1234 时间: 2013-3-13 10:08
本帖最后由 czjt1234 于 2013-3-13 10:14 编辑
MSDN里的代码是这样的
- ' Create an object sink
- set oSink = WScript.CreateObject("wbemscripting.swbemsink","sink_")
- ' Connect to WMI and the cimv2 namespace, and obtain
- ' an SWbemServices object
- set oSvc = GetObject("winmgmts:root\cimv2")
-
- bdone = false
- ' Query for all Win32_Process objects
- osvc.ExecQueryAsync oSink, "SELECT Name FROM Win32_Process"
- ' Wait until all instances are returned.
- ' The bdone flag prevents the script from exiting until
- ' the sink.OnCompleted subroutine is executed when
- ' all the objects are returned.
- while not bdone
- wscript.sleep 1000
- wend
-
- ' The sink subroutine to handle the OnObjectReady
- ' event. This is called as each object returns.
- sub sink_OnObjectReady(oInst, octx)
- WScript.Echo "Got Instance: " & oInst.Name
- end sub
- ' The sink subroutine to handle the OnCompleted event.
- ' This is called when all the objects are returned.
- ' The oErr parameter obtains an SWbemLastError object,
- ' if available from the provider.
- sub sink_OnCompleted(HResult, oErr, oCtx)
- WScript.Echo "ExecQueryAsync completed"
- bdone = true
- end sub
复制代码
改了这样也不行,估计是个Bug
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objSWbemServices = objSWbemLocator.ConnectServer()
- Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
-
- Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet01.Add "ExecQueryAsync", "strWQL01"
-
- Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet02.Add "ExecQueryAsync", "strWQL02"
-
- strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
- strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
- strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
-
- 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
-
- bdone = True
- Do While bdone
- Wscript.Sleep 1000
- Loop
-
- Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
- Msgbox "Sink_OnObjectReady" & vbCrLf & _
- objSWbemObject.Handle & vbCrLf & _
- objSWbemNamedValueSet.Item("ExecQueryAsync").Value
- objSWbemSink.Cancel()
- Set objSWbemSink = Nothing
- bdone = False
- End Sub
-
- Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
- Msgbox "Sink_OnCompleted" & vbCrLf & _
- "iHResult:" & iHResult & vbCrLf & _
- objSWbemNamedValueSet.Item("Process.Name").Value
- 'Msgbox objSWbemLastError.GetObjectText_
- End Sub
复制代码
作者: czjt1234 时间: 2013-3-13 10:27
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objSWbemServices = objSWbemLocator.ConnectServer()
- Set objSWbemSink = Wscript.CreateObject("WbemScripting.SWbemSink", "Sink_")
-
- Set objSWbemNamedValueSet01 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet01.Add "ExecQueryAsync", "strWQL01"
-
- Set objSWbemNamedValueSet02 = CreateObject("WbemScripting.SWbemNamedValueSet")
- objSWbemNamedValueSet02.Add "ExecQueryAsync", "strWQL02"
-
- strWQL = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
- strWQL01 = "SELECT * FROM Win32_Process WHERE Name = 'svchost.exe'"
- strWQL02 = "SELECT * FROM Win32_Process WHERE Name = 'System'"
-
- 'objSWbemServices.ExecNotificationQueryAsync objSWbemSink, strWQL
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL01,,,,objSWbemNamedValueSet01
- objSWbemServices.ExecQueryAsync objSWbemSink, strWQL02,,,,objSWbemNamedValueSet02
-
- bdone = True
- Do While bdone
- Wscript.Sleep 1000
- Loop
-
- Sub Sink_OnObjectReady(objSWbemObject, objSWbemNamedValueSet)
- Msgbox "Sink_OnObjectReady" & vbCrLf & _
- objSWbemObject.Handle & vbCrLf & _
- objSWbemNamedValueSet.Item("ExecQueryAsync").Value
- 'objSWbemSink.Cancel()
- 'Set objSWbemSink = Nothing
- bdone = False
- End Sub
-
- Sub Sink_OnCompleted(iHResult, objSWbemLastError, objSWbemNamedValueSet)
- Msgbox "Sink_OnCompleted" & vbCrLf & _
- "iHResult:" & iHResult & vbCrLf & _
- objSWbemNamedValueSet.Item("ExecQueryAsync").Value
- 'Msgbox objSWbemLastError.GetObjectText_
- End Sub
复制代码
晕的,这2行取消就可以了,画蛇添足
'objSWbemSink.Cancel()
'Set objSWbemSink = Nothing
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |