Win32API, CreateProcess and environment

From: Park Heesob [mailto:phasis68@hotmail.com]
Sent: Saturday, April 29, 2006 8:07 PM
To: ruby-talk ML
Subject: Re: Win32API, CreateProcess and environment

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.

D'oh!

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

Works great, thanks.

Dan

This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

ยทยทยท

-----Original Message-----