如何在 Perl 中以编程方式发现 Win32::OLE 对象的属性和方法?

发布于 2024-10-25 02:56:54 字数 236 浏览 1 评论 0原文

对于 Perl,使用 Win32::OLE 库来加载非常容易建立 COM/OLE 对象并控制它们。我遇到的问题是确切地知道我正在访问的对象中有哪些方法和属性可用。其他语言的某些 OLE 工具包可以通过读取对象上可用的所有属性和方法来为您生成静态接口。 Perl 的 Win32::OLE 库中是否存在这样的功能?

With Perl, it's quite easy using the Win32::OLE library to load up COM/OLE objects and control them. The issue I'm running into is knowing exactly what methods and properties are available in the object I'm accessing. Some OLE toolkits in other languages can generate a static interface for you by reading all of the properties and methods that are available on the object. Does such a facility exist with Perl's Win32::OLE library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-11-01 02:56:54

您应该直接从 Win32::OLE 对象的键访问属性。我们以 Excel 为例。代码来自 Win32::OLE 示例 -properties.pl
它将显示 Win32::OLE 对象的所有属性。

my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
# Add a workbook to get some more property values defined
$Excel->Workbooks->Add;
print "OLE object's properties:\n";
foreach my $Key (sort keys %$Excel) {
    my $Value;

    eval {$Value = $Excel->{$Key} };
    $Value = "***Exception***" if $@;

    $Value = "<undef>" unless defined $Value;

    $Value = '['.Win32::OLE->QueryObjectType($Value).']' 
      if UNIVERSAL::isa($Value,'Win32::OLE');

    $Value = '('.join(',',@$Value).')' if ref $Value eq 'ARRAY';

    printf "%s %s %s\n", $Key, '.' x (40-length($Key)), $Value;
}

在一行中,要获取 Win32::OLE 对象的所有属性:

keys %$OleObject;

可以通过 Win32::OLE::TypeInfo 检索所有 OLE 方法。此代码块将打印 $OleObject 的所有方法名称:

my $typeinfo = $OleObject->GetTypeInfo();
my $attr = $typeinfo->_GetTypeAttr();
for (my $i = 0; $i< $attr->{cFuncs}; $i++) {
    my $desc = $typeinfo->_GetFuncDesc($i);
    # the call conversion of method was detailed in %$desc
    my $funcname = @{$typeinfo->_GetNames($desc->{memid}, 1)}[0];
    say $funcname;
}

You should access the properties from the Win32::OLE object's keys directly. Let's use Excel as the example. The code is from Win32::OLE examples - properties.pl
It will show all properties of an Win32::OLE object.

my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
# Add a workbook to get some more property values defined
$Excel->Workbooks->Add;
print "OLE object's properties:\n";
foreach my $Key (sort keys %$Excel) {
    my $Value;

    eval {$Value = $Excel->{$Key} };
    $Value = "***Exception***" if $@;

    $Value = "<undef>" unless defined $Value;

    $Value = '['.Win32::OLE->QueryObjectType($Value).']' 
      if UNIVERSAL::isa($Value,'Win32::OLE');

    $Value = '('.join(',',@$Value).')' if ref $Value eq 'ARRAY';

    printf "%s %s %s\n", $Key, '.' x (40-length($Key)), $Value;
}

In one line, to get all properties of a Win32::OLE object:

keys %$OleObject;

All OLE methods can be retrieved via Win32::OLE::TypeInfo. this block of code will print all the method names of $OleObject:

my $typeinfo = $OleObject->GetTypeInfo();
my $attr = $typeinfo->_GetTypeAttr();
for (my $i = 0; $i< $attr->{cFuncs}; $i++) {
    my $desc = $typeinfo->_GetFuncDesc($i);
    # the call conversion of method was detailed in %$desc
    my $funcname = @{$typeinfo->_GetNames($desc->{memid}, 1)}[0];
    say $funcname;
}
染墨丶若流云 2024-11-01 02:56:54

有一点是肯定的,如果你这样做:

print Data::Dumper->Dump( [ $my_ole_object ] )

你很可能只会陷入无限循环。但你可以这样修改它:

local $Data::Dumper::Maxdepth = 2;
print Data::Dumper->Dump( [ $my_ole_object ] )

然后你至少可以看到属性名称。要查看其下一个级别的值,您需要 Maxdepth=3。我不太确定如何查看文档以外的所有方法。

One thing is for sure, if you do this:

print Data::Dumper->Dump( [ $my_ole_object ] )

you'll likely only get an endless loop. But you can modify it like this:

local $Data::Dumper::Maxdepth = 2;
print Data::Dumper->Dump( [ $my_ole_object ] )

And then you can at least see the property names. To see their next level of values, you will need Maxdepth=3. I'm not exactly sure how to look at all the methods other than documentation.

命比纸薄 2024-11-01 02:56:54

不,但看起来必要的类型发现代码 已经在 Win32 中::OLE 的实现。您可以使用它作为参考来编写自己的 Perl 扩展,该扩展公开函数和方法的类型和名称。

No. but it looks like the necessary type discovery code is already in Win32::OLE's implementation. You can use it as a reference to write your own perl extension that exposes function and method types and names.

恋竹姑娘 2024-11-01 02:56:54

如果您使用 ActiveState,则包含 OLE 浏览器(可在“开始”菜单中找到)。它要求您启用“本地 Intranet”区域的“初始化和脚本未标记为安全的 ActiveX 控件” 安全设置,但通常它运行良好,并为您提供以下列表:所有属性和方法及其类型。

另一个好的来源是各个应用程序中的文档 - MS 应用程序通常附带 VBA 文档。请注意,这些应用程序中描述的对象模型与 OLE 链接相同。

If you are using ActiveState, there is OLE Browser included (available in Start menu). It requires you to enable "Initialize and script ActiveX controls not marked as safe" security setting of the "Local intranet" zone, but generally it works well and gives you list of all properties and methods along with its types.

Another good source is documentation in individual applications - MS applications usually come with VBA docs. Note that object model described in those apps is the same OLE links to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文