0

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

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

这样可行。

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

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

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

nil 的未定义方法“exitstatus”:NilClass
NoMethodError

4

1 回答 1

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, '这里的标题')

再见

于 2012-03-07T01:02:34.657 回答