home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / Win32 / OLE.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  10.1 KB  |  447 lines

  1. #
  2. # Documentation at the __END__
  3. #
  4.  
  5. package Win32::OLE;
  6.  
  7. $VERSION = '0.03';
  8. #use Carp;
  9. use Exporter;
  10. use DynaLoader;
  11. @ISA = qw( Exporter DynaLoader );
  12.  
  13. @EXPORT = qw(
  14.         Variant
  15.         VT_UI1
  16.         VT_I2
  17.         VT_I4
  18.         VT_R4
  19.         VT_R8
  20.         VT_DATE
  21.         VT_BSTR
  22.         VT_CY
  23.         VT_BOOL
  24.         );
  25. @EXPORT_OK = qw(
  26.         VT_EMPTY
  27.         VT_NULL
  28.         VT_DISPATCH
  29.         VT_ERROR
  30.         VT_VARIANT
  31.         VT_UNKNOWN
  32.         VT_UI2
  33.         VT_UI4
  34.         VT_I8
  35.         VT_UI8
  36.         VT_INT
  37.         VT_UINT
  38.         VT_VOID
  39.         VT_HRESULT
  40.         VT_PTR
  41.         VT_SAFEARRAY
  42.         VT_CARRAY
  43.         VT_USERDEFINED
  44.         VT_LPSTR
  45.         VT_LPWSTR
  46.         VT_FILETIME
  47.         VT_BLOB
  48.         VT_STREAM
  49.         VT_STORAGE
  50.         VT_STREAMED_OBJECT
  51.         VT_STORED_OBJECT
  52.         VT_BLOB_OBJECT
  53.         VT_CF
  54.         VT_CLSID
  55.         TKIND_ENUM
  56.         TKIND_RECORD
  57.         TKIND_MODULE
  58.         TKIND_INTERFACE
  59.         TKIND_DISPATCH
  60.         TKIND_COCLASS
  61.         TKIND_ALIAS
  62.         TKIND_UNION
  63.         TKIND_MAX
  64.            );
  65.  
  66. bootstrap Win32::OLE;
  67.  
  68. # compatibility
  69.  
  70. *Win32::OLELastError = \&Win32::OLE::LastError;
  71. *Win32::OLECreateObject = \&Win32::OLE::CreateObject;
  72. *Win32::OLEDestroyObject = \&Win32::OLE::DestroyObject;
  73. *Win32::OLEDispatch = \&Win32::OLE::Dispatch;
  74. *Win32::OLEGetProperty = \&Win32::OLE::GetProperty;
  75. *Win32::OLESetProperty = \&Win32::OLE::SetProperty;
  76.  
  77. # helper routines. see ole.xs for all the gory stuff.
  78.  
  79. sub AUTOLOAD {
  80.     my( $self ) = shift;
  81.     my $fReturn = "";
  82.     $AUTOLOAD =~ s/.*:://;
  83.     if ( Win32::OLE::Dispatch( $self, $AUTOLOAD, $fReturn, @_ ) ) {
  84.         return $fReturn;
  85.     } else {
  86.         return undef;
  87.     }
  88. }
  89.  
  90.  
  91. # Automation data types.
  92.  
  93. sub VT_EMPTY {0;}
  94. sub VT_NULL {1;}
  95. sub VT_I2 {2;}
  96. sub VT_I4 {3;}
  97. sub VT_R4 {4;}
  98. sub VT_R8 {5;}
  99. sub VT_CY {6;}
  100. sub VT_DATE {7;}
  101. sub VT_BSTR {8;}
  102. sub VT_DISPATCH {9;}
  103. sub VT_ERROR {10;}
  104. sub VT_BOOL {11;}
  105. sub VT_VARIANT {12;}
  106. sub VT_UNKNOWN {13;}
  107. sub VT_I1 {16;}
  108. sub VT_UI1 {17;}
  109. sub VT_UI2 {18;}
  110. sub VT_UI4 {19;}
  111. sub VT_I8 {20;}
  112. sub VT_UI8 {21;}
  113. sub VT_INT {22;}
  114. sub VT_UINT {23;}
  115. sub VT_VOID {24;}
  116. sub VT_HRESULT {25;}
  117. sub VT_PTR {26;}
  118. sub VT_SAFEARRAY {27;}
  119. sub VT_CARRAY {28;}
  120. sub VT_USERDEFINED {29;}
  121. sub VT_LPSTR {30;}
  122. sub VT_LPWSTR {31;}
  123. sub VT_FILETIME {64;}
  124. sub VT_BLOB {65;}
  125. sub VT_STREAM {66;}
  126. sub VT_STORAGE {67;}
  127. sub VT_STREAMED_OBJECT {68;}
  128. sub VT_STORED_OBJECT {69;}
  129. sub VT_BLOB_OBJECT {70;}
  130. sub VT_CF {71;}
  131. sub VT_CLSID {72;}
  132.  
  133.  
  134. # Typelib
  135.  
  136. sub TKIND_ENUM {0;}
  137. sub TKIND_RECORD {1;}
  138. sub TKIND_MODULE {2;}
  139. sub TKIND_INTERFACE {3;}
  140. sub TKIND_DISPATCH {4;}
  141. sub TKIND_COCLASS {5;}
  142. sub TKIND_ALIAS {6;}
  143. sub TKIND_UNION {7;}
  144. sub TKIND_MAX {8;}
  145.  
  146. sub new {
  147.     my( $object );
  148.     my( $c ) = shift;
  149.     my( $type ) = shift;
  150.     if ( CreateObject( $type, $object ) ) {
  151.         return $object;
  152.     } else {
  153.         return undef;
  154.     }
  155. }
  156.  
  157. sub Variant {
  158.     return Win32::OLE::Variant->new(@_);
  159. }
  160.  
  161. #sub DESTROY {
  162. #    my( $self ) = shift;
  163. #    #warn "Destroy called on $self\n";
  164. #}
  165.  
  166. # egregium
  167. *OLECreateObject = \&new;
  168.  
  169. package Win32::OLE::Variant;
  170.  
  171. sub new {
  172.     my $self = {};
  173.     my $pack = shift;
  174.     $self->{'Type'} = shift;
  175.     $self->{'Value'} = shift;
  176.     return bless $self, $pack;
  177. }
  178.  
  179. 1;
  180.  
  181. __END__
  182.  
  183. =head1 NAME
  184.  
  185. Win32::OLE - OLE Automation extensions and Variants
  186.  
  187. =head1 SYNOPSIS
  188.  
  189.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  190.     $ex->Amethod("arg")->Bmethod->{'Property'} = "foo";
  191.  
  192. =head1 DESCRIPTION
  193.  
  194. This module provides an interface to OLE Automation from Perl.
  195. OLE Automation brings VisualBasic like scripting capabilities and
  196. offers powerful extensibility and the ability to control many Win32
  197. applications from Perl scripts.
  198.  
  199. OCX's are currently not supported.
  200.  
  201. =head2 Functions/Methods
  202.  
  203. =over 8
  204.  
  205. =item new Win32::OLE $oleclass
  206.  
  207. OLE Automation objects are created using the new() method, the
  208. second argument to which must be the OLE class of the application
  209. to create.  Return value is undef if the attempt to create an
  210. OLE connection failed for some reason.
  211.  
  212. The object returned by the new() method can be used to invoke
  213. methods or retrieve properties in the same fashion as described
  214. in the documentation for the particular OLE class (eg. Microsoft
  215. Excel documentation describes the object hierarchy along with the
  216. properties and methods exposed for OLE access).
  217.  
  218. Properties can be retrieved or set using hash syntax, while methods
  219. can be invoked with the usual perl method call syntax.
  220.  
  221. If a method or property returns an embedded OLE object, method
  222. and property access can be chained as shown in the examples below.
  223.  
  224. =item Variant(TYPENAME, DATA)
  225.  
  226. This function returns a Win32::OLE::Variant object of the specified
  227. type that contains the given data.  The Win32::OLE::Variant object
  228. can be used to specify data types other than IV, NV or PV (which are
  229. supported transparently).  See L<Variants> below for details.
  230.  
  231. =back
  232.  
  233. =head2 Constants
  234.  
  235. These constants are exported by default:
  236.  
  237.     VT_UI1
  238.     VT_I2
  239.     VT_I4
  240.     VT_R4
  241.     VT_R8
  242.     VT_DATE
  243.     VT_BSTR
  244.     VT_CY
  245.     VT_BOOL
  246.  
  247. Other OLE constants are also defined in the Win32::OLE package,
  248. but they are unsupported at this time, so they are exported
  249. only on request:
  250.  
  251.     VT_EMPTY
  252.     VT_NULL
  253.     VT_DISPATCH
  254.     VT_ERROR
  255.     VT_VARIANT
  256.     VT_UNKNOWN
  257.     VT_UI2
  258.     VT_UI4
  259.     VT_I8
  260.     VT_UI8
  261.     VT_INT
  262.     VT_UINT
  263.     VT_VOID
  264.     VT_HRESULT
  265.     VT_PTR
  266.     VT_SAFEARRAY
  267.     VT_CARRAY
  268.     VT_USERDEFINED
  269.     VT_LPSTR
  270.     VT_LPWSTR
  271.     VT_FILETIME
  272.     VT_BLOB
  273.     VT_STREAM
  274.     VT_STORAGE
  275.     VT_STREAMED_OBJECT
  276.     VT_STORED_OBJECT
  277.     VT_BLOB_OBJECT
  278.     VT_CF
  279.     VT_CLSID
  280.  
  281. =head2 Variants
  282.  
  283. A Variant is a data type that is used to pass data between OLE
  284. connections.
  285.  
  286. The default behavior is to convert each perl scalar variable into
  287. an OLE Variant according to the internal perl representation.
  288. The following type correspondence holds:
  289.  
  290.         C type          Perl type       OLE type
  291.         ------          ---------       --------
  292.           int              IV            VT_I4
  293.         double             NV            VT_R8
  294.         char *             PV            VT_BSTR
  295.  
  296. Note that VT_BSTR is a wide character or Unicode string.  This presents a
  297. problem if you want to pass in binary data as a parameter as 0x00 is
  298. inserted between all the bytes in your data. The C<Variant()> method
  299. provides a solution to this.  With Variants the script
  300. writer can specify the OLE variant type that the parameter should be
  301. converted to.  Currently supported types are:
  302.  
  303.         VT_UI1     unsigned char
  304.         VT_I2      signed int (2 bytes)
  305.         VT_I4      signed int (4 bytes)
  306.         VT_R4      float      (4 bytes)
  307.         VT_R8      float      (8 bytes)
  308.         VT_DATE    OLE Date
  309.         VT_BSTR    OLE String
  310.         VT_CY      OLE Currency
  311.         VT_BOOL    OLE Boolean
  312.  
  313. When VT_DATE and VT_CY objects are created, the input
  314. parameter is treated as a Perl string type, which is then converted
  315. to VT_BSTR, and finally to VT_DATE of VT_CY using the VariantChangeType()
  316. OLE API function.  See L<EXAMPLES> for how these types can be used.
  317.  
  318. =head1 EXAMPLES
  319.  
  320. Here is a simple Microsoft Excel application.
  321.  
  322.     use Win32::OLE;
  323.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  324.     
  325.     # open an existing workbook
  326.     $ex->Workbooks->Open( 'test.xls' );
  327.     
  328.     # write to a particular cell
  329.     $ex->Workbooks(1)->Worksheets('Sheet1')->Cells(1,1)->{Value} = "foo";
  330.     
  331.     # save and exit
  332.     $ex->Save;
  333.     $ex->Quit;
  334.  
  335. Here is an example of using Variant data types.
  336.  
  337.     use Win32::OLE;
  338.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  339.     $ex->{Visible} = 1;
  340.     $ex->Workbooks->Add;
  341.     $ovR8 = Variant(VT_R8, "3 is a good number");
  342.     $ex->Range("A1")->{Value} = $ovR8;
  343.     $ex->Range("A2")->{Value} = Variant(VT_DATE, 'Jan 1,1970');
  344.  
  345. The above will put value "3" in cell A1 rather than the string
  346. "3 is a good number".  Cell A2 will contain the date.
  347.  
  348. Similarly, to invoke a method with some binary data, you can
  349. do the following:
  350.  
  351.     $obj->Method( Variant(VT_UI1, "foo\000b\001a\002r") );
  352.  
  353. Here is a wrapper class that basically delegates everything but
  354. new() and DESTROY().  Such a wrapper is needed for properly
  355. shutting down connections if your application is liable to
  356. die without proper cleanup.
  357.  
  358.     package Excel;
  359.     use Win32::OLE;
  360.     
  361.     sub new {
  362.         my $s = {};
  363.         if ($s->{Ex} = Win32::OLE->new('Excel.Application')) {
  364.         return bless $s, shift;
  365.         }
  366.         return undef;
  367.     }
  368.     
  369.     sub DESTROY {
  370.         my $s = shift;
  371.         if (exists $s->{Ex}) {
  372.         print "# closing connection\n";
  373.         $s->{Ex}->Quit;
  374.         return undef;
  375.         }
  376.     }
  377.     
  378.     sub AUTOLOAD {
  379.         my $s = shift;
  380.         $AUTOLOAD =~ s/^.*:://;
  381.         $s->{Ex}->$AUTOLOAD(@_);
  382.     }
  383.     
  384.     1;
  385.  
  386. The above module can be used just like Win32::OLE, except that
  387. it takes care of closing connections in case of abnormal exits.
  388.  
  389. =head1 NOTES
  390.  
  391. There are some incompatibilities with the version distributed by Activeware
  392. (as of build 306).
  393.  
  394. =over 4
  395.  
  396. =item 1
  397.  
  398. The package name has changed from "OLE" to "Win32::OLE".
  399.  
  400. =item 2
  401.  
  402. All functions of the form "Win32::OLEFoo" are now "Win32::OLE::Foo",
  403. though the old names are temporarily accomodated.
  404.  
  405. =item 3
  406.  
  407. Package "OLE::Variant" is now "Win32::OLE::Variant".
  408.  
  409. =item 4
  410.  
  411. The Variant function is new, and is exported by default.  So are
  412. all the VT_XXX type constants.
  413.  
  414. =back
  415.  
  416. You are responsible for properly closing any open OLE servers
  417. down.  For example, if you open a OLE connection to Excel and
  418. subsequently just die(), Excel will not shutdown and you will have
  419. a process leak on your hands.  You will need to wrap the OLE
  420. connection in your own object and provide a DESTROY method that
  421. does proper cleanup to ensure smooth shutdown.  Alternatively,
  422. you can use a __DIE__ hook or an END{} block to do such cleanup.
  423. See L<EXAMPLES> above for an example of using a wrapper object.
  424.  
  425. =head1 AUTHORS
  426.  
  427. Originally put together by the kind people at Hip and Activeware.
  428.  
  429. Gurusamy Sarathy <gsar@umich.edu> has subsequently fixed several
  430. major bugs, memory leaks, and reliability problems, along with some
  431. redesign of the code.
  432.  
  433. =head1 COPYRIGHT
  434.  
  435.     (c) 1995 Microsoft Corporation. All rights reserved. 
  436.     Developed by ActiveWare Internet Corp., http://www.ActiveWare.com
  437.  
  438.     Other modifications (c) 1997 by Gurusamy Sarathy <gsar@umich.edu>
  439.  
  440.     You may distribute under the terms of either the GNU General Public
  441.     License or the Artistic License, as specified in the README file.
  442.  
  443.  
  444. =cut
  445.  
  446.  
  447.