in reply to Re: Calling c functions from perl...
in thread Calling c functions from perl...

The length is worked out withing the functions. I want to pass the a pointer only (not the whole array). I hope to create either an array or a string in perl and to pass the address of the array/string to the functions. Therefore, there is no need to pass the length.

Replies are listed 'Best First'.
Re^3: Calling c functions from perl...
by salva (Canon) on May 18, 2005 at 16:09 UTC
    SWIG will handle the input parameter for you without any typemap, but for the output parameter you need a way to get its length in order to convert it back to an SV and a typemapto do so.

    Look for perl5/typemaps.i in the SWIG Lib directory, you will find there some samples of how to handle output parameters... just grep for OUTPUT.

      Thank you very much for your answers.

      But in reply to salva, I seems that perl/swig doesn't understand 'unsigned char *' when I pass a string to my function.

      In Perl I wrote this:
      $message = 'Hello';
      &MyCProgram::CopyString($message);

      In my C program I wrote that in order to test simpy test the input (I will need to output something later):
      void CopyString(unsigned char *string1) {
      FILE *pf;
      pf = fopen("test.txt","w+");
      fputs(string1,pf);
      fclose(pf);
      }

      And finally we can find this in my interface file:
      %apply unsigned char *INPUT { unsigned char *string1 };
      extern void CopyString(unsigned char *string1);

      When I run my Perl program, I get this error message:
      Argument "Hello" isn't numeric in subroutine entry at ./index1.pl line 21.

      The problem here is that Perl expects a value and not a string. Ideally I would like to pass only the real address of my string.

      Have you got any idea how to solve my problem? Any help is welcomed! Merci beaucoup!

      Arnaud
        Oh, I see, by default SWIG expects unsigned chars to be numbers, not strings...

        Try using the typemaps for "signed char" instead:

        %apply signed char *INPUT { unsigned char *string1 }; %apply signed char *OUTPUT { unsigned char *string2 };