COM Object from IDispatch Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

COM Object from IDispatch

24 Mar 2017, 11:17

Hi all,

I've got the IDispatch of an instance of a currently open application. I want to get the COM object which this IDispatch points towards. Through brief research I assume that I can do this with the following:

Code: Select all

;Set value of IDispatch which points to my application's COM object
IDispatch:=524175952

;Get the COM Object pointed at by IDispatch
MI:=ComObjEnwrap(IDispatch)

;Call method of COM Object, returning name of application.
msgbox, % MI.Name()
The above code doesn't work for this specific application. In fact the msgbox never appears and it appears that ComObjEnwrap() fails to retrieve the COM Object without error.

Am I doing something wrong? Or is this a problem I cannot solve?

Thanks,
~Sancarn
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: COM Object from IDispatch

24 Mar 2017, 11:50

Wrong section. This should be posted in "Ask for Help."
I've got the IDispatch of an instance of a currently open application.
Your terminology is confused. IDispatch is the term to describe a particular type of COM interface -> https://duckduckgo.com/?q=idispatch&t=ffsb&ia=web
IDispatch does not refer to a number. Where did you get "524175952", and what makes you think it points to a COM object?
What you probably have there is a pointer to an IDispatch interface. Getting a pointer to an IDispatch interface usually requires DLLCall...
Notice how ComObjEnwrap is not highlighted in the codebox above? That's because it is depricated. See https://autohotkey.com/docs/commands/ComObjActive.htm
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

24 Mar 2017, 12:00

Edit: Wooops! Didn't realize it was the wrong section... :facepalm: Can a mod move it please?

Ah really? Hmmm... I guess that makes sense though, given the 'I' prefix.

I got the number from a function call from the Application's API:
SystemInfo(17) -- Returns an integer, representing the IDispatch OLE Automation pointer for the MapInfo Application.

So indeed I probably do have the pointer to the IDispatch interface.


Indeed I did read that it had been deprecated, however I also tried:

Code: Select all

ComObject := ComObject(9, IDispatch, 1), ObjAddRef(IDispatch)
Which also fails in exactly the same way.

------------------------------------------------------------------

Ultimately, a better question would be:

Given that I have the pointer to the IDispatch interface, how can I access the COM Object?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: COM Object from IDispatch

24 Mar 2017, 12:22

So you did this? (Not hard-coding the pointer as above)

Code: Select all

DispPtr := SystemInfo(17)
ComObject := ComObject(9, DispPtr, 1)
ObjAddRef(DispPtr)
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

24 Mar 2017, 12:42

kon wrote:So you did this? (Not hard-coding the pointer as above)

Code: Select all

DispPtr := SystemInfo(17)
ComObject := ComObject(9, DispPtr, 1)
ObjAddRef(DispPtr)
No... Because SystemInfo isn't an AHK function...

Rather I'd do:

AHK:

Code: Select all

DispPtr = %1%
ComObject := ComObject(9, DispPtr, 1)
ObjAddRef(DispPtr)

MB:

Code: Select all

Declare Sub Main()

Sub Main()
    Run "C:\Application.exe " & systeminfo(17)
End Sub
Last edited by sancarn on 24 Mar 2017, 12:43, edited 1 time in total.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: COM Object from IDispatch

24 Mar 2017, 12:42

I'm not an expert, so I may have messed up the AddRef's... not sure, but this seems to work.
This gets a pointer and then wraps it again.

Code: Select all

objShell := ComObjCreate("Shell.Application")
MsgBox, % objShell.Windows.Item(0).LocationName  ; 1 explorer window needs to be open

; Just for fun, get a pointer
DispPtr := ComObjValue(objShell)
;ObjAddRef(DispPtr)
MsgBox, % DispPtr

; and wrap the pointer again...
x := ComObject(9, DispPtr, 1)
ObjAddRef(DispPtr)
DispPtr := ""
MsgBox, % x.Windows.Item(0).LocationName  ; 1 explorer window needs to be openr
return
Last edited by kon on 24 Mar 2017, 12:52, edited 1 time in total.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: COM Object from IDispatch  Topic is solved

