XS code to call DLL 
Author Message
 XS code to call DLL

Hello,

This is my first attempt to try and access a DLL (Windows) from perl.
I have the C header file for the function definitions but don't know
if/how to link them with perl.  I looked into the XS module, but it
quickly confused me.  Any general guidence would be greatly appreciated
such as:
I'm assuming I need to make a module to access the .DLL
Do I need to write 'C' code to glue this together?
Can I do the module all in perl?

Thanks for any pointers/help

-brian



Sat, 20 Aug 2005 21:21:09 GMT  
 XS code to call DLL

Quote:

> Hello,

> This is my first attempt to try and access a DLL (Windows) from perl.
> I have the C header file for the function definitions but don't know
> if/how to link them with perl.

   use Win32::API;

Quote:
> I looked into the XS module, but it quickly confused me.

XS is a language, not a module.

It's essentially C, but with some other stuff thrown in to make hooking
it up to perl a little bit easier.

Quote:
> Any general guidence would be greatly appreciated such as:
> I'm assuming I need to make a module to access the .DLL
> Do I need to write 'C' code to glue this together?
> Can I do the module all in perl?

You probably can do it all in perl -- it depends on the data structures
involved.  If you need to pass to one of the dll's functions a function
pointer as a callback, then you'll need C or XS.  For the most common
types (ints, longs, pointers) Win32::API will work fine directly.

I *think*, but am not entirely certain, that you can use the following
tricks to pass 'float' or 'double' parameters to a .dll function:
   If a function takes a 'float' argument, tell Win32::API that you're
passing in a long, and then give it unpack("L", pack("f", $float)).
   If a takes a 'double' argument, tell Win32::API that you're passing
in two longs, and then give it unpack("LL", pack("d", $double)).
   If a .dll function returns a float, say that it returns a long, and
unpack("f",pack("L",$ret_val)) to get a float.

If these tricks don't work, or if the function returns a double, then
you may need to use XS.  

--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print



Sat, 20 Aug 2005 22:33:35 GMT  
 XS code to call DLL


<snip>

Quote:

> If these tricks don't work, or if the function returns a double, then
> you may need to use XS.

With Win32::API-0.20 you should be able to pass/receive doubles with [D] and
D respectively.
Documentation suggests that the same is supposed to work for floats ([F] and
F), but I've had no success with that - not that I've tried to any great
extent. (In fact I've only briefly used this module.)
Passing a value to the dll with [F] seems ok, but getting a returned float
with F is not. In the case I tried I was able to get the float back as a
double (D).

Just to throw something else into the mix, the op could also look at
accessing the dll functions using Inline::C module. It does all the XS stuff
for you, and getting access to the functions this way pretty much boils down
to being able to access them from C. Haven't actually accessed any Windows
system dll's this way, but I've found it very useful for some other dll's -
and I still don't know any XS ..... not so sure that that's either a selling
point *or* something to be proud of .....
:-)

Cheers,
Rob



Sun, 21 Aug 2005 07:13:41 GMT  
 XS code to call DLL

Quote:
> Hello,

> This is my first attempt to try and access a DLL (Windows) from perl.
> I have the C header file for the function definitions but don't know
> if/how to link them with perl.  I looked into the XS module, but it
> quickly confused me.  Any general guidence would be greatly appreciated
> such as:
> I'm assuming I need to make a module to access the .DLL
> Do I need to write 'C' code to glue this together?
> Can I do the module all in perl?

> Thanks for any pointers/help

+--------------------------------------------------------------------+
| Win32::API 0.40                                                    |

|   http://use.perl.org/article.pl?sid=03/03/08/2345221              |
+--------------------------------------------------------------------+


Win32::API, the popular tool to import functions from Win32 DLLs. in a
blurb, here's what this version can do: automatically define a Perl
sub with the name of the imported API (eg. no more $function->Call(...)
ugliness), support function prototypes (credits to Dave Roth for this),
support C structures 'natively', support callbacks (very experimental).
Enjoy!"

Discuss this story at:
    http://use.perl.org/comments.pl?sid=03/03/08/2345221

--
H.Merijn Brand    Amsterdam Perl Mongers (http://www.amsterdam.pm.org/)
using perl-5.6.1, 5.8.0 & 633 on HP-UX 10.20 & 11.00, AIX 4.2, AIX 4.3,
     WinNT 4, Win2K pro & WinCE 2.11 often with Tk800.024 &/| DBD-Unify
ftp://ftp.funet.fi/pub/languages/perl/CPAN/authors/id/H/HM/HMBRAND/



Fri, 26 Aug 2005 14:50:07 GMT  
 XS code to call DLL


Quote:

> +--------------------------------------------------------------------+
> | Win32::API 0.40                                                    |

> |   http://use.perl.org/article.pl?sid=03/03/08/2345221              |
> +--------------------------------------------------------------------+


> Win32::API, the popular tool to import functions from Win32 DLLs. in a
> blurb, here's what this version can do: automatically define a Perl
> sub with the name of the imported API (eg. no more $function->Call(...)
> ugliness), support function prototypes (credits to Dave Roth for this),
> support C structures 'natively', support callbacks (very experimental).
> Enjoy!"

> Discuss this story at:
>     http://use.perl.org/comments.pl?sid=03/03/08/2345221

This sounds great!  I don't see the latest version on dada's website.  It
still has
version 0.20 so I'll start with that although the automatic import stuff
sounds
much cleaner...

Thanks for the pointers!

-brian



Fri, 26 Aug 2005 20:52:16 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. XS DLL Wrapper Code: Please Review

2. XS DLL name clashes with DLL that needs to load (Win32)

3. Creating xs layer with specific name of the rersulting XS DLL

4. Creating xs layer with specific name of the rersulting XS DLL

5. Calling dll functions using Perl Code

6. XS code generates call to undefined procedure

7. Call C function in XS from C function in the same XS file

8. using other DLLs in XS under Win32

9. perl code to find version number of a Windows DLL

10. Calling dll from perl

11. Loading/Calling a DLL?

12. Calling a dll from a perl interface

 

 
Powered by phpBB® Forum Software