我正在编写一些代码,用于通过Win32::OLE
从Perl 5程序中驱动Internet Explorer,并且我正在寻找将数字状态/错误代码转换回Perl程序通过事件(例如NavigateError
)变成更人性化的形式。Internet Explorer COM自动化:将数字错误代码转换为字符串
是否有某种库函数可以将0x800C0005L或-2146697211转换为"INET_E_RESOURCE_NOT_FOUND"
或更具可读性?
我试过Win32::FormatMessage()
,但这似乎只适用于非应用程序特定的错误条件。
更新:下面是一些澄清示例代码。下面显示了一些测试 的输出。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::HiRes qw(sleep time);
use Win32::OLE qw(EVENTS);
use Win32::OLE::Variant;
$|++;
sub ie_browse {
my $url = shift;
my $ie = Win32::OLE->new('InternetExplorer.Application') or die;
Win32::OLE->WithEvents($ie,
sub {
my ($obj, $event, @args) = @_;
given ($event) {
when ('NavigateComplete2') {
push @extra,
'url='.($args[1]->As(VT_BSTR));
say "$event: @extra";
}
when ('NavigateError') {
push @extra,
'url='.($args[1]->As(VT_BSTR)),
'statuscode='.($args[3]->As(VT_I4));
say "$event: @extra";
}
}
}, 'DWebBrowserEvents2');
Win32::OLE->SpinMessageLoop;
$ie->{visible} = 1;
Win32::OLE->SpinMessageLoop;
$ie->Navigate2($url);
Win32::OLE->SpinMessageLoop;
while(1) {
Win32::OLE->SpinMessageLoop;
sleep(0.1);
}
}
ie_browse $ARGV[0];
这里有两个提取尝试的输出。当然,获取堆栈溢出 页面是成功的。
C:\Documents and Settings\nobody\Desktop>perl ie.pl http://stackoverflow.com/
NavigateComplete2: url=http://stackoverflow.com/
Terminating on signal SIGINT(2)
但是example.invalid
不存在。
C:\Documents and Settings\nobody\Desktop>perl ie.pl http://example.invalid/
NavigateError: url=http://example.invalid/ statuscode=-2146697211
NavigateComplete2: url=http://example.invalid/
Terminating on signal SIGINT(2)
我感兴趣的是转向数字值(-2146697211)已 通过回一些有用的东西。这不是OLE错误,而是由Internet Explorer COM对象发送的错误条件 。
@hillu谢谢。我仍然对此感到恼火。必须有一个API在某个地方。 – 2009-08-26 11:05:49