2

所以我试图修改微软提供的代码(这里)以使用WriteConsoleInput,而不是WriteFile,但它说句柄是无效的(我打赌这真的很愚蠢),比如最初创建句柄的方式或其他东西.

所以我的问题是,WriteConsoleInput 所需的句柄和 WriteFile 的句柄有什么区别?

写控制台输入

写文件

我猜它与 CreateFile 创建的 HANDLE 的权限标志与 CreateProcess/CreatePipe/DuplicateHandle 进程创建的继承句柄相比有关。

我决定如果你能看到问题会更容易,所以这是我的完整解决方案(使用 Visual Studio 2012)包括子应用程序和父应用程序。

GitHub 上的 ConsoleRedir

需要说明的是,我需要子应用程序使用 ReadConsoleInput,这是我沮丧的根源。

原始方法:

/////////////////////////////////////////////////////////////////////// 
// GetAndSendInputThreadOrig
// Thread procedure that monitors the console for input and sends input
// to the child process through the input pipe.
// This thread ends when the child application exits.
// Original from http://support.microsoft.com/kb/190351
/////////////////////////////////////////////////////////////////////// 
DWORD WINAPI GetAndSendInputThreadOrig(LPVOID lpvThreadParam)
{
    CHAR read_buff[256];
    DWORD nBytesRead,nBytesWrote;
    HANDLE hPipeWrite = (HANDLE)lpvThreadParam;

    // Get input from our console and send it to child through the pipe.
    while (bRunThread)
    {
        if(!ReadConsole(hStdIn,read_buff,1,&nBytesRead,NULL))
            DisplayError("ReadConsole");

        read_buff[nBytesRead] = '\0'; // Follow input with a NULL.

        if (!WriteFile(hPipeWrite,read_buff,nBytesRead,&nBytesWrote,NULL))
        {
            if (GetLastError() == ERROR_NO_DATA)
                break; // Pipe was closed (normal exit path).
            else
                DisplayError("WriteFile");
        }
    }

    return 1;
}

我的修改版本(必须构建击键):

/////////////////////////////////////////////////////////////////////// 
// GetAndSendInputThread
// Thread procedure that monitors the console for input and sends input
// to the child process through the input pipe.
// This thread ends when the child application exits.
/////////////////////////////////////////////////////////////////////// 
DWORD WINAPI GetAndSendInputThread(LPVOID lpvThreadParam)
{
    CHAR read_buff[256];
    DWORD nBytesWrote;
    HANDLE hPipeWrite = (HANDLE)lpvThreadParam;

    // Get input from our console and send it to child through the pipe.
    while (bRunThread)
    {
        INPUT_RECORD inputRecords[4];
        // Build a keyboard event, press '?' and then press RETURN
        inputRecords[0].EventType = KEY_EVENT;
        inputRecords[0].Event.KeyEvent.bKeyDown = TRUE;
        inputRecords[0].Event.KeyEvent.uChar.UnicodeChar = '?';
        inputRecords[1].EventType = KEY_EVENT;
        inputRecords[1].Event.KeyEvent.bKeyDown = FALSE;
        inputRecords[1].Event.KeyEvent.uChar.UnicodeChar = '?';
        inputRecords[2].EventType = KEY_EVENT;
        inputRecords[2].Event.KeyEvent.bKeyDown = TRUE;
        inputRecords[2].Event.KeyEvent.dwControlKeyState = 0;
        inputRecords[2].Event.KeyEvent.uChar.UnicodeChar = '\r';
        inputRecords[2].Event.KeyEvent.wRepeatCount = 1;
        inputRecords[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
        inputRecords[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);
        inputRecords[3].EventType = KEY_EVENT;
        inputRecords[3].Event.KeyEvent.bKeyDown = FALSE;
        inputRecords[3].Event.KeyEvent.dwControlKeyState = 0;
        inputRecords[3].Event.KeyEvent.uChar.UnicodeChar = '\r';
        inputRecords[3].Event.KeyEvent.wRepeatCount = 1;
        inputRecords[3].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
        inputRecords[3].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);


        if (!WriteConsoleInput(hPipeWrite,inputRecords,sizeof(inputRecords) / sizeof(*inputRecords),&nBytesWrote))
        {
            if (GetLastError() == ERROR_NO_DATA)
                break; // Pipe was closed (normal exit path).
            else
                DisplayError("WriteConsoleInput");
        }
    }

    return 1;
}
4

1 回答 1

3

WriteConsoleInput要求句柄是“控制台输入缓冲区的句柄”。handle(您问题的链接页面中输入参数的描述中的第一句话)。

您需要使用手柄 fromGetStdHandle来获得合适的手柄。

WriteConsoleInput只能在控制台的直接句柄上工作,而不是重定向管道或类似的。

于 2013-07-15T09:09:42.397 回答