ObjRegisterActive

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

ObjRegisterActive

29 Jan 2015, 03:22

Code: [Select all] [Expand] [Download] (ObjRegisterActive.ahk)GeSHi © Codebox Plus

Known limitations:
  • If the script quits while running a method called by a remote script, the remote script will receive an error.
  • There are also limitations related to how AutoHotkey locally interacts with COM objects, and how AutoHotkey objects respond to requests via COM interfaces.
    • For-loops don't support remote objects.
    • Variable can't be passed ByRef to a remote object.
    • Some invocations are ambiguous; for example, foo.bar triggers foo.__Call and then foo.__Get.

To try, run both of the following files:

Code: [Select all] [Expand] [Download] (Host.ahk)GeSHi © Codebox Plus

Code: [Select all] [Expand] [Download] (Client.ahk)GeSHi © Codebox Plus



Related: GetActiveObjects()
User avatar
boiler
Posts: 1920
Joined: 21 Dec 2014, 02:44

Re: ObjRegisterActive

29 Jan 2015, 07:54

Great. Thanks for this.
User avatar
cyruz
Posts: 243
Joined: 30 Sep 2013, 13:31

Re: ObjRegisterActive

29 Jan 2015, 08:47

Nice. Aside from IPC can you elaborate on possible use cases?
Is your "Worker thread" example. comment a way to say that with this code we can multithread safely?
ABCza on the old forum.
My GitHub.
min

Re: ObjRegisterActive

29 Jan 2015, 09:00

Nice! Btw, Host.ahk gives error:

Code: [Select all] [Download] (Host.ahk)GeSHi © Codebox Plus

Error:  0x800706BA - The RPC server is unavailable.
Specifically: Complete
---> work.Complete(this)
User avatar
joedf
Posts: 5959
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: ObjRegisterActive

29 Jan 2015, 09:17

@lexikos so this is the multithreading you were talking about... :)
min

Re: ObjRegisterActive

29 Jan 2015, 09:27

Does using ObjRegisterActive mean that now we can write AHK apps which can be controlled by COM by other programs written in other languages?
e.g. some C++ app uses COM and calls a method or sets/gets property in our AHK script? If so --> that's awesome! :D
User avatar
fincs
Posts: 500
Joined: 30 Sep 2013, 14:17
GitHub: fincs
Location: Seville, Spain
Contact:

Re: ObjRegisterActive

29 Jan 2015, 10:57

Nice script and example. This is exactly the same technique SciTE4AutoHotkey uses to expose its COM object.
fincs
Windows 10 x64 Build 14393 / AutoHotkey v1.1.24.04
Get SciTE4AutoHotkey v3.0.06.01 - [My project list]
User avatar
Learning one
Posts: 119
Joined: 04 Oct 2013, 13:59
Location: Croatia
Contact:

Re: ObjRegisterActive

29 Jan 2015, 15:25

Lexikos, this is fantastic! 8-)
Thank you so much! :)
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

29 Jan 2015, 17:14

cyruz wrote:Aside from IPC can you elaborate on possible use cases?
IPC is effectively all that it is. It's just a question of what you use IPC for.
Is your "Worker thread" example. comment a way to say that with this code we can multithread safely?
Multiple processes means multiple threads, so yes. Any IPC method could be used to coordinate multiple processes. This just makes it a bit easier in some cases. For simple tasks you can just run another script with (or without) command line parameters.
min wrote:Host.ahk gives error:
I've updated the examples. I think there was a race condition between the host handling the WM_CLOSE message and the client exiting. The limitation here is that if the script exits while the remote end is waiting for a method to return, they get an error. In one script I used PostMessage to work around that, but in the other script I forgot and used ExitApp. A proper COM server might implement the IExternalConnection interface to detect when all external connections have been released, then exit. However, in the case of just passing objects back and forward in method calls, that wouldn't be very practical.
joedf wrote:@lexikos so this is the multithreading you were talking about... :)
I don't recall making any specific reference to this in regard to multi-threading, only IPC. This is just one of many alternatives to direct multi-threading.
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] [Download] GeSHi © Codebox Plus

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

fincs wrote:This is exactly the same technique SciTE4AutoHotkey uses to expose its COM object.
It differs in a number of ways. For instance, this function uses a strong reference by default and doesn't bother with CoLockObjectExternal. Where ComRemote closes connections and revokes the object automatically when the server end releases its "ComRemote" object, this function keeps the object alive and accessible until it is revoked. The function also accepts a normal object, not a ComObject wrapper. An object can be revoked without disconnecting clients, so for instance, a worker process could revoke its object when in use to allow the objects of other (idle) worker processes to be accessed.
Guest

