Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdCOM - RegisterAsActiveObject() and OnTimeCall() #50

Open
sancarn opened this issue Jan 5, 2022 · 1 comment
Open

stdCOM - RegisterAsActiveObject() and OnTimeCall() #50

sancarn opened this issue Jan 5, 2022 · 1 comment
Labels
enhancement New feature or request lib-stdCOM

Comments

@sancarn
Copy link
Owner

sancarn commented Jan 5, 2022

Implementation of 2 methods ensures something that something which previously was not possible now is, although currently not optimally, more about that towards the end.

RegisterAsActiveObject

Description

Put an active object into the Running Object Table, and register it to a specified GUID (CLSID). From here other applications can use (and drive) your VBA application.

Example

The following example registers the myObj object as GUID e3cc5a52-6f89-41e3-9714-9502146a840a. This object can then be accessed from other processes via VBA's equivalent of GetObject("{e3cc5a52-6f89-41e3-9714-9502146a840a}").

Module Test
  Dim myObj as MyClass
  sub run()
    set myObj = new MyClass
    stdCOM.Create(myObj).RegisterAsActiveObject("e3cc5a52-6f89-41e3-9714-9502146a840a")
  end sub
end class

OnTimeCall(sMethodName,iMilliseconds)

Implementation Option 1 (Short term)

  1. Call RegisterAsActiveObject on a newly created GUID.
  2. Launch a binary/PowerShell script which connects to the object (via GUID) and continually calls a method every iMilliseconds milliseconds. If the object disconnects, close powershell runtime.

Implementation Option 2 (Long Term)

  1. Create a thunk which calls a method on an object pointer every iMilliseconds milliseconds.
  2. Pass pointer into thunk
  3. Resolve any crashes that occur, e.g. when object is un-allocated.

Example

The following example registers the myObj object as GUID e3cc5a52-6f89-41e3-9714-9502146a840a. This object can then be accessed from other processes via VBA's equivalent of GetObject("{e3cc5a52-6f89-41e3-9714-9502146a840a}").

class MyClass
  Public Function OnTick()
    static i as long: i=i+1 'increase every 100ms
  End Function
end class
Module Test
  Dim myObj as MyClass
  sub run()
    set myObj = new MyClass
    stdCOM.Create(myObj).OnTimeCall("OnTick", 100)
  end sub
end class

Potential issues

Unsure but may have difficulty calling methods in Break Mode.

@sancarn sancarn added the enhancement New feature or request label Jan 5, 2022
@sancarn
Copy link
Owner Author

sancarn commented Jan 5, 2022

Alternative specification for OnTimeCall could be as follows:

class MyClass
  Public WithEvents com as stdCOM
  Private Sub com_OnTick()
    static i as long: i = i + 1
  End Sub
end class

class stdCOM
  Public Event OnTick
  Public Sub OnTimeConnect(ByVal ms as long)
    Dim sGUID as string: sGUID = RandomGUID()
    Call Me.RegisterAsActiveObject(sGUID)
    Call OnTimeActiveObjectConnect(sGUID,"protOnTick", ms)
  End Sub
  Public Sub protOnTick()
    RaiseEvent OnTick
    DoEvents
  End Sub
end class

Pros

  • OnTick method doesn't have to be private, this is syphoned off to another object.
  • protOnTick is a common naming convention for "protected" methods in stdVBA so fits better here.
  • Using events is cleaner.

Cons

?

@sancarn sancarn mentioned this issue Jan 21, 2022
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib-stdCOM
Projects
Status: HOLD
Development

No branches or pull requests

1 participant