Help: WIN32::API to dll interfacing: crash on return value 
Author Message
 Help: WIN32::API to dll interfacing: crash on return value

I was very pleased with the Win32::API module to call arbitrary
dll's. However when making my own dll's, I run into a problem
in that Perl crashes as soon as I want to use a result value.
(Problem probably due to being a newbie in C/dll programming).

When calling routines from 'standard' dll's such as kernel32,
all goes fine, as in the below example:

$GetTempPath = new Win32::API("kernel32", "GetTempPath", [N, P], N);
$lpBuffer = " " x 80;                         # allocate buffer
$return = $GetTempPath->Call(80, $lpBuffer);    # 8 ("C:\TEMP\")

However when making my own dll (see extracted source code below),
the system crashes when I want to have the result value. As long
as I do not attempt to use the result value (e.g. by assigning
to a variable), all seems to work fine; e.g. argument handling
works OK. As soon as I assign the result value to $result, Perl
is halted (both on NT and Win'95), on NT with the message:
Exception number: access violation5 (0xc000000) address 0x002d0197

See the below example:

# long int dll_func_4  (long int *a)  { *a = 4; return 4L; }
$file = "C:\\Cygnus\\dllhelpers-0.2.5\\c\\cdll.dll";
$test4 = new Win32::API($file, "dll_func_4", [P], N);
        $var = pack("L", 12345);

        $test4->Call($var);                  # OK, $var=4
#       $result = $test4->Call($var);                # crashed

I tested the dll from within a 'C' program: all worked fine.
I compiled the dll both using egcs and using MS Visual C++,
this makes no difference in behaviour ('C' works fine,
Perl crashes).

What do I do wrong?

Kees Moerman.

--------------- tool versions used ----------------------------

gcc:  version egcs-2.91.66 19990314 (egcs-1.1.2 release) for mingw32
dll building utility for egcs:  dllhelpers-0.2.5
Perl: Win32 port of Perl, version 5.004_02
Win32::API Win32API-0.011

------------------------ cdll.h -------------------------------

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

DLLIMPORT long int dll_func_4 (long int*) ;

------------------------ cdll.c -------------------------------

#include "cdll.h"
DLLIMPORT long int dll_func_4  (long int *a)  { *a = 4; return 4L; }

------------------------ dllinit.c ----------------------------

BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{  switch (reason)
    {   case DLL_PROCESS_ATTACH: break;
        case DLL_PROCESS_DETACH: break;
        case DLL_THREAD_ATTACH: break;
        case DLL_THREAD_DETACH: break;
    }
  return TRUE;

Quote:
}

------------------------ to make ---------------------------------

gcc -c -DBUILDING_DLL=1 -I. -g -Wall  -o cdll.o cdll.c
gcc -c -DBUILDING_DLL=1 -I. -g -Wall  -o dllinit.o dllinit.c
dllwrap --export-all --output-def cdll.def --implib libcdll.a \
        --driver-name gcc -o cdll.dll cdll.o dllinit.o

----------------------------------------------------------------------
Prediction is very hard... Particularly of the future     (Niels Bohr)

    drs. C.M. Moerman (Kees)
    ASG/ESTC-ESTC (Embedded Systems Technology Centre)
    Philips Semiconductors, The Netherlands

    Phone: +31 40 2744385



Mon, 03 Dec 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. HELP! Invoking a DLL function using Win32::API

2. HELP! Invoking a DLL function using Win32::API

3. Win32::API and Novell DLL Pointers

4. Win32::API GetOpenFileName from comdlg32.dll

5. WIN32::API perl.dll is missing

6. Decoding complex structs returned from Win32::API

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

8. Using API's from 3rd party dlls [Newie question]

9. DLL/API Calls under Win NT

10. Please help, my WIN32::API script cant run after upgrading ActivePerl5.22 to 6.23

11. Win32::API needs help

12. Help with Win32::API

 

 
Powered by phpBB® Forum Software