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

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.

Replies are listed 'Best First'.
Re^4: Calling c functions from perl...
by ajaffres (Novice) on May 19, 2005 at 09:29 UTC
    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 };