c++ - 为什么一个 HANDLE 不适用于 WriteConsoleInput,但适用于 WriteFile?

标签 c++ c winapi user32

所以我尝试修改 Microsoft 提供的代码( here )以使用 WriteConsoleInput,而不是 WriteFile,但它说句柄无效(我敢打赌这真的很愚蠢),就像句柄的方式一样是最初创建的或者什么的。

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

WriteConsoleInput

WriteFile

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

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

ConsoleRedir on GitHub

请注意,我需要子应用程序使用 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;
}

最佳答案

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

您需要使用 GetStdHandle 中的句柄以获得合适的 handle 。

WriteConsoleInput 仅适用于控制台的直接句柄,不适用于重定向管道或类似的管道。

关于c++ - 为什么一个 HANDLE 不适用于 WriteConsoleInput,但适用于 WriteFile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17650336/

相关文章:

c - 在设备固件中支持 WinUSB

C 字符串指针初始化自身然后取消初始化自身?

c - 为什么我的 else 语句仍然会触发,即使我之前的 if 语句已经触发了为什么会发生这种情况

c++ - 如何检测win32应用程序中显示器的热插拔?

winapi - 在哪里可以找到 RD 网关 API 的示例代码?

C++,FindFirstFile() 的输出

c++ - 未初始化的数据和 memcpy

c++ - 影响C++中初始化的顺序

windows - 用于在 Windows 上监视单个文件 IO 性能的 API

c++ - 有没有办法在 Eclipse 中找到匹配的#if、#else、#elif 和#endif?