PerlDiscuss - Perl Newsgroups and mailing lists <[EMAIL PROTECTED]> writes: >Hi, > >I am using Swig to create a wrapper between C and Perl communication. It >generates run time error when I try to access file pointer in C opened in >Perl. The message shown at run time is > >"The instruction at "0x77f88216" referenced memory at "0x00000000". The >memory could not be written". > >Below are the corresponding files I am using to attain communication: > >1. Interface file cprog.i used in swig to create a wrapper file > >%module cprog >%typemap(perl5,in) FILE * { >$target = IoIFP(sv_2io($source));\ >} >int testing(FILE* fp);
perl5.8 and later don't use FILE * for IO by default. (There may be one under the hood but there may be UTF-8 encoding or CRLF stuff about too.) But for XS code it does provide a way to get a FILE * - as another post has pointed out XS typemaps take this into account. Note of you force a FILE * then you loose the effect of perl's IO "layers". If you are writing the C code specially for perl then using PerlIO * will allow you to access UTF-8 or other encoded files in same way perl does. > >Swig command I use to create wrapper cprog_wrap.c is > >swig -perl -exportall cprog.i > >2. C file "cprog.c" to communicate with perl > >#include <stdio.h> >int testing(FILE* fp) >{ >if(fp) >fputs("hello", fp); >else >return 0; >} If you wrote that as int testing (PerlIO *fp) { if (fp) PerlIO_printf(fp,"hello"); } It could work even if fp was a opened by perl as a gziped CRLF file in a Japanese encoding. > >3. Perl file "perl1.pl" I use to communicate with C > >file "cprog.c" >use cprog; >open(OUT, ">-"); >$ret = testing(*OUT); >print $ret; > >Following gives the compilling and linking I use to create the dll >"cprog.dll" > >cl.exe -I "c:\Program Files\Microsoft Visual Studio .NET\Vc7\include" >-Ic:\Perl\lib\CORE -I "c:\Program Files\Microsoft Visual Studio >NET\Vc7\PlatformSDK\Include" -c cprog.c cprog_wrap.c -nologo -Gf -W3 -MD >-Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT >-DNO_HASH_SEED -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO >-DPERL_MSVCRT_READFIX > >link.exe -dll -nologo -nodefaultlib -opt:ref,icf >-libpath:"C:\Perl\lib\CORE" -machine:x86 /out:cprog.dll cprog.obj >cprog_wrap.obj perl58.lib /LIBPATH:"c:\Program Files\Microsoft Visual >Studio .NET\Vc7\lib" msvcrt.lib kernel32.lib > >Actual run time problem occurs when I try to use "fputs()" in C using file >pointer created in Perl. > >Can anyone explain me soon wht's the problem and how should i rectify it? > >Regards, >Karthik.