Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to pass a pointer to an array of 'unsigned char' C data type with Win32::API ("U0")

by tye (Sage)
on Jul 27, 2016 at 16:39 UTC ( #1168660=note: print w/replies, xml ) Need Help??


in reply to How to pass a pointer to an array of 'unsigned char' C data type with Win32::API

my $data_ref = pack ('C*', (0x81 , 0x55, 0x00, 0xE0));

That is close to what you want. It would have worked on a slightly older version of Perl. However, p5p have decided that packing binary structures in Perl should honor Unicode (don't get me started). So you get problematic results:

$ say "0x81, 0x55, 0x00, 0xE0" 129 85 0 224 $ say "unpack 'U0C*', pack 'C*', 0x81, 0x55, 0x00, 0xE0" 194 129 85 0 195 160

You're first and last values, having their 8th bit set, got turned into UTF-8 byte sequences (2 bytes each). You can prepend "U0" to the front of the pack template to avoid such.

- tye        

  • Comment on Re: How to pass a pointer to an array of 'unsigned char' C data type with Win32::API ("U0")
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to pass a pointer to an array of 'unsigned char' C data type with Win32::API ("U0")
by apeks_okad (Novice) on Jul 28, 2016 at 11:08 UTC

    Oh... now that makes sense.

    So I changed the code accordingly as follows

    my $data_ref = pack ('U0C*', (0x81 , 0x55, 0x00, 0xE0));

    But still end up with the same problem

    Maybe its worth the mention that I use Strawberry Perl 5.24.0 (32Bit) on a Win7 64 Bit Machine (since the DLL provided by the manufacturer is a 32 Bit DLL). Could this lead to problems with the array reference?

      Could this lead to problems with the array reference?

      I don't see any reason that should lead to any problems.

      There are other approaches (apart from Win32::API) that you can take - eg XS or Inline::C.
      I personally prefer either of those (especially the latter) over Win32::API as I've always found Win32::API difficult to get right - though it's possible that the deficiency is in me, as opposed to Win32::API.

      For XS/Inline::C you'll need the header file (which it seems you already have), the dll (which you definitely already have) and a suitable import library (which you might not have).
      To create the import library ( let's call it libAID.a), place a copy of AID.dll in the cwd and run:
      gendef AID.dll dlltool --kill-at --input-def AID.def --output-lib libAID.a
      I would then install Inline::C and access the dll functionality using it.
      (You'll want to first run perldoc Inline::C-Cookbook for some basic instructions on how to successfully use the module.)

      Cheere,
      Rob

      I don't see any reason for Perl to crash. But you don't even provide the error message associated with the Perl crash so we don't have much to go on to diagnose the problem.

      - tye        

        The error message is a windows error "Perl interpreter does not function anymore" (translated from the original German Error "Perl interpreter funktioniert nicht mehr"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1168660]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-10-24 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?