From: Gordon Thiesfeld Date: 2010-07-03T02:15:17+09:00 Subject: Re: Ruby/DL problem On Fri, Jul 2, 2010 at 10:37 AM, Aston wrote: > Hello, > > I have very little experience with dl. But I want to use it to > call SetEnvironmentVariable() from Win32. I could use FFI but cannot, since this > script will be supplied to a customer where it is difficult to install. > > SetEnvironmentVariable on > MSDN http://msdn.microsoft.com/en-us/library/ms686206(v=VS.85).aspx > > reading sparse, old, and cryptic documentation I came up with this snippet, > which of course does not work > > require 'dl/import' > > module Win32 > extend DL::Importer > extern "int SetEnvironmentVariableA( char* lpName, char* lpValue)" > extern "int GetEnvironmentVariableA( char* lpName, char* lpValue, int size)" > end > > buff = Array.new( 256, 0).pack("c*") > Win32.GetEnvironmentVariableA( "ENV_VAR", buff, 255) > puts buff.unpack("a*") > > this is the error i get > D:/ruby/usr/lib/ruby/1.9.1/dl/import.rb:194:in `import_function': undefined > method `sym' for nil:NilClass (NoMethodError) >        from D:/ruby/usr/lib/ruby/1.9.1/dl/import.rb:118:in `extern' >        from test.rb:5:in `' >        from test.rb:3:in `
' > > machine is WinXP > ruby 1.9.1 (2008-10-28 revision 19983) [i386-mswin32_80] > > can anybody suggest a better signature ? > > Aston > I can't help you with your issue, but I'm curious why it's necessary. The MSDN page you link to says SetEnvironmentVariable is only for the current process. Doesn't ENV do what you need? irb(main):001:0> ENV['FOO']='bar' => "bar" irb(main):002:0> `echo %FOO%` => "bar\n" irb(main):003:0>