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,

From: Daniel Berger <djberg96@gmail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Win32API, CreateProcess and environment
Date: Sun, 30 Apr 2006 02:13:06 +0900

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?

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 Heesob