Re: ObjRegisterActive

29 Jan 2015, 19:53

Lexikos,

I tried this on as simple object in script #1:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

AnObject := {one:1, two:2, three:3}
ObjRegisterActive(AnObject, "{6B39CAA1-A320-4CB0-8DB4-352AA81E460E}")

#Persistent


I then accessed it in script #2:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

x := ComObjActive("{6B39CAA1-A320-4CB0-8DB4-352AA81E460E}")
Msgbox % x.one . "`n" . x.two . "`n" . x.three
for key, value in x
Msgbox % key . "`n" . value


The first Msgbox works but the for loop complains it cannot find a member. It looks as if x cannot be accessed the same way as it can in the script that created it.

Relayer
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

29 Jan 2015, 21:21

Huh? ComRemote.ahk is not shorter, unless you're including comments. That's even without considering the dependency (Str2GUID) and that it has less error checking. ComInterface.ahk registers a ProgID and the CLSID in the registry; look for RegisterIDs().
Relayer wrote:The first Msgbox works but the for loop complains it cannot find a member.
I haven't tested yet but I presume it's because the ComObject wrapper (for the remote object) tries to use an IEnumVARIANT-based enumerator instead of just calling object._NewEnum(). You should be able to get around it by calling _NewEnum and enum.Next() directly instead of using a for-loop.
[Edit: Wrong. See below.]
GeekDude
Posts: 741
Joined: 02 Oct 2013, 22:13

Re: ObjRegisterActive

29 Jan 2015, 21:32

You're completely right, of course. Thanks for pointing out ComInterface.ahk, also. HKCU is the one you can write to without admin permissions, right?

How exactly does the inter-process calling of Func()tion references work? I would've expected some kind of RegisterCallback to be required. Was that part of "Added support for passing AutoHotkey objects to COM APIs as IDispatch."?
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

29 Jan 2015, 22:17

"Added support for passing AutoHotkey objects to COM APIs as IDispatch." is the whole reason that this works at all. Without it, we would have to implement our own IDispatch interface in script, as SciTE4AutoHotkey does. %fn%() calls fn[""](), which usually fails with COM objects but works okay with AutoHotkey objects via COM. However, work.Complete() isn't calling a Func reference; it's calling a method of the work object.
User avatar
maestrith
Posts: 684
Joined: 16 Oct 2013, 13:52

Re: ObjRegisterActive

30 Jan 2015, 05:00

Thank you so much lexikos! Studio will have this soon.
AHK Studio OSD GUI Creator
Donations
Discord
All code is done on a Windows 10 PC Running x64 and AutoHotkey x32
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

31 Jan 2015, 07:45

Guest wrote:The first Msgbox works but the for loop complains it cannot find a member.

There are actually two problems:

  1. "Member not found" comes from requesting the member DISPID_NEWENUM instead of attempting to resolve the name "_NewEnum". This works well with COM objects (since some don't actually resolve the name "_NewEnum"), but AutoHotkey objects don't respond to that ID.

  2. Even if you call _NewEnum() directly, you can't use an AutoHotkey enumerator object remotely because it expects variable references to put the items into. The for-loop passes a normal variable reference, not Variant reference (a ComObject with VT_BYREF|VT_VARIANT). You can pass a Variant reference, but the AutoHotkey object at the other end will just dereference it and pass a useless value to the enumerator.
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

31 Jan 2015, 21:01

I have posted another example in the form of a new function: LoadFile - Load script file as a separate process.

It demonstrates temporary registration of an object using a unique ID to prevent conflicts.
GeekDude
Posts: 741
Joined: 02 Oct 2013, 22:13

Re: ObjRegisterActive

31 Jan 2015, 21:45

I want so much to have a use for this. GeekBot comes very close, but he already has a socket based system that works great under WINE, allowing non-WINE programs such as php to interact.
min

Re: ObjRegisterActive

02 Feb 2015, 02:46

Lexikos, what are the terms of use for your ObjRegisterActive(), LoadFile(), LoadLib() ?
lexikos
Posts: 5417
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: ObjRegisterActive

02 Feb 2015, 03:25

WTFPL or equivalent.

If use is allowed unconditionally, does that mean there are no terms of use?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Hellbent, Randy31416 and 20 guests