# Help embedding Ruby in Visual C++ project

**URL:** <https://www.ruby-forum.com/t/help-embedding-ruby-in-visual-c-project/48643>\
**Category:** Ruby\
**Created:** [November 23, 2005, 12:01am UTC](https://www.ruby-forum.com/t/help-embedding-ruby-in-visual-c-project/48643 "2005-11-23T00:01:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevewong4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@stevewong4](https://www.ruby-forum.com/u/stevewong4)\
**Post date:** [November 23, 2005, 12:01am UTC](https://www.ruby-forum.com/t/help-embedding-ruby-in-visual-c-project/48643/1 "2005-11-23T00:01:14Z")

</div>

Hi,  
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);

ruby\_init();  
ruby\_init\_loadpath();

rb\_load\_protect(rb\_str\_new2(“test.rb”), Qfalse, &state);

---

<div class="post-metadata">

**Author:** ![stevewong4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@stevewong4](https://www.ruby-forum.com/u/stevewong4)\
**Post date:** [November 23, 2005, 11:46am UTC](https://www.ruby-forum.com/t/help-embedding-ruby-in-visual-c-project/48643/2 "2005-11-23T11:46:38Z")

</div>

> > > > > “s” == stevewong4 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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  
the  
current directory.

You can use, ruby\_incpush() if you want to add directories to this  
variable.

moulon% grep ruby\_incpush _.h  
intern.h:void ruby\_incpush \_((const char_));  
moulon%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![stevewong4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2bfe46/32.png) [@stevewong4](https://www.ruby-forum.com/u/stevewong4)\
**Post date:** [November 23, 2005, 12:10pm UTC](https://www.ruby-forum.com/t/help-embedding-ruby-in-visual-c-project/48643/3 "2005-11-23T12:10:46Z")

</div>

In message [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
removed\_email\_address@domain.invalid writes

> Hi,  
> 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

Stephen

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

```
    ruby_init();

    ruby_script("embedded");

    char *args[] =
    {
            "arg1",
            "arg2",
            "arg3",
            NULL
    };

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

    char *options[] =
    {
            "",
            NULL
    };

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

    ruby_init_loadpath();

    rb_load_file("d:\\ruby_1_8_2_15\\samples\\tk\\tkhello.rb");

    ruby_run();

    return 0;

```

}
