Forum: Ruby Win32API, CreateProcess and environment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-05-03 19:09
(Received via mailing list)
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
E51d56251ec4affafe85ee9367228965?d=identicon&s=25 Park Heesob (Guest)
on 2006-05-03 19:10
(Received via mailing list)
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 Heesob
This topic is locked and can not be replied to.