Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Com Events - Using ByRef Parms


  • Please log in to reply
11 replies to this topic
jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Can ByRef Params be utilized in Com Events? I would assume the following NewWindow3 event handler would cancel the navigation, but it does not:

#Persistent
pwb := ComObjCreate("InternetExplorer.Application")
ComObjConnect(pwb, IE_Events)
pwb.Navigate("www.autohotkey.com")
pwb.visible := true

class IE_Events {
	DocumentComplete(pwb) {
		pwb.document.parentWindow.Open("http://www.autohotkey.com")
	}
	NewWindow3(ppDisp, Cancel, flags, Url, NewURL, pwb) {
		TrayTip, , NewWindow3 Event Fired
		[color=red]Cancel := true[/color]
	}
	OnQuit(pwb) {
		ExitApp
	}
}
... or am I just missing something?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
VT_BYREF is not supported natively. If you use ListVars, you should see something like this:
Local Variables for IE_Events.NewWindow3()
--------------------------------------------------
Cancel[Object]: 0x02401920 <= ComObject([color=red]0x400B[/color], [color=red]0xAB92A8[/color])
flags[1 of 3]: 4
NewURL[26 of 63]: http://www.autohotkey.com/
ppDisp[Object]: 0x024018E0 <= ComObject(0x4009, 0xAB92B8)
pwb[Object]: 0x024017F0 <= ComObject(0x0009, 0xA743C4)
this[Object]: 0x00142008
Url[26 of 63]: http://www.autohotkey.com/
ListVars shows that ComObjType(Cancel) is 0x400B and ComObjValue(Cancel) is 0xAB92A8. 0x400B is a combination of VT_BYREF (0x4000) and VT_BOOL (0xB), which means that 0xAB92A8 is the address of a VARIANT_BOOL value. This type is defined as:
[color=green]/* 0 == FALSE, -1 == TRUE */[/color]
typedef short VARIANT_BOOL;
You can store a "true" value like so:
NumPut(-1, ComObjValue(Cancel), "short")
For ByRef in/out parameters which are VT_DISPATCH (such as ppDisp), VT_UNKNOWN or VT_BSTR, before storing a new pointer at the address returned by ComObjValue(), you must free the previous contents (if non-NULL).

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Wow - thanks for the thorough answer :)

For ByRef in/out parameters which are VT_DISPATCH (such as ppDisp) ...

... thanks for reading my mind as well 8)

silly
  • Guests
  • Last active:
  • Joined: --
For those of us who aren't quite at mind reading ability, can you elaborate this last point:

Lexikos wrote:
For ByRef in/out parameters which are VT_DISPATCH (such as ppDisp) ...
... thanks for reading my mind as well 8)


I think this might be my exact problem.

I created a NewWindow3 event which returns a COM object of type 0x4009 (by ref).

Any property or function (such as ReadyState or Navigate) on that COM throws an error "Error: No valid COM object!"

I have protected mode off, and I've tried running the script as administrator.

I've spent quite some time looking on the google and in the forum without an explicit solution. Any help would be appreciated, I think I'm close.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Do you have any code to share? I think you're getting confused on what the NewWindow3 event args are.

silly
  • Guests
  • Last active:
  • Joined: --
Sure:

...
nextPage:=false
ie2:=""
while, !nextPage||ie2==""
{
	Sleep, 100
}
ListVars
ie2.Navigate("Google.com")
return

IE_NewWindow3(ppDisp,Cancel,dwFlags,bstrUrlContext,bstrUrl)
{
	global ie2:=ppDisp
	global nextPage:=true
}

I guess I can back my question up a little bit. I'm using a COM object for IE to access a webpage. I click on a link and that opens a new window. I want the COM object to be able to control that new window.

Thanks for the help and fast response.

silly
  • Guests
  • Last active:
  • Joined: --
bump?

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

... this event precedes the creation of a new window in the WebBrowser in response to a navigation targeted to a new window, or from script using the window.open method.
Source: NewWindow3 Event (InternetExplorer, WebBrowser)

In other words, the new window doesn't exist yet.

  • Guests
  • Last active:
  • Joined: --
I understand that the window doesn't *quite* exist yet, but my understanding was that the ppDisp pointer would then be assigned to the new window unless canceled or otherwise altered by the the event method, such as redirected, etc.

ppDisp
Object expression that, optionally, receives a new, hidden WebBrowser or InternetExplorer object with no URL loaded.


The application that processes this notification can respond in one of three ways:

Create a new, hidden, non-navigated WebBrowser object or InternetExplorer object that is returned in ppDisp. On return, the object that fired this event configures and navigates (including a BeforeNavigate2 event) the new object to the target location.
Cancel the navigation by setting Cancel to True.
Do nothing and do not set ppDisp to any value. This will cause the object that fired the event to create a new InternetExplorer object as a separate (non-hosted) process to handle the navigation.


if there is another way to do this, please let me know, but i'm pretty sure this is the intent of this method. list vars shows it as a com object by ref type. i came to this thread originally because there was some specific language related to the by ref that i thought i might not be doing correctly/don't know how to do.

thanks for the help.

silly
  • Guests
  • Last active:
  • Joined: --
also just noticed the extra pwb parameter in the previous example. what is that about?

from previous:

NewWindow3(ppDisp, Cancel, flags, Url, NewURL, pwb)


from microsoft docs:

Private Sub object_NewWindow3( _
ByRef ppDisp As Object, _
ByRef Cancel As Boolean, _
ByVal dwFlags As Long, _
ByVal bstrUrlContext As String, _
ByVal bstrUrl As String)



any help is appreciated. thanks.

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012

I'm trying to do the same thing, get a pointer to the new window, but can't figure out if this is possible using ppDisp. If it is could someone please post an example?


Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


steventaitinger
  • Members
  • 17 posts
  • Last active: May 02 2018 08:12 PM
  • Joined: 05 Feb 2013

I second this request for a way to get a pointer to the new window.