Example: This DLL
#include <windows.h> __declspec(dllexport) short _stdcall test( short a, short b, short c, short d ) { return a+b+c+d; }
Built with this.DEF file
LIBRARY TESTDLL.DLL EXPORTS test
And this command line
cl /LD testdll.c /link /DEF:testdll.def
Can be successfully accessed using this perl/win32::API code:
#! perl -slw use strict; use Win32::API::Prototype;; ApiLink( 'testdll', q[SHORT test( SHORT a, SHORT b, SHORT c, SHORT d )] ) or die $^E;; print "$_*4 = ", test( ($_) x 4 ) for 1 .. 10; __END__ C:\test>testdll 1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16 5*4 = 20 6*4 = 24 7*4 = 28 8*4 = 32 9*4 = 36 10*4 = 40
However, if you omit the .DEF file above, then the exported entrypoint will be mangled and look like this:
Dump of file testdll.dll ... ordinal hint RVA name 1 0 00001000 _test@16
instead of like this (when built with the def file):
Dump of file testdll.dll File Type: DLL ... ordinal hint RVA name 1 0 00001000 test
And you would have to change the import and invokation to look like this (Note the use of the mangled name and the disabling of strict):
#! perl -slw #use strict; use Win32::API::Prototype;; ApiLink( 'testdll', q[SHORT _test@16( SHORT a, SHORT b, SHORT c, SHORT d )] ) or die $^E;; print "$_*4 = ", &{'_test@16'}( ($_) x 4 ) for 1 .. 10; __END__ C:\test>testdll 1*4 = 4 2*4 = 8 3*4 = 12 4*4 = 16 5*4 = 20 6*4 = 24 7*4 = 28 8*4 = 32 9*4 = 36 10*4 = 40
Alternatively, if the module was built using compiler options that mean that WINAPI translates to _cdecl (the standard C calling convention):
#include <windows.h> __declspec(dllexport) short _cdecl test( short a, short b, short c, short d ) { return a+b+c+d; }
Then although if you build it without a def file and inspect the dll it looks the same as when built with _stdcall and a .def file:
Dump of file testdll.dll ... ordinal hint RVA name 1 0 00001000 test
When you try to call it with Win32::API
#! perl -slw #use strict; use Win32::API::Prototype;; ApiLink( 'testdll', q[SHORT test( SHORT a, SHORT b, SHORT c, SHORT d )] ) or die $^E;; print "$_*4 = ", test( ($_) x 4 ) for 1 .. 10; __END__
You will get a segfault because of the mis-match in the calling conventions.
It is not always possible to tell which calling convention was used to build the DLL by simple inspection.
You say you have the .def file; it may be possible to determine from that. You also say that you don't get segfaults when you are calling the code, which is a pretty strong indication that it was built using _stdcall, which is generally the MS compiler default.
If the DLL was built with non-MS tools, things can be more complicated still.
In reply to Re: Win32::API and 'short' arguments
by BrowserUk
in thread Win32::API and 'short' arguments
by spurperl
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |