ウィンドウアプリから非表示のコンソールウィンドウを開く。(その2)
CreateNoWindowで非表示のコンソールウィンドウを開いて、それをAttachConsole()するサンプル。
vbc /t:winexe sample2.vb
Imports System.Diagnostics
Imports System.Threading
Public Class Class1
Private Declare Function AttachConsole Lib "kernel32" (dwProcessId As Integer) As Integer
Public Shared Sub Main()
Dim oProcess As New Process()
oProcess.StartInfo.FileName = "cmd"
oProcess.StartInfo.UseShellExecute = False
oProcess.StartInfo.CreateNoWindow = True
oProcess.StartInfo.RedirectStandardInput = True
oProcess.StartInfo.RedirectStandardOutput = True
oProcess.Start()
oProcess.StandardOutput.ReadLine()
AttachConsole(oProcess.Id)
oProcess.StandardInput.Close()
oProcess.WaitForExit()
MsgBox(CreateObject("WScript.Shell").Exec("fc.exe").StdErr.ReadAll())
End Sub
End Class
CreateNoWindowで作った非表示のコンソールウィンドウはShowWindow()で再表示できないようです。
コンソールアプリでは、WaitForInputIdle()が使えません。
なので、代わりに、StandardOutput.ReadLine()で待ちます。
CMD.EXEを終了するために、StandardInput.Close()します。
« ウィンドウアプリから非表示のコンソールウィンドウを開く。 | トップページ | ウィンドウアプリから非表示のコンソールウィンドウを開く。(その3) »