2012-03-04 63 views
0

我知道在窗戶打開CMD的一個實例,並獲得返回代碼返回由win32ole模塊打開的CMD的HIDE實例的exitcode?

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

的作品。

但是現在我需要打開一個CMD的隱藏實例,我只知道用Win32ole模塊來做,而funcion「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 

未定義的方法`退出狀態的零:NilClass
NoMethodError

+0

你是什麼意思是「隱藏的CMD的實例」? – 2012-03-04 17:28:40

+0

你沒有定義'response',也許你打算使用'response = shell.ShellExecute(...)'。 – Koraktor 2012-03-05 22:28:36

回答

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.c所有(EnumWindowsProc,「標題此處」)

再見