Advertisement
omegastripes

mproc_letters.vbs

Aug 11th, 2013
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option explicit
  2. dim mproc
  3. set mproc = new multiprocess
  4. mproc.launch "base", "run", ""
  5.  
  6. ' main programm section
  7.  
  8. class base
  9.    
  10.     private index, letters
  11.    
  12.     private sub class_initialize()
  13.         letters = "abcdefghi jklmnopqr stuvwxyz "
  14.         index = 0
  15.     end sub
  16.    
  17.     public sub run()
  18.         dim i
  19.         host.startproc "outputwindow", "", "console"
  20.         console "Start"
  21.         host.startproc "packing", "run", ""
  22.         for i = 1 to len(letters)
  23.             host.createproc "typing", "run", ""
  24.         next
  25.         host.start("typing")
  26.         host.startproc "proctrace", "", "tracing"
  27.         msgbox "OK to terminate", vbinformation, "base"
  28.         host.free "typing"
  29.         host.joint "typing", 64, 0
  30.         host.free packing
  31.         host.joint packing, 64, 0
  32.         host.free tracing
  33.         host.joint tracing, 64, 0
  34.         host.free console
  35.         host.joint console, 64, 0
  36.         host.free host.id
  37.     end sub
  38.    
  39.     public sub typing_oninitialized(source)
  40.         index = index + 1
  41.         source.process.token = mid(letters, index, 1)
  42.         console "Loaded [" & source.process.token & "]"
  43.     end sub
  44.    
  45. end class
  46.  
  47. class packing
  48.    
  49.     private buffer, console
  50.    
  51.     private sub class_initialize()
  52.         buffer = ""
  53.         set console = scene.console
  54.     end sub
  55.    
  56.     public sub push(token)
  57.         buffer = buffer & token
  58.     end sub
  59.    
  60.     public sub run
  61.         dim pack, i, output
  62.         do until host.release
  63.             pack = split(buffer, " ")
  64.             if ubound(pack) >= 3 then
  65.                 output = ""
  66.                 for i = 0 to 2
  67.                     output = output & pack(i) & " "
  68.                 next
  69.                 buffer = right(buffer, len(buffer) - len(output))
  70.                 console  "[" & output & "]"
  71.             end if
  72.             wscript.sleep 100
  73.         loop
  74.     end sub
  75.    
  76. end class
  77.  
  78. class typing
  79.    
  80.     public token
  81.     private packing, console
  82.    
  83.     private sub class_initialize()
  84.         set packing = scene.packing
  85.         set console = scene.console
  86.     end sub
  87.    
  88.     public sub run
  89.         do until host.release
  90.             packing.push token
  91.             wscript.sleep 500
  92.         loop
  93.     end sub
  94.    
  95.     private sub class_terminate()
  96.         on error resume next
  97.         console "Terminated [" & token & "]"
  98.     end sub
  99.    
  100. end class
  101.  
  102. class proctrace
  103.    
  104.     private process_start_sink, process_stop_sink, console
  105.    
  106.     private sub class_initialize()
  107.         set console = scene.console
  108.         host.assignhandler "process_start_onobjectready", 2
  109.         host.assignhandler "process_stop_onobjectready", 2
  110.         set process_start_sink = wscript.createobject("WbemScripting.SWbemSink", "process_start_")
  111.         set process_stop_sink = wscript.createobject("WbemScripting.SWbemSink", "process_stop_")
  112.         with getobject("winmgmts:\\.\root\CIMV2")
  113.             .execnotificationqueryasync process_start_sink, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  114.             .execnotificationqueryasync process_stop_sink, "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'"
  115.         end with
  116.         console "Process tracing start"
  117.     end sub
  118.    
  119.     public sub process_start_onobjectready(receivedevent, asynccontext)
  120.         message receivedevent, "Started"
  121.     end sub
  122.    
  123.     public sub process_stop_onobjectready(receivedevent, asynccontext)
  124.         message receivedevent, "Stopped"
  125.     end sub
  126.    
  127.     private function message(receivedevent, action)
  128.         console now() & " " & action & " " & receivedevent.targetinstance.name & " (" & receivedevent.targetinstance.processid & "|" & receivedevent.targetinstance.parentprocessid & ")"
  129.     end function
  130.    
  131.     private sub class_terminate()
  132.         on error resume next
  133.         process_start_sink.cancel
  134.         process_stop_sink.cancel
  135.         console "Process tracing stop"
  136.     end sub
  137.    
  138. end class
  139.  
  140. class outputwindow
  141.    
  142.     private cscriptmode, ieapplication, iedocument, scenewscript, sweepout
  143.    
  144.     private sub class_initialize()
  145.         sweepout = false
  146.         cscriptmode = (instr(1, wscript.fullname, "cscript.exe") > 0)
  147.         if cscriptmode then
  148.             set scenewscript = scene.wscript
  149.         else
  150.             set ieapplication = wscript.createobject("internetexplorer.application", "ieobj_")
  151.             with ieapplication
  152.                 .menubar = false
  153.                 .toolbar = false
  154.                 .resizable = true
  155.                 .statusbar = false
  156.                 .addressbar = false
  157.                 .visible = true
  158.                 .navigate "about:blank"
  159.             end with
  160.             set iedocument = ieapplication.document
  161.             iedocument.write "<html><head><title>Output</title><style>*{font-family: 'courier new'; font-size: 10pt; color: #000; background-color: #FFF; }</style></head><body></body>"
  162.             host.assignhandler "ieobj_onquit", 0
  163.         end if
  164.     end sub
  165.    
  166.     public sub ieobj_onquit
  167.         if not sweepout then host.free 0
  168.     end sub
  169.    
  170.     public default sub writeln(text)
  171.         if cscriptmode then
  172.             scenewscript.echo text
  173.         else
  174.             on error resume next
  175.             iedocument.write text & "<br>"
  176.             err.clear
  177.         end if
  178.     end sub
  179.    
  180.     private sub class_terminate()
  181.         sweepout = true
  182.         on error resume next
  183.         do while typename(iedocument) = "HTMLDocument"
  184.             ieapplication.quit
  185.             wscript.sleep 1
  186.         loop
  187.     end sub
  188.    
  189. end class
  190.  
  191. ' do not modify service class section
  192.  
  193. class multiprocess
  194.    
  195.     public primary, ancestor, parent, process, err
  196.     public names, execs, hosts
  197.     public id, aid, isprimary
  198.     public classname, methodname, aliasname
  199.     public found, lost, active
  200.     public state, permit, release
  201.     private container, signature, wshshell
  202.    
  203.     public sub launch(startclassname, startmethodname, startaliasname)
  204.         permit = false
  205.         release = false
  206.         executeglobal "dim scene, host, ancestor, process"
  207.         if not isempty(host) then exit sub
  208.         set host = me
  209.         executeglobal "set host.err = err"
  210.         executeglobal "function getroot: set getroot = me: end function"
  211.         set parent = getroot
  212.         isprimary = not wscript.arguments.named.exists("task")
  213.         if isprimary then
  214.             dim sample
  215.             state = 24
  216.             randomize
  217.             signature = ""
  218.             do
  219.                 signature = signature & hex(rnd * 16)
  220.             loop while len(signature) < 16
  221.             aid = empty
  222.             id = 0
  223.             found = 0
  224.             lost = 0
  225.             set wshshell = createobject("WScript.Shell")
  226.             set primary = host
  227.             set ancestor = nothing
  228.             set process = nothing
  229.             set scene = parent
  230.             set parent.ancestor = nothing
  231.             set parent.process = nothing
  232.             set hosts = createobject("Scripting.Dictionary")
  233.             set execs = createobject("Scripting.Dictionary")
  234.             set names = createobject("Scripting.Dictionary")
  235.             classname = empty
  236.             methodname = empty
  237.             aliasname = empty
  238.             set hosts(0) = host
  239.             set container = getobject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
  240.             container.putproperty signature, parent
  241.             startproc startclassname, startmethodname, startaliasname
  242.             on error resume next
  243.             do
  244.                 for each sample in execs.keys
  245.                     if release or active = 0 then exit do
  246.                     if not (execs(sample) is nothing) then
  247.                         if execs(sample).status > 0 then
  248.                             abolish sample
  249.                         end if
  250.                     end if
  251.                     wscript.sleep 1
  252.                 next
  253.             loop
  254.             release = true
  255.             state = 28
  256.             scenequit
  257.         else
  258.             dim job
  259.             job = split(wscript.arguments.named("task"), ";")
  260.             signature = cstr(job(0))
  261.             do
  262.                 for each container in createobject("Shell.Application").windows
  263.                     if isobject(container.getproperty(signature)) then
  264.                         exit do
  265.                     end if
  266.                 next
  267.                 wscript.sleep 1
  268.             loop
  269.             aid = clng(job(1))
  270.             id = clng(job(2))
  271.             found = null
  272.             lost = null
  273.             set scene = container.getproperty(signature)
  274.             set primary = scene.host
  275.             set hosts = primary.hosts
  276.             set ancestor = hosts(aid)
  277.             if isresponsive(aid) then
  278.                 set parent.ancestor = ancestor.parent.process
  279.             else
  280.                 set parent.ancestor = nothing
  281.             end if
  282.             classname = cstr(job(3))
  283.             methodname = cstr(job(4))
  284.             aliasname = cstr(job(5))
  285.             state = 4
  286.             primary.implicate id, aliasname, host
  287.             executeglobal "set process = new " & classname
  288.             executeglobal "set host.process = process"
  289.             executeglobal "set scene." & aliasname & " = process"
  290.             if isresponsive(aid) then
  291.                 executeglobal "set host.ancestor.parent." & aliasname & " = process"
  292.             end if
  293.             state = 8
  294.             primary.staff host
  295.             ancestorevent "oninitialized"
  296.             state = 12
  297.             if methodname <> "" then
  298.                 do until permit
  299.                     wscript.sleep 10
  300.                 loop
  301.                 state = 16
  302.                 executeglobal "process." & methodname
  303.             end if
  304.             state = 20
  305.             ancestorevent "oncompleted"
  306.             state = 24
  307.             do until release
  308.                 wscript.sleep 10
  309.             loop
  310.             state = 28
  311.         end if
  312.     end sub
  313.    
  314.     public default function startproc(classname, methodname, aliasname)
  315.         set startproc = start(createproc(classname, methodname, aliasname))
  316.     end function
  317.    
  318.     public function createproc(classname, methodname, aliasname)
  319.         if aliasname = "" then aliasname = classname
  320.         newvar aliasname
  321.         scene.host.newvar aliasname
  322.         createproc = primary.spawn(id, classname, methodname, aliasname)
  323.     end function
  324.    
  325.     public function spawn(issuer, classname, methodname, aliasname)
  326.         if not release then
  327.             found = found + 1
  328.             spawn = found
  329.             active = found - lost
  330.             names(spawn) = aliasname
  331.             set hosts(spawn) = nothing
  332.             if not hosts.exists(aliasname) then
  333.                 hosts.add aliasname, createobject("Scripting.Dictionary")
  334.             end if
  335.             set hosts(aliasname)(spawn) = nothing
  336.             execs.add spawn, wshshell.exec("""" & wscript.fullname & """ """ & wscript.scriptfullname & """ ""/task:" & join(array(signature, issuer, spawn, classname, methodname, aliasname), ";") & """")
  337.         end if
  338.     end function
  339.    
  340.     public function start(target)
  341.         select case outline(target)
  342.         case "Nothing", "multiprocess"
  343.             do while getstate(target) < 12
  344.                 wscript.sleep 10
  345.             loop
  346.             if isresponsive(target) then
  347.                 set start = hosts(target).process
  348.                 hosts(target).permit = true
  349.             else
  350.                 set start = nothing
  351.             end if
  352.         case "Dictionary"
  353.             dim elt
  354.             set start = hosts(target)
  355.             for each elt in start.keys
  356.                 do while getstate(elt) < 12
  357.                     wscript.sleep 10
  358.                 loop
  359.             next
  360.             for each elt in start.keys
  361.                 if isresponsive(elt) then
  362.                     hosts(elt).permit = true
  363.                 end if
  364.             next
  365.         case else
  366.             set start = nothing
  367.         end select
  368.     end function
  369.    
  370.     public sub implicate(id, aliasname, host)
  371.         set hosts(aliasname)(id) = host
  372.         set hosts(id) = host
  373.     end sub
  374.    
  375.     public sub staff(host)
  376.         set hosts(host.process) = host
  377.     end sub
  378.    
  379.     public sub abolish(id)
  380.         if hosts.exists(names(id)) then
  381.             hosts(names(id))(id) = empty
  382.         end if
  383.         names(id) = empty
  384.         if isresponsive(id) then
  385.             hosts(hosts(id).process) = empty
  386.         end if
  387.         hosts(id) = empty
  388.         set execs(id) = nothing
  389.         lost = lost + 1
  390.         active = found - lost
  391.     end sub
  392.    
  393.     private sub ancestorevent(eventname)
  394.         if aid > 0 then
  395.             on error resume next
  396.             executeglobal "ancestor." & aliasname & "_" & eventname & " host.hosts(" & id & ")"
  397.             if err.number = 424 or err.number = 438 then err.clear
  398.         end if
  399.     end sub
  400.    
  401.     public sub assignhandler(handlername, byval varsqty)
  402.         dim vars
  403.         vars = ""
  404.         if varsqty > 0 then
  405.             do
  406.                 vars = vars & "param" & varsqty
  407.                 varsqty = varsqty - 1
  408.                 if varsqty = 0 then exit do
  409.                 vars = vars & ", "
  410.             loop
  411.         end if
  412.         executeglobal "sub " & handlername & "(" & vars & "): process." & handlername & " " & vars & ": end sub"
  413.     end sub
  414.    
  415.     public sub newvar(varname)
  416.         executecommand "dim " & varname
  417.     end sub
  418.    
  419.     public sub executecommand(command)
  420.         executeglobal command
  421.     end sub
  422.    
  423.     public function getstate(target)
  424.         select case outline(target)
  425.         case "multiprocess"
  426.             on error resume next
  427.             getstate = hosts(target).state
  428.             if err.number <> 0 then
  429.                 err.clear
  430.                 getstate = 64
  431.             end if
  432.         case "Nothing"
  433.             getstate = 1
  434.         case "Dictionary"
  435.             getstate = null
  436.         case empty
  437.             getstate = 0
  438.         case else
  439.             getstate = 64
  440.         end select
  441.     end function
  442.    
  443.     private function outline(target)
  444.         on error resume next
  445.         if hosts.exists(target) then
  446.             outline = typename(hosts(target))
  447.             if err.number <> 0 then
  448.                 err.clear
  449.                 outline = "Object"
  450.             end if
  451.         else
  452.             outline = empty
  453.         end if
  454.     end function
  455.    
  456.     public function isresponsive(target)
  457.         isresponsive = cbool(getstate(target) and 28)
  458.     end function
  459.    
  460.     public function getid(target)
  461.         on error resume next
  462.         if isobject(target) then
  463.             if isresponsive(target) then
  464.                 getid = hosts(target).id
  465.                 if err.number = 0 then exit function
  466.                 err.clear
  467.             end if
  468.         elseif primary.execs.exists(target) then
  469.             getid = target
  470.             exit function
  471.         end if
  472.         getid = null
  473.     end function
  474.    
  475.     public function gethost(target)
  476.         on error resume next
  477.         if hosts.exists(target) then
  478.             set gethost = hosts(target)
  479.             if err.number = 0 then exit function
  480.             err.clear
  481.         end if
  482.         set gethost = nothing
  483.     end function
  484.    
  485.     public sub free(target)
  486.         select case outline(target)
  487.         case "multiprocess"
  488.             on error resume next
  489.             gethost(target).release = true
  490.             err.clear
  491.         case "Dictionary"
  492.             dim elt
  493.             for each elt in gethost(target)
  494.                 free(elt)
  495.             next
  496.         end select
  497.     end sub
  498.    
  499.     public function joint(target, state, timeout)
  500.         dim reftime
  501.         reftime = timer
  502.         select case outline(target)
  503.         case "multiprocess", "Nothing"
  504.             do while getstate(target) < state
  505.                 if timeisout(timeout, reftime) then
  506.                     joint = false
  507.                     exit function
  508.                 end if
  509.                 wscript.sleep 10
  510.             loop
  511.         case "Dictionary"
  512.             dim elt
  513.             for each elt in gethost(target)
  514.                 do while getstate(elt) < state
  515.                     if timeisout(timeout, reftime) then
  516.                         joint = false
  517.                         exit function
  518.                     end if
  519.                     wscript.sleep 10
  520.                 loop
  521.             next
  522.         end select
  523.         joint = true
  524.     end function
  525.    
  526.     private function timeisout(timeout, reftime)
  527.         if timeout > 0 then
  528.             dim delta
  529.             delta = timer - reftime
  530.             if delta < 0 then delta = delta + 86400
  531.             if delta > timeout then
  532.                 timeisout = true
  533.             end if
  534.         else
  535.             timeisout = false
  536.         end if
  537.     end function
  538.    
  539.     public sub interrupt(target, timeout)
  540.         select case outline(target)
  541.         case "multiprocess"
  542.             on error resume next
  543.             with gethost(target).parent
  544.                 .wscript.timeout = timeout
  545.                 .wscript.quit
  546.             end with
  547.             err.clear
  548.         case "Dictionary"
  549.             dim elt
  550.             for each elt in gethost(target)
  551.                 interrupt elt, timeout
  552.             next
  553.         end select
  554.     end sub
  555.    
  556.     public sub kickout(target)
  557.         if primary.execs.exists(target) then
  558.             if getstate(target) < 64 then
  559.                 on error resume next
  560.                 primary.execs(target).terminate
  561.                 err.clear
  562.             end if
  563.         else
  564.             select case outline(target)
  565.             case "multiprocess"
  566.                 kickout getid(target)
  567.             case "Dictionary"
  568.                 dim elt
  569.                 for each elt in gethost(target)
  570.                     kickout(elt)
  571.                 next
  572.             end select
  573.         end if
  574.     end sub
  575.    
  576.     public sub terminate(target)
  577.         interrupt target, 1
  578.         if not joint(target, 64, 2) then kickout target
  579.     end sub
  580.    
  581.     public sub push(name, value)
  582.         container.putproperty name, value
  583.     end sub
  584.    
  585.     public function pop(name)
  586.         on error resume next
  587.         if isobject(container.getproperty(name)) then
  588.             set pop = container.getproperty(name)
  589.         else
  590.             pop = container.getproperty(name)
  591.         end if
  592.     end function
  593.    
  594.     private sub scenequit
  595.         if isprimary then
  596.             dim col, i, status
  597.             col = execs.keys
  598.             for i = ubound(col) to 0 step -1
  599.                 interrupt col(i), 1
  600.             next
  601.             wscript.sleep 2000
  602.             on error resume next
  603.             for i = ubound(col) to 0 step -1
  604.                 status = execs(col(i)).status
  605.                 if err.number = 0 and status = 0 then execs(col(i)).terminate
  606.                 err.clear
  607.             next
  608.             container.quit
  609.             createobject("wscript.shell").popup "scenequit", 1, "primary", 64
  610.         end if
  611.     end sub
  612.    
  613.     private sub class_terminate()
  614.         if state < 28 and isprimary then scenequit
  615.     end sub
  616.    
  617. end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement