# Win32API, CreateProcess and environment

**URL:** <https://www.ruby-forum.com/t/win32api-createprocess-and-environment/94440>\
**Category:** Ruby\
**Created:** [May 3, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/win32api-createprocess-and-environment/94440 "2006-05-03T19:09:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [May 3, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/win32api-createprocess-and-environment/94440/1 "2006-05-03T19:09:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [May 3, 2006, 7:10pm UTC](https://www.ruby-forum.com/t/win32api-createprocess-and-environment/94440/2 "2006-05-03T19:10:43Z")

</div>

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.
