Page 3 of 3

Re: ObjRegisterActive

Posted: 27 Oct 2017, 20:22
by lexikos
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.

Re: ObjRegisterActive

Posted: 27 Jan 2018, 18:06
by nnnik
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.

Re: ObjRegisterActive

Posted: 27 Mar 2018, 14:33
by A_User
(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

Re: ObjRegisterActive

Posted: 28 Mar 2018, 03:52
by sancarn
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**