Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.1 KiB

  1. # The documentation is at the __END__
  2. package Win32::OLE::Const;
  3. use strict;
  4. use Carp;
  5. use Win32::OLE;
  6. my $Typelibs;
  7. sub _Typelib {
  8. my ($clsid,$title,$version,$langid,$filename) = @_;
  9. # Filenames might have a resource index appended to it.
  10. $filename = $1 if $filename =~ /^(.*\.(?:dll|exe))(\\\d+)$/i;
  11. # Ignore if it looks like a file but doesn't exist.
  12. # We don't verify existance of monikers or filenames
  13. # without a full pathname.
  14. return unless -f $filename || $filename !~ /^\w:\\.*\.(exe|dll)$/;
  15. push @$Typelibs, \@_;
  16. }
  17. __PACKAGE__->_Typelibs;
  18. sub import {
  19. my ($self,$name,$major,$minor,$language,$codepage) = @_;
  20. return unless defined($name) && $name !~ /^\s*$/;
  21. $self->Load($name,$major,$minor,$language,$codepage,scalar caller);
  22. }
  23. sub EnumTypeLibs {
  24. my ($self,$callback) = @_;
  25. foreach (@$Typelibs) { &$callback(@$_) }
  26. return;
  27. }
  28. sub Load {
  29. my ($self,$name,$major,$minor,$language,$codepage,$caller) = @_;
  30. if (UNIVERSAL::isa($name,'Win32::OLE')) {
  31. my $typelib = $name->GetTypeInfo->GetContainingTypeLib;
  32. return _Constants($typelib, undef);
  33. }
  34. undef $minor unless defined $major;
  35. my $typelib = $self->LoadRegTypeLib($name,$major,$minor,
  36. $language,$codepage);
  37. return _Constants($typelib, $caller);
  38. }
  39. sub LoadRegTypeLib {
  40. my ($self,$name,$major,$minor,$language,$codepage) = @_;
  41. undef $minor unless defined $major;
  42. unless (defined($name) && $name !~ /^\s*$/) {
  43. carp "Win32::OLE::Const->Load: No or invalid type library name";
  44. return;
  45. }
  46. my @found;
  47. foreach my $Typelib (@$Typelibs) {
  48. my ($clsid,$title,$version,$langid,$filename) = @$Typelib;
  49. next unless $title =~ /^$name/;
  50. next unless $version =~ /^([0-9a-fA-F]+)\.([0-9a-fA-F]+)$/;
  51. my ($maj,$min) = (hex($1), hex($2));
  52. next if defined($major) && $maj != $major;
  53. next if defined($minor) && $min < $minor;
  54. next if defined($language) && $language != $langid;
  55. push @found, [$clsid,$maj,$min,$langid,$filename];
  56. }
  57. unless (@found) {
  58. carp "No type library matching \"$name\" found";
  59. return;
  60. }
  61. @found = sort {
  62. # Prefer greater version number
  63. my $res = $b->[1] <=> $a->[1];
  64. $res = $b->[2] <=> $a->[2] if $res == 0;
  65. # Prefer default language for equal version numbers
  66. $res = -1 if $res == 0 && $a->[3] == 0;
  67. $res = 1 if $res == 0 && $b->[3] == 0;
  68. $res;
  69. } @found;
  70. #printf "Loading %s\n", join(' ', @{$found[0]});
  71. return _LoadRegTypeLib(@{$found[0]},$codepage);
  72. }
  73. 1;
  74. __END__
  75. =head1 NAME
  76. Win32::OLE::Const - Extract constant definitions from TypeLib
  77. =head1 SYNOPSIS
  78. use Win32::OLE::Const 'Microsoft Excel';
  79. printf "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  80. my $wd = Win32::OLE::Const->Load("Microsoft Word 8\\.0 Object Library");
  81. foreach my $key (keys %$wd) {
  82. printf "$key = %s\n", $wd->{$key};
  83. }
  84. =head1 DESCRIPTION
  85. This modules makes all constants from a registered OLE type library
  86. available to the Perl program. The constant definitions can be
  87. imported as functions, providing compile time name checking.
  88. Alternatively the constants can be returned in a hash reference
  89. which avoids defining lots of functions of unknown names.
  90. =head2 Functions/Methods
  91. =over 4
  92. =item use Win32::OLE::Const
  93. The C<use> statement can be used to directly import the constant names
  94. and values into the users namespace.
  95. use Win32::OLE::Const (TYPELIB,MAJOR,MINOR,LANGUAGE);
  96. The TYPELIB argument specifies a regular expression for searching
  97. through the registry for the type library. Note that this argument is
  98. implicitly prefixed with C<^> to speed up matches in the most common
  99. cases. Use a typelib name like ".*Excel" to match anywhere within the
  100. description. TYPELIB is the only required argument.
  101. The MAJOR and MINOR arguments specify the requested version of
  102. the type specification. If the MAJOR argument is used then only
  103. typelibs with exactly this major version number will be matched. The
  104. MINOR argument however specifies the minimum acceptable minor version.
  105. MINOR is ignored if MAJOR is undefined.
  106. If the LANGUAGE argument is used then only typelibs with exactly this
  107. language id will be matched.
  108. The module will select the typelib with the highest version number
  109. satisfying the request. If no language id is specified then a the default
  110. language (0) will be preferred over the others.
  111. Note that only constants with valid Perl variable names will be exported,
  112. i.e. names matching this regexp: C</^[a-zA-Z_][a-zA-Z0-9_]*$/>.
  113. =item Win32::OLE::Const->Load
  114. The Win32::OLE::Const->Load method returns a reference to a hash of
  115. constant definitions.
  116. my $const = Win32::OLE::Const->Load(TYPELIB,MAJOR,MINOR,LANGUAGE);
  117. The parameters are the same as for the C<use> case.
  118. This method is generally preferrable when the typelib uses a non-english
  119. language and the constant names contain locale specific characters not
  120. allowed in Perl variable names.
  121. Another advantage is that all available constants can now be enumerated.
  122. The load method also accepts an OLE object as a parameter. In this case
  123. the OLE object is queried about its containing type library and no registry
  124. search is done at all. Interestingly this seems to be slower.
  125. =back
  126. =head1 EXAMPLES
  127. The first example imports all Excel constants names into the main namespace
  128. and prints the value of xlMarkerStyleDot (-4118).
  129. use Win32::OLE::Const ('Microsoft Excel 8.0 Object Library');
  130. print "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  131. The second example returns all Word constants in a hash ref.
  132. use Win32::OLE::Const;
  133. my $wd = Win32::OLE::Const->Load("Microsoft Word 8.0 Object Library");
  134. foreach my $key (keys %$wd) {
  135. printf "$key = %s\n", $wd->{$key};
  136. }
  137. printf "wdGreen = %s\n", $wd->{wdGreen};
  138. The last example uses an OLE object to specify the type library:
  139. use Win32::OLE;
  140. use Win32::OLE::Const;
  141. my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
  142. my $xl = Win32::OLE::Const->Load($Excel);
  143. =head1 AUTHORS/COPYRIGHT
  144. This module is part of the Win32::OLE distribution.
  145. =cut