Capturing a browser event in an ActiveX Browser Window
slloyd
created: 2004-06-15 00:09:07
Hi fellow monks, This one has me stumped. I want to use Win32::GUI::AxWindow to load an HTML page and then capture when a user clicks on a link in that page. My intention is to run a subroutine based on the link that is clicked. When I try to capture the BeforeNavigate2 event Perl blows up. I can capture other events fine. Here is the code. What am I doing wrong?
#!perl
use strict;
use Win32::GUI;
use Win32::GUI::AxWindow;
################ MAIN WINDOW #############
my $FormWindow = new Win32::GUI::Window(
    -name   => "FormWindow",
    -text   => "Test",
    -pos	=> [0,0],
    -size	=> [400,300],
	);
sub FormWindow_Resize{return 0;}
sub FormWindow_Minimize {return 0;}
sub FormWindow_Maximize {return 0;}
sub FormWindow_Terminate {return -1;}
##########################
# Add a WebBrowser AxtiveX
my $Control = new Win32::GUI::AxWindow  (
	-parent  => $FormWindow,
	-name    => "Control",
	-control => "Shell.Explorer.2",
	#-control => "InternetExplorer.Application",
	#-control => "{8856F961-340A-11D0-A96B-00C04FD705A2}",
	-pos     => [0, 0],
	-size    => [400, 300],
	);  
# # Register some event
#	When I try to register the following events, Perl blows up.
#		NavigateComplete2
#		BeforeNavigate2
#		NewWindow2
#	These events work though
#		DownloadBegin
#		DocumentComplete
#		StatusTextChange

$Control->RegisterEvent("StatusTextChange",
	sub {
		my $self    = shift;
		my $eventid = shift;
		my $str=shift;
		print "Event [$eventid]: ", $str, "\n" if length($str) && $str !~/done/is;
		} 
	);
print "Calling Navigate\n";
# Call Method
$Control->CallMethod("Navigate", 'http://www.google.com');
print "Done\n";
##########################
$FormWindow->Show();
Win32::GUI::Dialog();
exit;
Re: Capturing a browser event in an ActiveX Browser Window
created: 2004-06-15 01:03:53

This is one of those kinds of questions where the gut level reaction is "this person is not likely to get a satisfactory answer" ...

Hopefully, the gut is wrong, but on the off chance that a more direct and satisfying solution is not forthcoming, perhaps you can try an alternative approach. The alternative is to use 'perlscript' directly in a web browser page hosted in Internet Explorer. If you want it, here is a sample that does what you were asking about. Admittedly, it is a far cry from the (apparently a spider?) application that you are working on.



    insert_title_here
    




    foo link



Re^2: Capturing a browser event in an ActiveX Browser Window
created: 2004-06-15 05:23:22
How'll that help the OP to track where user navigates? It wont
Re: Capturing a browser event in an ActiveX Browser Window
created: 2004-06-15 05:08:41

The immediate problem is the the BeforeNavigate2 event is incorrectly handled by AxWindow with the net result of trying to deref a null pointer (memory at 0x00000000). This is a bug in AxWindow, however there are also known bugs in BeforeNavigate2 just to add fun. As you can see StatusTextChange is a simple method, BN2 is more complex and needs to be fed appropriately - it is not being fed what it needs. I suggest you contact the author or if you want to do ActiveX in Perl I understand ActiveState has some commercial (ie pay for) tools that probably work.

void BeforeNavigate2(          
    IDispatch *pDisp,
    VARIANT *&url,
    VARIANT *&Flags,
    VARIANT *&TargetFrameName,
    VARIANT *&PostData,
    VARIANT *&Headers,
    VARIANT_BOOL *&Cancel
);

void StatusTextChange(          
    BSTR Text
);

cheers

tachyon

perlmonks.org content © perlmonks.org and Anonymous Monk, dimar, slloyd, tachyon

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03