Tutorial :Executing a Cygwin process from .NET?



Question:

I'm trying to launch Cygwin version of ruby.exe from a .NET application, but I'm stuck.

c:\>"c:\cygwin\bin\ruby.exe" c:\test\ruby.rb  /usr/bin/ruby: no such file to load -- ubygems (LoadError)  

As you see Ruby can't locate libraries because it's looking some Linux style paths.

Obviously when I run ruby.exe from .NET since it can't find libraries it fails like above.

If I don't load any library it works fine :

c:\>"c:\cygwin\bin\ruby.exe" -v  ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]  

Originally cygwin starts with this cygwin.bat

chdir C:\cygwin\bin  bash --login -i  

How can I make .NET to first go into cygwin enviroment and then execute ruby in that enviroment ?

  • I can't use Windows Ruby, I need to cygwin ruby.
  • I'm aware of potential usage of interactively driving "bash" but that sounds dirty, unless there is nice way of doing it.


Solution:1

Are you using perhaps mixing native Windows rubygems and Cygwin ruby? Using Cygwin rubygems seems to work fine for me. (Why is your Cygwin ruby interpreter apparently searching a path with Windows backslashes in it?).

Alternatively, have you tried run.exe?

C:\cygwin\bin\run.exe -p /starting/dir exe_to_run  

Here's the man-page entry:

NAME

run - start programs with hidden console window

SYNOPSIS

run [ -p path ] command [ -wait ] arguments

runcommand [ -p path ] [ -wait ] arguments

DESCRIPTION

Windows programs are either GUI programs or console programs. When started console programs will either attach to an existing console or create a new one. GUI programs can never attach to an exiting con- sole. There is no way to attach to an existing console but hide it if started as GUI program.

run will do this for you. It works as intermediate and starts a pro- gram but makes the console window hidden.

With -p path you can add path to the PATH environment variable.

Issuing -wait as first program argument will make run wait for program completition, otherwise it returns immediately.

The second variant is for creating wrappers. If the executable is named runcommand (eg runemacs), run will try to start the program (eg emacs).

EXAMPLES

run -p /usr/X11R6/bin xterm

run emacs -wait runemacs -wait

run make -wait


Solution:2

Why don't you execute a non-interactive bash shell which runs ruby from your .NET application? Something like:

bash --login -c <your-path-to-ruby> <your-library-to-load>  

Not sure your file structure, but for example,

bash --login -c /usr/bin/ruby ~/test/ruby.rb  

Look at the -c option from the bash man page for more info.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »