in reply to Calling c functions from perl...

<lang mode="C">I'm kind of curious - without the nul terminator, how does your C code know the length of input, or the length of the output, for that matter? I would have to think that you would need size_t's for the sizes of the buffers.</lang>

Replies are listed 'Best First'.
Re^2: Calling c functions from perl...
by ajaffres (Novice) on May 18, 2005 at 15:47 UTC
    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.
      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