Win32API, CreateProcess and environment

Hi all,

Ruby 1.8.4

I’m working on a pure Ruby Process.create method. What I have below
seems to work, except that if I attempt to pass an environment string,
it causes the app to die instantly. What am I doing wrong?

require ‘Win32API’

params = ‘LPLLLLLLPP’
CreateProcess = Win32API.new(‘kernel32’,‘CreateProcess’, params, ‘I’)

def create(hash={})
env = 0
if hash[‘environment’]
env = hash[‘environment’].split(File::PATH_SEPARATOR) << nil
env = env.pack(‘p*’).unpack(‘L’).first
end

startinfo = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
startinfo = startinfo.pack('LLLLLLLLLLLLSSLLLL')
procinfo  = [0,0,0,0].pack('LLLL')

CreateProcess.call(
   0, "notepad", 0, 0, 0, 0, env, 0, startinfo, procinfo
)

return procinfo[8,4].unpack('L').first # pid

end

p create() # ok
p create(‘environment’=>“PATH=C:\;LIB=C:\lib” # boom!

What have I goofed up here?

Thanks,

Dan

Hi,

I’m working on a pure Ruby Process.create method. What I have below seems
to work, except that if I attempt to pass an environment string, it causes
the app to die instantly. What am I doing wrong?

The ‘notepad’ process requires ‘SystemRoot’ environment variable.
And the environment block must be separated by “\0” and end with “\0\0”.

Here is modified code:

require ‘Win32API’

params = ‘LPLLLLLLPP’

CreateProcess = Win32API.new(‘kernel32’,‘CreateProcess’, params, ‘I’)

def create(hash={})
env = 0
if hash[‘environment’]
env = hash[‘environment’].split(File::PATH_SEPARATOR) <<
“SystemRoot=#{ENV[‘SystemRoot’]}” << “\0”
env = [env.join("\0")].pack(‘p*’).unpack(‘L’).first

end

startinfo = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
startinfo = startinfo.pack('LLLLLLLLLLLLSSLLLL')
procinfo  = [0,0,0,0].pack('LLLL')


CreateProcess.call(
   0, "notepad", 0, 0, 0, 0, env, 0, startinfo, procinfo
)

return procinfo[8,4].unpack('L').first # pid

end

p create() # ok
p create(‘environment’=>“PATH=C:\;LIB=C:\lib”)

Regards,

Park H.