ObjRegisterActive

Post your working scripts, libraries and tools
lexikos
Posts: 6608
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

27 Oct 2017, 20:22

Your object would need to implement the IConnectionPointContainer and IConnectionPoint interfaces. In order to do that, you would need to construct the object yourself, including an IUnknown interface implementation which can be queried for those interfaces. You could not simply pass an AutoHotkey object to the client.
User avatar
nnnik
Posts: 4253
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: ObjRegisterActive

27 Jan 2018, 18:06

It seems I never said anything.
I'm very greatful to you for this function. It has been used in several projects of mine and really makes coding more powerful.
Recommends AHK Studio
A_User
Posts: 36
Joined: 21 Aug 2017, 01:15

Re: ObjRegisterActive

27 Mar 2018, 14:33

(Sorry for the previous post which I deleted as it had no useful information)

It seems the code below causes memory leaks. I'm guessing ObjRegisterActive() or ComObjActive() is something to do with it.

Main.ahk

Code: Select all

#SingleInstance, Force

loop {
    testMemoryLeak()
}
Return

~Esc::ExitApp

testMemoryLeak() {
    
    _sGUID         := CreateGUID()
    _o := new TestObjRegisterActive
    ObjRegisterActive( _o, _sGUID )

    _sPath         := A_ScriptDir "\Remote.ahk"
    Run, "%A_AhkPath%" "%_sPath%" %_sGUID%,, UseErrorLevel, _iPID    
    
    ; Wait for the remote script to be activated
    while ! _o.oRemote {
        sleep 10
    }
    
    ; ( doing something in an actual script )
    
    ; Done with the remote script
    _o.oRemote.exit()
    
    ObjRegisterActive( _o, "" )
    return _iPID
    
}

class TestObjRegisterActive {
    
    oRemote := ""
    
    __New() {
        this.TickCount := A_TickCount
    }
    
    activate( sRemoteGUID ) {    
        this.oRemote := ComObjActive( sRemoteGUID )
        tooltip % "(From Main) Actiavted: " sRemoteGUID
    }
        
}
Remote.ahk

Code: Select all

#Persistent 
#SingleInstance, Off

_sMainGUID       := %0% ; script argument

_oMain           := ComObjActive( _sMainGUID )

_oRemote         := new Remote
_sRemoteGUID     := CreateGUID()
ObjRegisterActive( _oRemote, _sRemoteGUID )

_oMain.activate( _sRemoteGUID )

ObjRegisterActive( _oRemote, "" )

tooltip % "From Remote: " _oMain.TickCount

Return
~Esc::ExitApp

class Remote {
    
    exit() {
        _func := ObjBindMethod( this, "_exitApp" )
        SetTimer, % _func, -1
    }
        _exitApp() {
            ExitApp
        }
    
}
Tested with: AutoHotkey v1.1.28.00, Windows 7 64bit
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: ObjRegisterActive

28 Mar 2018, 03:52

lexicos wrote:
min wrote:Does using ObjRegisterActive mean that now we can write AHK apps which can be controlled by COM by other programs written in other languages?
Yes. It's easy to do from VBScript. However, it seems GetObject() requires a ProgID rather than a CLSID, so you need to register it in the registry.

Code: Select all

' First set HKCR\testy\CLSID (default value) to the GUID
set x = GetObject(,"testy")
x.Message("Hello, world!")

Apparrently according to this site:

http://web.archive.org/web/200210011332 ... /index.asp

You can also use:

Code: Select all

Set o = GetObject(, "clsid:5c172d3c-c8bf-47b0-80a4-a455420a6911")
A full example can be found here

Code: Select all

Sub CreateGorillaAndEatBanana()
  Dim gc as IApeClass
  Dim ape as IApe
  Dim sz as String
  sz = "clsid:571F1680-CC83-11d0-8C48-0080C73925BA:"
' get the class object for gorillas
  Set gc = GetObject(sz)
' ask gorilla class object to create a new gorilla
  Set ape = gc.CreateApe()
' ask gorilla to eat a banana
  ape.EatBanana
End Sub
I've had no luck with either using CLSID or PROGID sadly (in vba), I just receive "Runtime error 429 - ActiveX component can't create object", which seems a bit odd, but perhaps it's due to IT policy or something **shrug**

Return to “Scripts and Functions”

Who is online

Users browsing this forum: DataLife, JoeWinograd and 52 guests