8 @libs = split(/;/, $ENV{'PERL5REXX'} || $ENV{'PERLREXX'} || $ENV{'LIBPATH'} || $ENV{'PATH'});
11 # Preloaded methods go here. Autoload methods go after __END__, and are
12 # processed by the autosplit program.
14 # Cannot be autoload, the autoloader is used for the REXX functions.
16 my $load_with_dirs = sub {
17 my ($class, $file, @where) = (@_);
18 return $dlls{$file} if $dlls{$file};
21 $handle = DynaLoader::dl_load_file("$_/$file.dll");
24 $handle = DynaLoader::dl_load_file($file) unless $handle;
25 return undef unless $handle;
26 my @packs = $INC{'OS2/REXX.pm'} ? qw(OS2::DLL::dll OS2::REXX) : 'OS2::DLL::dll';
27 my $p = "OS2::DLL::dll::$file";
28 @{"$p\::ISA"} = @packs;
29 *{"$p\::AUTOLOAD"} = \&OS2::DLL::dll::AUTOLOAD;
31 bless {Handle => $handle, File => $file, Queue => 'SESSION' }, $p;
35 my ($dirs, $class, $file) = (shift, shift, shift);
37 push @_, @libs if $dirs;
38 $handle = $load_with_dirs->($class, $file, @_)
40 my $path = @_ ? " from '@_'" : '';
41 my $err = DynaLoader::dl_error();
42 $err =~ s/\s+at\s+\S+\s+line\s+\S+\s*\z//;
43 croak "Can't load '$file'$path: $err";
47 confess 'Usage: OS2::DLL->new( <file> [<dirs>] )' unless @_ >= 2;
52 confess 'Usage: OS2::DLL->module( <file> [<dirs>] )' unless @_ >= 2;
57 confess 'Usage: load OS2::DLL <file> [<dirs>]' unless $#_ >= 1;
58 $load_with_dirs->(@_, @libs);
61 package OS2::DLL::dll;
66 $AUTOLOAD =~ /^OS2::DLL::dll::.+::(.+)$/
67 or confess("Undefined subroutine &$AUTOLOAD called");
68 return undef if $1 eq "DESTROY";
69 die "AUTOLOAD loop" if $1 eq "AUTOLOAD";
70 $_[0]->find($1) or confess($@);
75 confess 'Usage: $dllhandle->wrapper_REXX($func_name)' unless @_ == 2;
77 my $file = $self->{File};
78 my $handle = $self->{Handle};
79 my $prefix = exists($self->{Prefix}) ? $self->{Prefix} : "";
80 my $queue = $self->{Queue};
82 $prefix = '' if $name =~ /^#\d+/; # loading by ordinal
83 my $addr = (DynaLoader::dl_find_symbol($handle, uc $prefix.$name)
84 || DynaLoader::dl_find_symbol($handle, $prefix.$name));
86 OS2::DLL::_call($name, $addr, $queue, @_);
88 my $err = DynaLoader::dl_error();
89 $err =~ s/\s+at\s+\S+\s+line\s+\S+\s*\z//;
90 croak "Can't find symbol `$name' in DLL `$file': $err";
96 my $file = $self->{File};
99 my $f = eval {$self->wrapper_REXX($_)} or return 0;
100 ${"${p}::"}{$_} = sub { shift; $f->(@_) };
105 XSLoader::load 'OS2::DLL';
112 OS2::DLL - access to DLLs with REXX calling convention.
116 When you use this module, the REXX variable pool is not available.
118 See documentation of L<OS2::REXX> module if you need the variable pool.
123 $emx_dll = OS2::DLL->module('emx');
124 $emx_version = $emx_dll->emx_revision();
125 $func_emx_version = $emx_dll->wrapper_REXX('#128'); # emx_revision
126 $emx_version = $func_emx_version->();
130 =head2 Create a DLL handle
132 $dll = OS2::DLL->module( NAME [, WHERE] );
134 Loads an OS/2 module NAME, looking in directories WHERE (adding the
135 extension F<.dll>), if the DLL is not found there, loads in the usual OS/2 way
136 (via LIBPATH and other settings). Croaks with a verbose report on failure.
138 The DLL is not unloaded when the return value is destroyed.
140 =head2 Create a DLL handle (looking in some strange locations)
142 $dll = OS2::DLL->new( NAME [, WHERE] );
144 Same as L<C<module>|Create a DLL handle>, but in addition to WHERE, looks
145 in environment paths PERL5REXX, PERLREXX, PATH (provided for backward
148 =head2 Loads DLL by name
150 $dll = load OS2::DLL NAME [, WHERE];
152 Same as L<C<new>|Create a DLL handle (looking in some strange locations)>,
153 but returns DLL object reference, or undef on failure (in this case one can
154 get the reason via C<DynaLoader::dl_error()>) (provided for backward
157 =head2 Check for functions (optional):
159 BOOL = $dll->find(NAME [, NAME [, ...]]);
161 Returns true if all functions are available. As a side effect, creates
162 a REXX wrapper with the specified name in the package constructed by the name
163 of the DLL so that the next call to C<$dll->NAME()> will pick up the cached
166 =head2 Create a Perl wrapper (optional):
168 $func = $dll->wrapper_REXX(NAME);
170 Returns a reference to a Perl function wrapper for the entry point NAME
171 in the DLL. Similar to the OS/2 API, the NAME may be C<"#123"> - in this case
172 the ordinal is loaded. Croaks with a meaningful error message if NAME does
173 not exists (although the message for the case when the name is an ordinal may
176 =head2 Call external function with REXX calling convention:
178 $ret_string = $dll->function_name(arguments);
180 Returns the return string if the REXX return code is 0, else undef.
181 Dies with error message if the function is not available. On the first call
182 resolves the name in the DLL and caches the Perl wrapper; future calls go
185 Unless used inside REXX environment (see L<OS2::REXX>), the REXX runtime
186 environment (variable pool, queue etc.) is not available to the called
193 =item Call a _System linkage function via a pointer
195 If a function takes up to 20 ULONGs and returns ULONG:
197 $res = call20( $pointer, $arg0, $arg1, ...);
199 =item Same for packed arguments:
201 $res = call20_p( $pointer, pack 'L20', $arg0, $arg1, ...);
203 =item Same for C<regparm(3)> function:
205 $res = call20_rp3( $pointer, $arg0, $arg1, ...);
207 =item Same for packed arguments and C<regparm(3)> function
209 $res = call20_rp3_p( $pointer, pack 'L20', $arg0, $arg1, ...);
211 =item Same for a function which returns non-0 and sets system-error on error
213 call20_Dos( $msg, $pointer, $arg0, $arg1, ...); # die("$msg: $^E") if error
215 [Good for C<Dos*> API - and rare C<Win*> calls.]
217 =item Same for a function which returns 0 and sets WinLastError() on error
219 $res = call20_Win( $msg, $pointer, $arg0, $arg1, ...);
220 # would die("$msg: $^E") if error
222 [Good for most of C<Win*> API.]
224 =item Same for a function which returns 0 and sets WinLastError() on error but
225 0 is also a valid return
227 $res = call20_Win_0OK( $msg, $pointer, $arg0, $arg1, ...);
228 # would die("$msg: $^E") if error
230 [Good for some of C<Win*> API.]
232 =item As previous, but without die()
234 $res = call20_Win_0OK_survive( $pointer, $arg0, $arg1, ...);
235 if ($res == 0 and $^E) { # Do error processing here
238 [Good for some of C<Win*> API.]
244 If C<PERL_REXX_DEBUG> is set, emits debugging output. Looks for DLLs
245 in C<PERL5REXX>, C<PERLREXX>, C<PATH>.
249 Extracted by Ilya Zakharevich perl-module-OS2-DLL@ilyaz.org from L<OS2::REXX>
250 written by Andreas Kaiser ak@ananke.s.bawue.de.