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

Rob*_*t P 13 com perl win32ole

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

Yi *_*hao 13

您应该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;
}
Run Code Online (Sandbox Code Playgroud)

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

keys %$OleObject;
Run Code Online (Sandbox Code Playgroud)

可以通过检索所有OLE方法Win32::OLE::TypeInfo.这段代码将打印$ 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;
}
Run Code Online (Sandbox Code Playgroud)