From: Francis Cianfrocca Date: 2006-08-09T11:51:21+09:00 Subject: Re: Signaling Ruby from C/C++ Ok, Asterix, this was driving me crazy because I know I've done it before. Here's some code from an early version of EventMachine. Note that this code is Windows-only. For Unix, you need something a little different (socketpair). /*******************************************/ static int Pipe[2]; // C code called from a Ruby instance method. // We create an instance variable of whatever object // self is. int sp = _pipe (Pipe, 4096, _O_BINARY); if (sp) { // no pipe, handle the error somehow. } char buf [100]; snprintf (buf, sizeof(buf), "@rd = IO.new( %d, \"r\")", Pipe[0]); rb_eval_string (buf); /********************************************/ After executing this code, the object that called it has an instance variable named @rd which is an IO object you can select for readability in Ruby (or use a Ruby global if you want). The Windows program can write bytes to Pipe[1]. Obviously make sure Pipe isn't allocated on the stack, and obviously make sure you close both sides of the pipe when you're done with it. Also, be very wary that _pipe produces a half-duplex pipe in Windows! Unlike some Unix flavors, you may not write Pipe[0] and you may not read Pipe[1]. If you do, you'll get bizarre errors. -- Posted via http://www.ruby-forum.com/.