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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Win32::API and 'short' arguments by BrowserUk
in thread Win32::API and 'short' arguments by spurperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.