# Embedding the Ruby interpreter in C# app

**URL:** <https://www.ruby-forum.com/t/embedding-the-ruby-interpreter-in-c-app/70308>\
**Category:** Ruby\
**Created:** [September 28, 2006, 3:16am UTC](https://www.ruby-forum.com/t/embedding-the-ruby-interpreter-in-c-app/70308 "2006-09-28T03:16:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Partridge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@Nick\_Partridge](https://www.ruby-forum.com/u/Nick_Partridge)\
**Post date:** [September 28, 2006, 3:16am UTC](https://www.ruby-forum.com/t/embedding-the-ruby-interpreter-in-c-app/70308/1 "2006-09-28T03:16:32Z")

</div>

Hi, I’m looking for some assistance in embedding the ruby interpreter  
in C#. I’ve got well through the basics of using p/invoke to load the  
C api methods, and I’m having some brain-destroying problems with  
getting a ruby string back to a System.String for certain size  
strings.

[http://pastie.caboo.se/15227](http://pastie.caboo.se/15227)

The pastie has some snips of the code that I can reproduce the failure  
with. It seems, that for arbitrarily large strings rb\_string\_to\_cstr  
fails. The value of i at which the code fails is not constant and  
seems to be anywhere greater than 200. Occasionally, the code will run  
through to completion.

I have no idea what I need to do here. I’ve tried some GCHandle  
pinning of objects to no avail, but I’m pretty green at  
.net/C#/dllimport/p|invoke.

Has anyone done this? Or something similar and can shed some light on  
my problem?

– Nick
