# Help embedding Ruby in Visual C++ project

**URL:** <https://rubytalk.org/t/help-embedding-ruby-in-visual-c-project/22702>\
**Category:** ruby-talk\
**Created:** [22 November 2005 22:57 UTC](https://rubytalk.org/t/help-embedding-ruby-in-visual-c-project/22702 "2005-11-22T22:57:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stevewong4](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Stevewong4](https://rubytalk.org/u/Stevewong4)\
**Post date:** [22 November 2005 22:57 UTC](https://rubytalk.org/t/help-embedding-ruby-in-visual-c-project/22702/1 "2005-11-22T22:57:24Z")

</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:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [23 November 2005 10:45 UTC](https://rubytalk.org/t/help-embedding-ruby-in-visual-c-project/22702/2 "2005-11-23T10:45:29Z")

</div>

> 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:** ![Stephen\_Kellett](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Stephen\_Kellett](https://rubytalk.org/u/Stephen_Kellett)\
**Post date:** [23 November 2005 11:07 UTC](https://rubytalk.org/t/help-embedding-ruby-in-visual-c-project/22702/3 "2005-11-23T11:07:24Z")

</div>

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)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NtInitialize(&argc, &argv);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_init();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_script("embedded");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char \*args =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"arg1",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"arg2",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"arg3",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_set\_argv((sizeof(args) / sizeof(args[0])) - 1, args);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char \*options =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_options(0, options); // required to setup TK properly

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_init\_loadpath();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rb\_load\_file("d:\\ruby\_1\_8\_2\_15\\samples\\tk\\tkhello.rb");

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ruby\_run();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
}

> **···**
>
> In message \<1132700093.526946.71730@g47g2000cwa.googlegroups.com\>, stevewong4@gmail.com 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?
> 
> --  
> Stephen Kellett  
> Object Media Limited [http://www.objmedia.demon.co.uk/software.html](http://www.objmedia.demon.co.uk/software.html)  
> Computer Consultancy, Software Development  
> Windows C++, Java, Assembler, Performance Analysis, Troubleshooting
