使用Perl,使用Win32::OLE
库很容易加载COM / OLE对象并对其进行控制。我遇到的问题是确切地知道我正在访问的对象中可用的方法和属性。通过阅读对象上可用的所有属性和方法,其他语言的某些OLE工具包可以为您生成静态接口。 Perl的Win32::OLE
库是否存在这样的功能?
最佳答案
您应该直接从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;
}