1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# header stuff
use feature ':5.10';
binmode(STDOUT, ":encoding(cp850)");
use Win32::API;
# header end
# *******************************
# -> THE MAGIC SHOULD START HERE:
# *******************************
# invoke the function 'DOTP' within the Carbondioxide.dll
# with 2 arguments, Double precision 'DD' and a Double precision return 'D'
# (shall calculate the density of co2 at given temperature in Kelvin and pressure in MPa)
# !!! ARGUMENTS shall be handed over BY REFERENCE whereas
# RETURN values of the function are given back BY VALUE
$function=Win32::API->new('CARBONDIOXIDE','DOTP','DD','D');
$T=300;
$P=6;
$return = $function->Call(\$T,\$P); # CRASHES here for unknown reason
say $return."\n";
$return = $function->Call(\$T,\$P); # CRASHES here for unknown reason
Guest Fabian[...]
Die .dll heißt Carbondioxide.dll und die Funktion dazu DOTP mit den Argumenten Druck und Temperatur. Diese Argumente sind als Referenz zu übergeben; der Rückgabewert erfolgt direkt als value schreiben die Autoren der .dll.
[...]
Code (perl): (dl )$function=Win32::API->new('CARBONDIOXIDE','DOTP','DD','D');
[...]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use 5.010; use strict; use warnings; use Win32::API; # Wrapper for external function with awkward interface sub DOTP { # Get arguments my ($T, $P) = @_; # Prepare foreign function interface, if necessary state $ffi = Win32::API->new( 'CARBONDIOXIDE', 'double DOTP(LPDOUBLE lpT, LPDOUBLE lpP)' ); # Pack arguments into memory to be passed to the function my $bufT = pack('d', $T); my $bufP = pack('d', $P); # Perform external call and return value return $ffi->Call($bufT, $bufP); } # Test the thing say DOTP(300, 6);