我正在編寫一些代碼,用於通過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