24 Mar 2017, 12:44

sancarn wrote:No... Because SystemInfo isn't an AHK function...

Rather I'd do: [...]
Ah, ok. A pointer to a COM interface cannot be used in another process --> https://autohotkey.com/board/topic/7784 ... /?p=495011
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

24 Mar 2017, 12:46

kon wrote:I'm not an expert, so I may have messed up the AddRef's... not sure, but this seems to work.
This gets a pointer and then wraps it again.

Code: Select all

objShell := ComObjCreate("Shell.Application")
MsgBox, % objShell.Windows.Item(0).LocationName  ; 1 explorer window needs to be open

; Just for fun, get a pointer
DispPtr := ComObjValue(objShell)
ObjAddRef(DispPtr)
MsgBox, % DispPtr

; and wrap the pointer again...
x := ComObject(9, DispPtr, 1)
ObjAddRef(DispPtr)
DispPtr := ""
MsgBox, % x.Windows.Item(0).LocationName  ; 1 explorer window needs to be openr
return
I did a new experiment...

Code: Select all

MI := ComObjActive("Mapinfo.Application.x64")
msgbox, % "Ptr: " . ComObjValue(MI) . "; Type: " . ComObjType(MI) . "; Flags: " . ComObjFlags(MI)
Returned values:
---------------------------
COM_Via_IDispatch.ahk
---------------------------
Ptr: 7293400; Type: 9; Flags: 0
---------------------------
OK
---------------------------

So this pointer is completely different to the pointer returned by that function... O_o It looks like I will have to take this up with the Developers of the software...
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

24 Mar 2017, 13:00

kon wrote:
sancarn wrote:No... Because SystemInfo isn't an AHK function...

Rather I'd do: [...]
Ah, ok. A pointer to a COM interface cannot be used in another process --> https://autohotkey.com/board/topic/7784 ... /?p=495011
Well crackers... Oh wait... FML... I must have misread the documentation... It's really unfortunate ComObjActive only gets the first instance of the object on the ROT...
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: COM Object from IDispatch

24 Mar 2017, 13:05

sancarn wrote:It's really unfortunate ComObjActive only gets the first instance of the object on the ROT...
GetActiveObjects - Get multiple active COM objects :?:

Here's some examples using GetActiveObjects to get Excel workbooks:
https://autohotkey.com/boards/viewtopic ... 15#p116315
https://autohotkey.com/boards/viewtopic ... 76#p134876
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

24 Mar 2017, 13:39

kon wrote:
sancarn wrote:It's really unfortunate ComObjActive only gets the first instance of the object on the ROT...
GetActiveObjects - Get multiple active COM objects :?:

Here's some examples using GetActiveObjects to get Excel workbooks:
https://autohotkey.com/boards/viewtopic ... 15#p116315
https://autohotkey.com/boards/viewtopic ... 76#p134876
Waaaahhh?!?! :o ... Where has this been my entire life?! Jeez this is gong to help out so much... Thanks to yourself & Lexicos of course!
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: COM Object from IDispatch

24 Mar 2017, 22:25

kon wrote:Wrong section. This should be posted in "Ask for Help."
Thanks kon
sancarn wrote:Wooops! Didn't realize it was the wrong section... :facepalm: Can a mod move it please?
done :)
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

Re: COM Object from IDispatch

25 Mar 2017, 08:42

TLM wrote:
kon wrote:Wrong section. This should be posted in "Ask for Help."
Thanks kon
sancarn wrote:Wooops! Didn't realize it was the wrong section... :facepalm: Can a mod move it please?
done :)
Thanks TLM!

P.S. Whenever I read your name I think of "The Lemon"... :lol:
User avatar
lmstearn
Posts: 695
Joined: 11 Aug 2016, 02:32
Contact:

Re: COM Object from IDispatch

14 Aug 2019, 08:52

As an afterthought, the COM object is easy for retrieval with ComObjActive as long as it's registered with OLE. As a registrant alternative, some FoxPro dlls will also do the job.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Spawnova and 369 guests