Internet Explorer COM 自动化:将数字错误代码转换为字符串
我正在编写一些代码,用于通过 Win32:: 从 Perl 5 程序驱动 Internet Explorer OLE
,我正在寻找方法来转换通过事件传递回 Perl 程序的数字状态/错误代码(例如 NavigateError
) 转换为更易于理解的形式。
是否有某种库函数可以将 ie 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 对象发出信号。
I am writing some code for driving Internet Explorer from a Perl 5 program through Win32::OLE
, and I am looking for ways to convert numeric status/error codes that are delivered back to the Perl program via events (such as NavigateError
) into a somewhat more human-readable form.
Is there some kind of library function that converts i.e. 0x800C0005L or -2146697211 to "INET_E_RESOURCE_NOT_FOUND"
or something even more readable?
I have tried Win32::FormatMessage()
, but that seems to work only for non application-specific error conditions.
Update: Here is some example code for clarification. Some test
output is shown below.
#!/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];
Here is some output for two fetch attempt. Fetching the Stack Overflow
page is successful, of course.
C:\Documents and Settings\nobody\Desktop>perl ie.pl http://stackoverflow.com/
NavigateComplete2: url=http://stackoverflow.com/
Terminating on signal SIGINT(2)
But example.invalid
doesn't exist.
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)
I am interested in turning that numeric value (-2146697211) that has been
passed back into something useful. This is not an OLE error as such but an error condition
signaled by the Internet Explorer COM object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:根据您的评论,我相信您正在寻找有关
NavigateError
事件:您可以通过解析此列表来创建模块:
对于 OLE 相关错误,请参阅
Win32::OLE
文档:Update: In light of your comment, I believe you are looking for Microsoft's documentation on the
NavigateError
event:You could create a module by parsing this list:
For OLE related errors, see the
Win32::OLE
documentation: