2

出力を表示し、F4が終了するのを待つコンソールアプリケーションを読み取るGUIアプリがありますが、次の方法でプロセスを起動できました。

p.StartInfo.FileName = "consoleapp.exe";
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false; 
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(ConsoleOutputHandler);
p.Start();
p.BeginOutputReadLine();

そして私はF4を使用して送ることができます:

PostMessage(p.MainWindowHandle, (uint)WM_KEYUP, (IntPtr) Keys.F4, (IntPtr) 0x3E0001 );

StandardOutputを次のようにリダイレクトするまで、すべてが正常に機能します。

p.StartInfo.RedirectStandardOutput = true;

そうすれば、PostMessageは引き続きイベントを送信します(Spy ++によってチェックされます)が、コンソールアプリはそれを認識しません。

「RedirectStandardInput」を変更しても、何の進展もありませんでした。

何かご意見は?

4

2 に答える 2

0

あなたはおそらくこれに使用したくないでしょうPostMessageWriteConsoleInputターゲットはコンソールアプリケーションであるため、次の行に沿って、p/invokeを介してWin32APIを使用して入力バッファにキーを書き込む必要があります。

p.StartInfo.RedirectStandardInput = true ;
// the rest of your code 

int written ;
var record = new KEY_INPUT_RECORD
{
    EventType = KEY_EVENT,
    bKeyDown  = true,
    wVirtualKeyCode = VK_F4,
    // maybe set other members, use ReadConsoleInput 
    // to get a sample and hard-code it
    // you might even use a byte array representation
    // of the input record, since you only need one key
} ;

WriteConsoleInput (((FileStream)p.StandardInput.BaseStream).SafeFileHandle,
    ref record, 1, out written) ;
于 2013-01-25T15:05:37.097 に答える