From: Shea Martin Date: 2007-01-26T01:15:11+09:00 Subject: Re: ebedded: calling a C function from script. Stefano Crocco wrote: > Alle 17:00, mercoled� 24 gennaio 2007, Shea Martin ha scritto: >> Here is another one i can't find in the book or online: >> >> How do I call a global function (which resides in a script) from C. >> >> >> >> >> ruby_init(); >> ruby_script("embedded"); >> >> init_my_ruby_stuff(); >> >> rb_load_file( "test.rb" ) >> ID id = rb_intern( "script_main" ); >> rb_funcall( rb_cObject, id, 0 ); >> >> ruby_finalize(); >> >> return 0; >> >> >> But this crashes with an access violation when I run it... am I doing >> something wrong? >> >> ~S > > Your problem is that you're calling 'script_main', which is an instance method > of class object, as if it were a class method of Object. To do this, you > should access the top level object. I've tried to find the correct method to > do this, in vain. The best suggestion is this > > VALUE top_level=rb_eval_string("self"); > rb_funcall(self, rb_intern("script_main"),0); > > As I said, there should be a better way, but this should work. > > Stefano > I tried what you said: VALUE script_obj = rb_eval_string("self"); ID script_main_id = rb_intern("script_main"); rb_funcall( script_obj, script_main_id, 0 ); but it crashes with the access violation. I also tried this: VALUE script_obj = rb_class_new_instance( 0, 0, rb_cObject ); ID script_main_id = rb_intern("script_main"); rb_funcall( script_obj, script_main_id, 0 ); and it has the same problem. Any ideas? ~S