返回由win32ole模块打开的CMD HIDE实例的exitcode?

时间:2012-03-04 16:11:07

标签: ruby win32ole

我知道在Windows中打开一个CMD实例并获取返回代码

puts %x[Tasklist /v | Find "%tmp:~0,30%" >NUL]
response = $?.exitstatus

有效。

但是现在我需要打开一个CMD的隐藏实例,我只知道用Win32ole模块做这个,而函数“exitstatus”给我发错了。 我不知道为什么......

请帮助获取该实例的退出代码或其他方式 打开(并获取exitcode)隐藏实例。

require 'win32ole'
shell = WIN32OLE.new('Shell.Application')

shell.ShellExecute('CMD', '/K Tasklist /v | Find "%tmp:~0,30%" >NUL',
'', '', 0)
response = $?.exitstatus
    if response == 0
        puts "hola"
        end
  

未定义的方法`exitstatus'为nil:NilClass
  NoMethodError

1 个答案:

答案 0 :(得分:0)

谢谢你2

我在尝试另一种更有效的方法时解决了这个问题:

require 'win32/api'
include Win32

# Callback example - Enumerate windows
EnumWindows     = API.new('EnumWindows', 'KP', 'L', 'user32')
GetWindowText   = API.new('GetWindowText', 'LPI', 'I', 'user32')
EnumWindowsProc = API::Callback.new('LP', 'I'){ |handle, param|
  buf = "\0" * 200
  GetWindowText.call(handle, buf, 200);

  if (!buf.index(param).nil?)
    puts "window was found: handle #{handle}"
    0 # stop looking after we find it
  else
    1
  end
}

EnumWindows.call(EnumWindowsProc,'title here')

再见