On 2004-02-03, at 08:14:19 -0800, Chris Masters wrote:

> Hi All,
> 
> I have a C library that I am using that has a function
> that returns an array of 12 bytes as a character
> array.
> 
> I have wrapped this like:
> 
> void*
> mywrap(stringin)
>                 char *stringin
>         CODE:
>                 RETVAL = myFunction(stringin);
>         OUTPUT:
>                 RETVAL
> 
> 
> Why do I get different answers if the return type is
> defined as char *?

Because with the return type 'void *' the result is expected
to be a pointer, which will actually be returned as an IV.
With a return type 'char *', the result is expected to be a
string, making the XSUB return a PV (which is actually close
to what you want).

When in doubt about what happens when the return type changes,
it helps to have a look at the C code generated by xsubpp.

> How do I use the pointer within perl? I need to loop
> through each byte and print it's value (in hex or
> binary).

No way. The pointer is useless inside a perl script. What you
need is a copy of the array in a perl string.

You most probably want something like this:

  SV *
  mywrap(stringin)
                  char *stringin
          CODE:
                  RETVAL = newSVpvn(myFunction(stringin), 12);
          OUTPUT:
                  RETVAL

This will tell xsubpp that you want to return an SV *. You are
now responsible for creating the scalar value yourself. This
isn't very complicated. newSVpvn() creates a perl string (SV)
from a pointer and a length. The length is necessary, because
otherwise you cannot know only from the pointer that it points
to 12 bytes of valid data.

> OT: I need to prove I'm getting back the 12 bytes
> correctly. Should I convert the value to binary (96
> bits) using pack and unpack? If so, what TEMPLATES
> should I use?

>From perl, you maybe want to use unpack to turn the string
into an array again:

  use XS::Test;
  use Data::Dumper;
  
  $x = XS::Test::mywrap('foo');
  
  my @bytes = unpack 'C*', $x;
  
  print Dumper([EMAIL PROTECTED]);

Just replace XS::Test with the name of your XS module.

> I'm almost there, just need a push in the right
> direction. Thanks for any help,

Hope this helps,
Marcus

-- 
Gold's Law:
        If the shoe fits, it's ugly.

Reply via email to