From: ara.t.howard@... Date: 2006-06-02T12:14:42+09:00 Subject: Re: How to pass const char *vect[] to C via Ruby/DL? On Fri, 2 Jun 2006, Andreas Haas wrote: > but I get a SEGV and it is not complete anyways. Instead of > > ids[DL.sizeof('S')*i] = s.to_ptr > > I already tried > > ids[DL.sizeof('S')*i] = s > > and other variations I could think of ... > > You know: I can imagine there were better means for > passing string vectors to C, yet the C function interface > as such is given. > > So any help or hint is greatly appreciated! i'm no dl expert, but: harp:~ > cat a.rb # # generate the lib # unless test(?e, 'a.so') open('a.c','w') do |f| f.write <<-'code' int setv(const char *ids[]){ while(*ids){ printf("setv: %s\n", ids[0]); ids++; } printf("setv: %s\n", ids[0]); } code end system "gcc -shared a.c -o a.so" end require 'dl/import' def setv(ary) setv = $lib['setv', 'IP'] ids = ary.map{|s| s.to_ptr} ids << DL::PtrData.new(0) r,r1 = setv.call ids.to_ptr end $lib = DL.dlopen('./a.so') setv %w( 1 2 3 ) harp:~ > ruby a.rb setv: 1 setv: 2 setv: 3 setv: (null) regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama