ComObjConnect with function inside of class

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

ComObjConnect with function inside of class

Post by pv007 » 22 Jan 2022, 00:14

Is possible to use ComObjConnect with a function thats inside of a class?

Tried to reply this post from Lexikos:
viewtopic.php?t=18348

But i didn't understand how to write it:

Code: Select all

Class MyClass {

   ProcessCreate_OnObjectReady(obj){
      ; https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN

      proc        := obj.TargetInstance

      Id          := proc.ProcessId
      Parent      := proc.ParentProcessID
      Name        := proc.Name
      Path        := proc.ExecutablePath
      CommandLine := proc.CommandLine

      FileAppend, ===========`n,*
      FileAppend,   CREATED `n,*
      FileAppend, ===========`n,*
      FileAppend, Name: %Name%`nPid: %Id% Parent: %Parent%`n,*
      FileAppend, Path: %Path%`n,*
      FileAppend, CommandLine: %CommandLine%`n`n,*

      /*
      TrayTip New Process Detected, % "
      (LTrim
         ID:`t" proc.ProcessID "
         Parent:`t" proc.ParentProcessID "
         Name:`t" proc.Name "
         Path:`t" proc.ExecutablePath "
         
         Command line (requires XP or later):
         
         " proc.CommandLine
      )
      */
   }

}


ProcessWatcher() {

   ; Get WMI service object.
   winmgmts := ComObjGet("winmgmts:")

   ; Create sink objects for receiving event noficiations.
   ;ComObjConnect(createSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessCreate_")

   (New MyClass).ProcessCreate_(createSink := ComObjCreate("WbemScripting.SWbemSink"))
   ComObjConnect(deleteSink := ComObjCreate("WbemScripting.SWbemSink"), "ProcessDelete_")

   interval := 2

   ; Register for process creation notifications:
   winmgmts.ExecNotificationQueryAsync(createSink
      , "Select * from __InstanceCreationEvent"
      . " within " interval
      . " where TargetInstance isa 'Win32_Process'")

 }




teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: ComObjConnect with function inside of class

Post by teadrinker » 22 Jan 2022, 04:04

An example:

Code: Select all

#Persistent
PM := new ProcessMonitoring(Func("OnProcessCreate"), Func("OnProcessClose"))

OnProcessCreate(name, PID, cmd) {
   TrayTip, Created, % name "`n" PID "`n" cmd
}

OnProcessClose(name, PID, cmd) {
   TrayTip, Closed, % name "`n" PID "`n" cmd
}

class ProcessMonitoring
{
   __New(UserFuncOnCreate := "", UserFuncOnDelete := "", singleProcess := "") {
      command := "Within .5 Where TargetInstance ISA 'Win32_Process'"
               . (singleProcess ? " And TargetInstance.Name = '" . singleProcess . "'" : "")
      this.WMI := ComObjGet("winmgmts:")
      if UserFuncOnCreate {
         this.createSink := ComObjCreate("WbemScripting.SWbemSink")
         ComObjConnect( this.createSink, new this.EventSink(UserFuncOnCreate) )
         this.WMI.ExecNotificationQueryAsync(this.createSink, "select * from __InstanceCreationEvent " . command)
      }
      if UserFuncOnDelete {
         this.deleteSink := ComObjCreate("WbemScripting.SWbemSink")
         ComObjConnect( this.deleteSink, new this.EventSink(UserFuncOnDelete) )
         this.WMI.ExecNotificationQueryAsync(this.deleteSink, "select * from __InstanceDeletionEvent " . command)
      }
   }
   
   class EventSink
   {
      __New(UserFunc) {
         this.UserFunc := UserFunc
      }
      
      OnObjectReady(params*) {
         Process := params[1].TargetInstance
         name := Process.Name
         PID := Process.ProcessID
         cmd := Process.CommandLine
         timer := this.UserFunc.Bind(name, PID, cmd)
         SetTimer, % timer, -10
      }
   }
}

Post Reply

Return to “Ask for Help (v1)”