Help converting ObjRegisterActive to AHK V2 version Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Help converting ObjRegisterActive to AHK V2 version

Post by viv » 22 Mar 2023, 23:50

Original post : viewtopic.php?f=6&t=6148

V2 version access (V1 version) no problem
but cannot create
I carry on to this point can not continue
I saw that the last item in the reply was also a request for V2 version

Code: Select all

obj := ActiveObject()
ObjRegisterActive(obj, "{6B39CAA1-A320-4CB0-8DB4-352AA81E460E}")
Persistent
OnExit(Revoke)

ObjRegisterActive(Object, CLSID, Flags := 0) {
    static cookieJar := Map()
    if (!CLSID) {
        if !(cookie := cookieJar.Has(Object)) || (cookie := cookieJar.Delete(Object)) != ""
            DllCall("oleaut32\RevokeActiveObject", "uint", cookie, "ptr", 0)
        return
    }
    if cookieJar.Has(Object)
        throw Error("Object is already registered", -1)
    _clsid := Buffer(16, 0)
    if (hr := DllCall("ole32\CLSIDFromString", "wstr", CLSID, "ptr", _clsid)) < 0
        throw Error("Invalid CLSID", -1, CLSID)

    hr := DllCall("oleaut32\RegisterActiveObject", "ptr", &Object, "ptr", _clsid, "uint", Flags, "uint*", &cookie, "uint")
    if (hr < 0)
        throw Error(format("Error 0x{:x}", hr), -1)
    cookieJar[Object] := cookie
}

Revoke(A_ExitReason, ExitCode)
{
    ObjRegisterActive(obj, "")
    ExitApp()
}

class ActiveObject {
    Message(Data) {
        ToolTip("Received message: " Data)
        return 42
    }
}

ntepa
Posts: 428
Joined: 19 Oct 2022, 20:52

Re: Help converting ObjRegisterActive to AHK V2 version  Topic is solved

Post by ntepa » 23 Mar 2023, 04:28

try changing &Object to ObjPtr(Object)

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Help converting ObjRegisterActive to AHK V2 version

Post by viv » 23 Mar 2023, 04:51

@ntepa

Thank you. Now it is working properly

I know to pass its memory address
But I haven't seen the ObjPtr function...

I've tried Buffer and others
I also asked chatgpt and bing ai
They didn't give the right answer...

I just added some content based on the reported errors
such as cookie := 0 etc.
I don't understand what they do
I hope it doesn't affect the function
The current converted versions are as follows

Code: Select all

ObjRegisterActive(Object, CLSID, Flags := 0) {
    static cookieJar := Map()
    cookie := 0
    if (!CLSID) {
        if !(cookie := cookieJar.Has(Object)) || (cookie := cookieJar.Delete(Object)) != ""
            DllCall("oleaut32\RevokeActiveObject", "uint", cookie, "ptr", 0)
        return
    }
    if cookieJar.Has(Object)
        throw Error("Object is already registered", -1)
    _clsid := Buffer(16, 0)
    if (hr := DllCall("ole32\CLSIDFromString", "wstr", CLSID, "ptr", _clsid)) < 0
        throw Error("Invalid CLSID", -1, CLSID)

    hr := DllCall("oleaut32\RegisterActiveObject", "ptr", ObjPtr(Object), "ptr", _clsid, "uint", Flags, "uint*", &cookie, "uint")
    if (hr < 0)
        throw Error(format("Error 0x{:x}", hr), -1)
    cookieJar[Object] := cookie
}

viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Help converting ObjRegisterActive to AHK V2 version

Post by viv » 23 Mar 2023, 07:32

The problem with ObjRegisterActive at the moment is that admin privileges can't access the com registered by the normal privileges script
I'm not sure if it's inherently not allowed or if it requires a specific method

Post Reply

Return to “Ask for Help (v2)”