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


I'm trying to use a simple windows .dll through the Win32::API utility. Every time I call a C-function that uses arguments, I get an exception that causes Perl to quit. I've broken it down to the following test example.

use strict; use Win32::API; my $winPerlTest = Win32::API->new( 'test', 'PerlTest', 'I', 'I'); die "Can't import API PerlTest: $!\n" unless ( defined $winPerlTest); my $id = 137; my $return; $return = $winPerlTest->Call($id); print $return, "\n"; __DATA__ /* declaration of PerlTest C-function */ int PerlTest(int a) { return a+1; }
If I redefine the PerlTest function to take no input arguments, (i.e. return some static number), it works correctly. Also, the examples given in the POD concerning kernel32.dll functions also work correctly, including those with input arguments. This leads me to suspect I'm missing something when creating the C test.dll

I'm using AS 586.811 and Win32::API v0.41 that I got through PPM from AS.

Thanks in advance, Jim

Replies are listed 'Best First'.
Re: Help avoiding exception in Win32::API ?
by Tanktalus (Canon) on Sep 14, 2005 at 21:10 UTC

    Trying to avoid Windows at nearly any cost for the last, oh, 10 years now, all I can ask is whether you're compiling in C or C++ mode, and, if C++, try in C mode, or just simply put 'extern "C"' in front of your function name:

    extern "C" int PerlTest(int a) { return a+1; }
    Just a WAG, but I offer it in the off-chance that this is the problem. Last I recall, MSVC defaulted to C++ mode for compiling C code, but I could easily be misremembering, and they could have easily changed the default, or both. :-)

      sorry, my node below 492054 should have been a reply to your node.

      - j

Re: Help avoiding exception in Win32::API ?
by PodMaster (Abbot) on Sep 15, 2005 at 07:53 UTC
    Every time I call a C-function that uses arguments, I get an exception that causes Perl to quit.
    What exception (what's the message you receive)? You need to investigate (maybe fire up Event Viewer, or a debugger).

    You might need to make sure that your DLL is compiled with the -MD flag (or whatever perl -V:ccflags reports).

    update: also, try enabling Win32::API::DEBUG and edit API.h and #define WIN32_API_DEBUG.

    I did some testing, and I get a memory access violation in the perl dll, and I've tracked it down to API.xs line 651         XSRETURN_IV(lReturn);. It happens with Win32-API-0.41 and perl 5.8.4, 5.6.2, 5.8.7, but not 5.8.6, go figure. XSRETURN_IV comes from XSUB.h (official api). You should solicit help from the author and the perl5-porters. Here's the verbatim debug output

    (XS)Win32::API::Call: params[0].t=1, .u=137 (XS)Win32::API::Call: parameter 0 (N) is 137 (XS)Win32::API::Call: Calling ApiFunctionInteger() (XS)Win32::API::Call: ApiFunctionInteger returned 138 (XS)Win32::API::Call: freeing memory... (XS)Win32::API::Call: returning to caller. (XS)Win32::API::Call: returning 138.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.


      Thanks for the suggestions. I will pull down the source and rebuild Win32::API as you suggest (I had been using the pre-compiled binaries from Activestate).

      I've also started looking at Swig to create a direct connection to the actual .dll that I need to use.

      Thanks for the help, J

Re: Help avoiding exception in Win32::API ?
by jimbojones (Friar) on Sep 14, 2005 at 22:06 UTC

    Thanks for the update, but the cl.exe compiler threw an error when I used the extern "C" as above. So I think it's compiling it as C, (not C++).

    - j