Ruby/DL problem

A

Aston

Hello,=0A=0AI have very little experience with dl. But I want to use it to =
=0Acall SetEnvironmentVariable() from Win32. I could use FFI but cannot, si=
nce this =0Ascript will be supplied to a customer where it is difficult to =
install.=0A=0ASetEnvironmentVariable on =0AMSDN http://msdn.microsoft.com/e=
n-us/library/ms686206(v=3DVS.85).aspx=0A=0Areading sparse, old, and cryptic=
documentation I came up with this snippet, =0Awhich of course does not wor=
k=0A=0Arequire 'dl/import'=0A=0Amodule Win32=0Aextend DL::Importer=0Aextern=
"int SetEnvironmentVariableA( char* lpName, char* lpValue)"=0Aextern "int =
GetEnvironmentVariableA( char* lpName, char* lpValue, int size)"=0Aend=0A=
=0Abuff =3D Array.new( 256, 0).pack("c*")=0AWin32.GetEnvironmentVariableA( =
"ENV_VAR", buff, 255)=0Aputs buff.unpack("a*")=0A=0Athis is the error i get=
=0AD:/ruby/usr/lib/ruby/1.9.1/dl/import.rb:194:in `import_function': undefi=
ned =0Amethod `sym' for nil:NilClass (NoMethodError)=0A from D:/ruby=
/usr/lib/ruby/1.9.1/dl/import.rb:118:in `extern'=0A from test.rb:5:i=
n `<module:Win32>'=0A from test.rb:3:in `<main>'=0A=0Amachine is Win=
XP=0Aruby 1.9.1 (2008-10-28 revision 19983) [i386-mswin32_80]=0A=0Acan anyb=
ody suggest a better signature ?=0A=0AAston=0A=0A=0A
 
G

Gordon Thiesfeld

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, sin= ce 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=3DVS.85).aspx

reading sparse, old, and cryptic documentation I came up with this snippe= t,
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 siz= e)"
end

buff =3D 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': undefin= ed
method `sym' for nil:NilClass (NoMethodError)
=A0 =A0 =A0 =A0from D:/ruby/usr/lib/ruby/1.9.1/dl/import.rb:118:in `exter= n'
=A0 =A0 =A0 =A0from test.rb:5:in `<module:Win32>'
=A0 =A0 =A0 =A0from test.rb:3:in `<main>'

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']=3D'bar'
=3D> "bar"
irb(main):002:0> `echo %FOO%`
=3D> "bar\n"
irb(main):003:0>
 
A

Aston

=0A=0A=0A=0A----- Original Message ----=0A> From: Aston <blackapache512-tic=
(e-mail address removed)>=0A> To: Ruby Talk <[email protected]>; Ruby Core <ruby=
(e-mail address removed)>=0A> Sent: Fri, 2 July, 2010 9:07:00 PM=0A> Subject: [r=
uby-core:30996] Ruby/DL problem=0A> =0A> Hello,=0A> =0A> I have very little=
experience with dl. But I want to use it to =0A> call SetEnvironmentVariab=
le() from Win32. I could use FFI but cannot, since =0A>this =0A>=0A> scrip=
t will be supplied to a customer where it is difficult to install.=0A> =0A=
SetEnvironmentVariable on =0A> MSDN http://msdn.microsoft.com/en-us/libra=
ry/ms686206(v=3DVS.85).aspx=0A> =0A> reading sparse, old, and cryptic docu=
mentation I came up with this snippet, =0A> which of course does not work=
=0A> =0A> require 'dl/import'=0A> =0A> module Win32=0A> extend DL::Importe=
r=0A> extern "int SetEnvironmentVariableA( char* lpName, char* lpValue)"=
=0A> extern "int GetEnvironmentVariableA( char* lpName, char* lpValue, int=
size)"=0A> end=0A> =0A> buff =3D Array.new( 256, 0).pack("c*")=0A> Win32.=
GetEnvironmentVariableA( "ENV_VAR", buff, 255)=0A> puts buff.unpack("a*")=
=0A> =0A> this is the error i get=0A> D:/ruby/usr/lib/ruby/1.9.1/dl/import=
rb:194:in `import_function': undefined =0A> method `sym' for nil:NilClass=
(NoMethodError)=0A> from D:/ruby/usr/lib/ruby/1.9.1/dl/import.rb:=
118:in `extern'=0A> from test.rb:5:in `<module:Win32>'=0A> =
from test.rb:3:in `<main>'=0A> =0A> machine is WinXP=0A> ruby 1.9.1 (200=
8-10-28 revision 19983) [i386-mswin32_80]=0A> =0A> can anybody suggest a b=
etter signature ?=0A> =0A> Aston=0A> =0A> =0A=0A=0Ahmm, nobody here using =
Ruby.DL ?=0A=0A=0A
 
L

Luis Lavena

----- Original Message ----
From: Aston <[email protected]>
To: Ruby Talk <[email protected]>; Ruby Core <[email protected]>
Sent: Fri, 2 July, 2010 9:07:00 PM
Subject: [ruby-core:30996] Ruby/DL problem

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
MSDNhttp://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/importrb: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  `<module:Win32>'
        from test.rb:3:in  `<main>'
machine is WinXP
ruby 1.9.1 (2008-10-28 revision 19983)  [i386-mswin32_80]
can anybody suggest a better signature  ?

hmm, nobody here using Ruby.DL ?

Is not DL the problem, but the version you're using. revision 19983 is
*way* older than latest released one:

Pathlevel 378 was revision 26273

Lurking in the NEWS and ChangeLog pointed several changes in DL.

Trying your example with latest RubyInstaller 1.9.1 release:

C:\Users\Luis\Desktop>ruby -v t.rb
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]
C:/Users/Luis/Tools/Ruby/ruby-1.9.1-p378-i386-mingw32/lib/ruby/1.9.1/
dl/import.rb:116: warning: instance variable @type_alias not
initialized
C:/Users/Luis/Tools/Ruby/ruby-1.9.1-p378-i386-mingw32/lib/ruby/1.9.1/
dl/import.rb:194: warning: instance variable @handler not initialized
C:/Users/Luis/Tools/Ruby/ruby-1.9.1-p378-i386-mingw32/lib/ruby/1.9.1/
dl/import.rb:194:in `import_function': undefined method `sym' for
nil:NilClass (NoMethodError)
from C:/Users/Luis/Tools/Ruby/ruby-1.9.1-p378-i386-mingw32/lib/
ruby/1.9.1/dl/import.rb:118:in `extern'
from t.rb:5:in `<module:Win32>'
from t.rb:3:in `<main>'

Which brings the question, why bother on doing this way when Ruby does
it out of the box?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top