I'm trying to substitute $structParameter by its low-level version as suggested in the Win32::API doc, this is using something like pack('NC',$size,$aKey).

Win32::API's letter codes and pack()'s letter codes are different, do not use letters from one on the other, always reread the docs for pack and Win32::API. You want 'LP' (L=native (LE) 32 bit, P=pointer to scalar string) for pack. That pack 'L' is a 'I' for Win32::API BTW.
# myCell* findInTableByKey(myTable that, struct cRefStr key, unsigned +offset); # with: # struct myCell { # unsigned char opaque[10]; #}; my $aKeyFunc = Win32::API->new("myDll.dll",'findInTableByKey', 'NSN',' +C','_cdecl');
Your prototype is wrong. 'C' as a return type either will be a undef, or a mangled something (number or single letter). You probably want 'N' instead of C to save that myCell * as a Perl scalar number.

If you C header prototype is absolutly correct. 'S' and ::Struct won't work. "struct cRefStr key" is a pass by copy parameter, not a pointer or pass by reference. Thats going to require serious hacking on your part to get working since Win32::API can't do pass by copy structs/strings/anything except native machine numbers. I know its possible.

Since that "struct cRefStr" has a size_t and a char *, and is pass by copy now, on 32 bit machine code ONLY (x64 machine code has a different calling convention and something else must be done), replace the 'S' letter with a 'N' and then a 'P', you will not have a struct anymore in Perl but the C function will think it is getting the pass by copy struct. So 'NSN' becomes 'NNPN'.

The recipe for pass by copy struct with ::API on 32 bit machine code is, do a pack() to create the binary packed struct as a scalar string. Then add nulls to the end of the scalar string to align it to a multiple of 4 (I think)(this can also be done with the pack() letter codes during the previous pack). Now do "length($packed)/4", 4 being the byte size of an ::API N on 32 bits, this is the number of Ns you will turn the packed scalar string into an array of scalar numbers, "@structasnums = unpack('L6', $packedstruct);", then for the prototype to ::API, you put "length($packed)/4" 'N's in a row where that struct is in the C header prototype so it looks like "$obj = new Win32::API('somedll.dll', 'aFunction', 'PPNNNNNNN', 'N');" . Then you do "$obj->Call($something, $foo1, @structasnums, $lastparam);". To break down the ::API prototype, "'PPNNNNNNN'", P=$something, P=$foo1, NNNNNN=@structasnums, N=$lastparam.

From the last thread you made, WIN32::API and void*, your running 32 bit cygwin perl with 64 bit ints on x64 Windows.

In reply to Re: Low-level version of Win32::API::Struct by bulk88
in thread Low-level version of Win32::API::Struct by sgv_81

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.