Module: Win32Open4::Kernel32

Extended by:
Kernel32
Included in:
Kernel32, ProcessHandler
Defined in:
lib/procreate/win32/open4.rb,
lib/procreate/win32/open4.rb

Overview

Now add wrapper functions

Defined Under Namespace

Classes: Kernel32Error

Constant Summary collapse

API =

:nodoc:

Windows::API
ERROR_SUCCESS =
0x00
FORMAT_MESSAGE_FROM_SYSTEM =
0x1000
FORMAT_MESSAGE_ARGUMENT_ARRAY =
0x2000
WAIT_OBJECT_0 =
0
WAIT_TIMEOUT =
0x102
WAIT_ABANDONED =
128
WAIT_ABANDONED_0 =
WAIT_ABANDONED
WAIT_FAILED =
0xFFFFFFFF
NORMAL_PRIORITY_CLASS =
0x00000020
STARTUP_INFO_SIZE =
68
PROCESS_INFO_SIZE =
16
SECURITY_ATTRIBUTES_SIZE =
12
HANDLE_FLAG_INHERIT =
1
HANDLE_FLAG_PROTECT_FROM_CLOSE =
2
STARTF_USESHOWWINDOW =
0x00000001
STARTF_USESTDHANDLES =
0x00000100

Class Method Summary collapse

Class Method Details

.close_handle(handle) ⇒ Object



155
156
157
# File 'lib/procreate/win32/open4.rb', line 155

def close_handle(handle)
  raise_last_error! if CloseHandle(handle).zero?
end

.create_pipeObject

returns read and write handle



144
145
146
147
148
149
# File 'lib/procreate/win32/open4.rb', line 144

def create_pipe
  read_handle, write_handle = Array.new(2) { [0].pack('I') }
  sec_attrs = [SECURITY_ATTRIBUTES_SIZE, 0, 1].pack('III')
  raise_last_error! if CreatePipe(read_handle, write_handle, sec_attrs, 0).zero?
  [read_handle.unpack('I')[0], write_handle.unpack('I')[0]]
end

.create_process(command, stdin, stdout, stderr) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/procreate/win32/open4.rb', line 159

def create_process(command, stdin, stdout, stderr)
  startupInfo = [
    STARTUP_INFO_SIZE,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW,
    0, 0, 0,
    stdin, stdout, stderr
  ].pack('IIIIIIIIIIIISSIIII')
  processInfo = [0, 0, 0, 0].pack('IIII')
  command = command + 0.chr
  raise_last_error! if CreateProcessA(0, command, 0, 0, 1, 0, 0, 0, startupInfo, processInfo).zero?
  # hProcess, hThread, dwProcessId, dwThreadId
  processInfo.unpack('LLLL')
end

.peek_named_pipe(hFile) ⇒ Object



188
189
190
191
192
193
# File 'lib/procreate/win32/open4.rb', line 188

def peek_named_pipe(hFile)
  available = [0].pack('I')
  # FIXME? as above
  return -1 if PeekNamedPipe(hFile, 0, 0, 0, available, 0).zero?
  available.unpack('I')[0]
end

.raise_last_error!Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/procreate/win32/open4.rb', line 130

def raise_last_error!
  errorCode = GetLastError()
  if errorCode != ERROR_SUCCESS
    msg = ' ' * 255
    msgLength = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, '', errorCode, 0, msg, 255, '')
    msg.delete! 0.chr
    msg.strip!
    raise Kernel32Error, msg
  else
    raise Kernel32Error, 'GetLastError returned ERROR_SUCCESS'
  end
end

.read_file(hFile, size = 1024) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/procreate/win32/open4.rb', line 180

def read_file(hFile, size=1024)
  number = [0].pack('I')
  buffer = ' ' * size
  # FIXME? we're masking errors here and just returning an empty string...
  return '' if ReadFile(hFile, buffer, size, number, 0).zero?
  buffer[0...number.unpack('I')[0]]
end

.set_handle_information(handle, flags, value) ⇒ Object



151
152
153
# File 'lib/procreate/win32/open4.rb', line 151

def set_handle_information(handle, flags, value)
  raise_last_error! if SetHandleInformation(handle, flags, value).zero?
end

.write_file(hFile, buffer) ⇒ Object



174
175
176
177
178
# File 'lib/procreate/win32/open4.rb', line 174

def write_file(hFile, buffer)
  written = [0].pack('I')
  raise_last_error! if WriteFile(hFile, buffer, buffer.size, written, 0).zero?
  written.unpack('I')[0]
end