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

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

Replies are listed 'Best First'.
Re^5: Calling c functions from perl...
by salva (Canon) on May 19, 2005 at 10:17 UTC
    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 };