Forum: Ruby Help embedding Ruby in Visual C++ project

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3e24b9aa7510c8edb2889e483198e04b?d=identicon&s=25 stevewong4 (Guest)
on 2005-11-23 00:01
(Received via mailing list)
I am trying to embed Ruby within a Visual C++ project, but I am having
trouble loading my script file.

How do I tell the Ruby interpreter where to look for my script file?

If I use ruby_options(), then my program hangs.  So, I am trying to
pass the "-S" flag to Ruby, and I have set RUBYPATH to the directory
where "test.rb" lives.

Note: I am able to load the script if I put the script in the same
directory as my source files (which isn't the same directory as the
built C++ .exe).  I do not understand why this works.  Ideally, I would
like to tell the Ruby interpreter where to find my "test.rb" script.
How do I do this?  Thanks in advance for your help.

Here is a code snippet:
int state;
int rargc=2;
char *rargv[2], rarg1[MAX_PATH]

strcpy(rarg1, "-S");
rargv[0] = "ruby";
rargv[1] = rarg1;

NtInitialize(&rargc, (char ***)&rargv);


rb_load_protect(rb_str_new2("test.rb"), Qfalse, &state);
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 decoux (Guest)
on 2005-11-23 11:46
(Received via mailing list)
>>>>> "s" == stevewong4  <> writes:

s> ruby_init_loadpath();

 Well, ruby use the variable $LOAD_PATH to know where it must search a
 library. This is this variable which is initialized when you call
 rb_init_loadpath() and by default (with $SAFE == 0) it add `.', the
 current directory, in it. This is why it find the script when it's in
 current directory.

 You can use, ruby_incpush() if you want to add directories to this

moulon% grep ruby_incpush *.h
intern.h:void ruby_incpush _((const char*));

Guy Decoux
Dba0cb4cdad3b8e3b7ed0fddff5d20a5?d=identicon&s=25 snail (Guest)
on 2005-11-23 12:10
(Received via mailing list)
In message <>, writes
>I am trying to embed Ruby within a Visual C++ project, but I am having
>trouble loading my script file.
>How do I tell the Ruby interpreter where to look for my script file?

When we want to test something in Ruby using C we have a test harness.
I've stripped out the non-relevant code to reveal this. Hope it helps


int main(int argc, char* argv[])
        NtInitialize(&argc, &argv);



        char    *args[] =

        ruby_set_argv((sizeof(args) / sizeof(args[0])) - 1, args);

        char    *options[] =

        ruby_options(0, options);       // required to setup TK properly




        return 0;
This topic is locked and can not be replied to.