[v2-a101|a103] ComObjConnect not working

Report problems with documented functionality
User avatar
kczx3
Posts: 932
Joined: 06 Oct 2015, 21:39

[v2-a101|a103] ComObjConnect not working

28 Jun 2019, 11:09

ComObjConnect doesn't seem to be working for me with Excel in v2 (both a101 and a103). Uncommenting the MsgBox function allows running in v1 and the script works there.

Code: Select all

#Persistent

OnExit("beforeExit")

oxl := ComObjCreate("Excel.Application")
ComObjConnect(oxl, "oXL_")
oWorkbook := oxl.Workbooks.Add()

oxl.Visible := True
oxl.WindowState := xlMaximized := -4137
return

oXL_NewWorkbook(wb) {
    ; msgbox(Type(wb))
    msgbox("new workbook")
}

beforeExit() {
    Global
    
    oxl := ""
}

; uncomment for v1
; msgbox(text) {
    ; msgbox, % text
; }
User avatar
kczx3
Posts: 932
Joined: 06 Oct 2015, 21:39

Re: [v2-a101|a103] ComObjConnect not working

12 Sep 2019, 08:31

Curious if anyone has any thoughts on this. Is this commit supposed to address this?
Helgef
Posts: 4031
Joined: 17 Jul 2016, 01:02
Contact:

Re: [v2-a101|a103] ComObjConnect not working

12 Sep 2019, 09:02

Did you test with a104? If I recall correctly your example worked (perhaps with some modification) "between" 103 and 104 when I compiled ahk myself. That commit is not supposed to address this.
User avatar
kczx3
Posts: 932
Joined: 06 Oct 2015, 21:39

Re: [v2-a101|a103] ComObjConnect not working

12 Sep 2019, 09:56

Yeah I tried with 104. Still no go.

Scratch that. Adding a few asterisks to function definitions seems to have fixed it.

Code: Select all

#Persistent

OnExit("beforeExit")

oxl := ComObjCreate("Excel.Application")
ComObjConnect(oxl, "oXL_")
oWorkbook := oxl.Workbooks.Add()

oxl.Visible := True
oxl.WindowState := xlMaximized := -4137
return

oXL_NewWorkbook(wb, *) {
    ; msgbox(Type(wb))
    msgbox("new workbook")
}

beforeExit(*) {
    Global
    MsgBox("test")
    oxl := ""
}

; uncomment for v1
; msgbox(text) {
    ; msgbox, % text
; }
User avatar
kczx3
Posts: 932
Joined: 06 Oct 2015, 21:39

Re: [v2-a101|a103] ComObjConnect not working

12 Sep 2019, 10:21

I think the problem was that the call to oXL_NewWorkbook wasn't throwing an error about too few parameters defined like a normal function call.
lexikos
Posts: 6668
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [v2-a101|a103] ComObjConnect not working

21 Sep 2019, 03:45

When the event is raised, the COM server calls your event sink. AutoHotkey attempts to call your function, but instead throws an exception because there are too few parameters. Note that it throws an exception and does not display an error message.

Currently when an AutoHotkey object is called via IDispatch (even via our own COM event sink), any thrown exception is converted to a failure HRESULT. If the caller provides a non-null pExcepInfo (such as if the caller is the JScript engine), it is populated with the properties of the thrown exception and the result is DISP_E_EXCEPTION. Otherwise, the result is E_FAIL.

The COM event sink code currently always returns S_OK, since I had no clue what would (or should) happen if it returned failure. However, returning E_FAIL doesn't seem to make any difference for IE.NavigateComplete2 (I don't have Excel).

It would probably be more useful to show an error message for any uncaught (by AutoHotkey) exception, even though:
  • Returning a failure result might cause the caller to show an additional error message.
  • It might result in an inaccurate message: "The current thread will exit".
For event handlers, I have doubts about whether it is ever helpful to require all parameters to be defined. For COM event handlers, we add a parameter for the original COM object wrapper, since some events don't pass the object, and if the object is passed, it can't easily be used for comparison since it gets a new wrapper object.

If the restriction results in a general rule that "event handlers always have *", perhaps the drawbacks outweigh the benefits. It would be possible to permit surplus parameters for functions being called by COM or an "On" event, although it would make them inconsistent with script-based "event-raising" code.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 9 guests