home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Callback.pm < prev    next >
Encoding:
Perl POD Document  |  2003-03-10  |  3.9 KB  |  171 lines

  1. package Win32::API::Callback;
  2.  
  3. # See the bottom of this file for the POD documentation.  Search for the
  4. # string '=head'.
  5.  
  6. #######################################################################
  7. #
  8. # Win32::API::Callback - Perl Win32 API Import Facility
  9. # Version: 0.41
  10. # Date: 10 Mar 2003
  11. # Author: Aldo Calpini <dada@perl.it>
  12. # $Id: Callback.pm,v 1.0 2001/10/30 13:57:31 dada Exp $
  13. #######################################################################
  14.  
  15. $VERSION = "0.41";
  16.  
  17. require Exporter;       # to export the constants to the main:: space
  18. require DynaLoader;     # to dynuhlode the module.
  19. @ISA = qw( Exporter DynaLoader );
  20.  
  21. sub DEBUG { 
  22.     if ($WIN32::API::DEBUG) { 
  23.         printf @_ if @_ or return 1; 
  24.     } else {
  25.         return 0;
  26.     }
  27. }
  28.  
  29. use Win32::API;
  30. use Win32::API::Type;
  31. use Win32::API::Struct;
  32.  
  33. #######################################################################
  34. # This AUTOLOAD is used to 'autoload' constants from the constant()
  35. # XS function.  If a constant is not found then control is passed
  36. # to the AUTOLOAD in AutoLoader.
  37. #
  38.  
  39. sub AUTOLOAD {
  40.     my($constname);
  41.     ($constname = $AUTOLOAD) =~ s/.*:://;
  42.     #reset $! to zero to reset any current errors.
  43.     $!=0;
  44.     my $val = constant($constname, @_ ? $_[0] : 0);
  45.     if ($! != 0) {
  46.         if ($! =~ /Invalid/) {
  47.             $AutoLoader::AUTOLOAD = $AUTOLOAD;
  48.             goto &AutoLoader::AUTOLOAD;
  49.         } else {
  50.             ($pack,$file,$line) = caller;
  51.             die "Your vendor has not defined Win32::API::Callback macro $constname, used at $file line $line.";
  52.         }
  53.     }
  54.     eval "sub $AUTOLOAD { $val }";
  55.     goto &$AUTOLOAD;
  56. }
  57.  
  58.  
  59. #######################################################################
  60. # dynamically load in the API extension module.
  61. #
  62. bootstrap Win32::API::Callback;
  63.  
  64. #######################################################################
  65. # PUBLIC METHODS
  66. #
  67. sub new {
  68.     my($class, $proc, $in, $out) = @_;
  69.     my %self = ();
  70.  
  71.     # printf "(PM)Callback::new: got proc='%s', in='%s', out='%s'\n", $proc, $in, $out;
  72.         
  73.     $self{in} = [];
  74.     if(ref($in) eq 'ARRAY') {
  75.         foreach (@$in) {
  76.             push(@{ $self{in} }, Win32::API::type_to_num($_));
  77.         }    
  78.     } else {
  79.         my @in = split '', $in;
  80.         foreach (@in) {
  81.             push(@{ $self{in} }, Win32::API::type_to_num($_));
  82.         }            
  83.     }
  84.     $self{out} = Win32::API::type_to_num($out);
  85.     $self{sub} = $proc;
  86.     my $self = bless \%self, $class;
  87.     
  88.     DEBUG "(PM)Callback::new: calling CallbackCreate($self)...\n";
  89.     my $hproc = CallbackCreate($self);
  90.  
  91.     DEBUG "(PM)Callback::new: hproc=$hproc\n";
  92.  
  93.     #### ...if that fails, set $! accordingly
  94.     if(!$hproc) {
  95.         $! = Win32::GetLastError();
  96.         return undef;
  97.     }
  98.     
  99.     #### ok, let's stuff the object
  100.     $self->{code} = $hproc;
  101.     $self->{sub}  = $proc;
  102.  
  103.     #### cast the spell
  104.     return $self;
  105. }
  106.  
  107. sub MakeStruct {
  108.     my($self, $n, $addr) = @_;    
  109.     DEBUG "(PM)Win32::API::Callback::MakeStruct: got self='$self'\n";
  110.     my $struct = Win32::API::Struct->new($self->{intypes}->[$n]);    
  111.     $struct->FromMemory($addr);
  112.     return $struct;
  113. }
  114.  
  115. 1;
  116.  
  117. __END__
  118.  
  119. #######################################################################
  120. # DOCUMENTATION
  121. #
  122.  
  123. =head1 NAME
  124.  
  125. Win32::API::Callback - Callback support for Win32::API
  126.  
  127. =head1 SYNOPSIS
  128.  
  129.   use Win32::API;
  130.   use Win32::API::Callback;
  131.  
  132.   my $callback = Win32::API::Callback->new(
  133.     sub { my($a, $b) = @_; return $a+$b; },
  134.     "NN", "N",
  135.   );
  136.  
  137.   Win32::API->Import(
  138.       'mydll', 'two_integers_cb', 'KNN', 'N',
  139.   );
  140.  
  141.   $sum = two_integers_cb( $callback, 3, 2 );
  142.  
  143.  
  144. =head1 FOREWORDS
  145.  
  146. =over 4
  147.  
  148. =item *
  149. Support for this module is B<highly experimental> at this point.
  150.  
  151. =item *
  152. I won't be surprised if it doesn't work for you.
  153.  
  154. =item *
  155. Feedback is very appreciated.
  156.  
  157. =item *
  158. Documentation is in the work. Either see the SYNOPSIS above
  159. or the samples in the F<samples> directory.
  160.  
  161. =back
  162.  
  163. =head1 AUTHOR
  164.  
  165. Aldo Calpini ( I<dada@perl.it> ).
  166.  
  167. =cut
  168.  
  169.  
  170.