我有一个GUI应用程序,它读取显示输出的控制台应用程序并等待F4退出,我已设法启动该过程:
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”没有取得任何进展。
有什么想法吗?
答案 0 :(得分:1)
我做到了!
我在我的app开始运行:
AllocConsole();
uint codepage = GetACP();
SetConsoleOutputCP(codepage);
IntPtr handleconsole = new IntPtr();
handleconsole = GetConsoleWindow();
然后使用..
创建流程p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = false;
p.StartInfo.RedirectStandardError = true;
这为我的应用程序创建了一个控制台,在这之后,每个进程都开始继承该控制台,因此即使你调用ShowWindow来隐藏它也很容易读取stdout。
之后,我创建了一个INPUT_RECORD来发送F4键:
inHandle = GetStdHandle(STD_INPUT_HANDLE);
record[0].EventType = KEY_EVENT;
record[0].KeyEvent.bKeyDown = true;
record[0].KeyEvent.dwControlKeyState = 0;
record[0].KeyEvent.wRepeatCount = 1;
record[0].KeyEvent.wVirtualKeyCode = VirtualKeys.F4;
record[0].KeyEvent.wVirtualScanCode = MapVirtualKey(VK_F4, MAPVK_VK_TO_VSC);
record[1].EventType = KEY_EVENT;
record[1].KeyEvent.bKeyDown = false;
record[1].KeyEvent.dwControlKeyState = 0;
record[1].KeyEvent.wRepeatCount = 1;
record[1].KeyEvent.wVirtualKeyCode = VirtualKeys.F4;
record[1].KeyEvent.wVirtualScanCode = MapVirtualKey(VK_F4, MAPVK_VK_TO_VSC);
WriteConsoleInput(inHandle, record, 1, out written);
答案 1 :(得分:0)
您可能不希望使用PostMessage
。由于您的目标是控制台应用程序,因此您应该使用Win32 API WriteConsoleInput
通过p / invoke将键写入其输入缓冲区,其行如下:
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) ;