Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Calling a C API in a dll from Perl on Windows

by jira0004 (Monk)
on Jun 24, 2002 at 14:37 UTC ( [id://176808] : perlquestion . print w/replies, xml ) Need Help??

jira0004 has asked for the wisdom of the Perl Monks concerning the following question:


I have been developing in Perl for a few years, but I have never mixed Perl and C before. According to "Advanced Perl Programming" by Sriram Srinivasan as well as several other sources Perl can be called from C and vice versa.

I have a product API that will allow me to run a product we are using here at work. The API is in C; the API is in a dll (dynamic link library). I would like to call this API from Perl. According to my Perl reference material this is possible. Can I do this on Windows or not???

Since I've never mixed Perl with any other language, any advice input or help would be useful.

Thanks for any helpful info regarding this topic.

P.S. I did do a search for this topic on the discussion groups for this site but didn't find any postings regarding calling "C" from Perl on Windows.

--- Peter Jirak


Replies are listed 'Best First'.
Re: Calling a C API in a dll from Perl on Windows
by fenonn (Chaplain) on Jun 24, 2002 at 15:55 UTC
    I think the best bet is to go with using Win32::API. I have used this module to easily allow my perl program to control Winamp. Here is a short example that uses a function in the user32.dll to send a Win32 message to Winamp to obtain its playback status.
    #!/usr/bin/perl -w use strict; use Win32::GUI; use Win32::API; my $winampHandle = Win32::GUI::FindWindow("Winamp v1.x", ""); print "Handle: $winampHandle\n"; my $sendMessage = new Win32::API("user32", "SendMessageA", ['N','N','N +','N'], 'N'); my $playStatus = $sendMessage->Call($winampHandle,1024,2,104); print $playStatus == 0 ? 'stopped' : $playStatus == 1 ? 'playing' : 'p +aused';
Re: Calling a C API in a dll from Perl on Windows
by RMGir (Prior) on Jun 24, 2002 at 14:41 UTC
    If you check out the perlxs and perlxstut doc pages, I think those will help you quite a bit.

    But if you don't want to go as far as writing an XS interface module, or your needs are simpler. Aldo Calpini wrote an AWESOME module, called Win32::API.

Re: Calling a C API in a dll from Perl on Windows
by Joost (Canon) on Jun 24, 2002 at 14:45 UTC
    Depending on the complixity of the API, you might be quite happy using Inline::C
    -- Joost downtime n. The period during which a system is error-free and immune from user input.
Re: Calling a C API in a dll from Perl on Windows
by broquaint (Abbot) on Jun 24, 2002 at 15:32 UTC
    If you're using Inline::C then it's as simple as setting the AUTO_INCLUDE (and optionally INC) parameter and calling the appropriate functions from within the C code.

    You could learn up on XS and write your own module, but this route in somewhat trickier.

    There's also the handy C::DynaLib module which is specifically designed for interfacing with C libraries from within perl. Here's an example from the module docs

    use C::DynaLib; $libc = new C::DynaLib("-lc"); $strncmp = $libc->DeclareSub("strncmp", "i", "p", "p", "I"); $string1 = "foobar"; $string2 = "foolish"; $result = &{$strncmp}($string1, $string2, 3); # $result is 0 $result = &{$strncmp}($string1, $string2, 4); # $result is -1



Re: Calling a C API in a dll from Perl on Windows
by Pug (Monk) on Jun 24, 2002 at 14:45 UTC
    Try Inline::C
    I have yet to use it but I have heard many good things about it.