繁体   English   中英

需要帮助从 Windows 上的计时器程序严重重定向输入和输出

[英]Need help badly redirecting input and output from timer program on Windows

我希望程序能够重定向父进程和子进程的输入和输出。 例如,

parent "child.exe > child_output.txt" > parent_output.txt

家长叫孩子。 子输出到 child_output,父输出到父输出。

下面的示例代码应该可以工作,但不能。 为方便起见,我在字符串“ToBeRun”中对子项的重定向运算符进行了硬编码,而不是将其作为参数传递,就像在主程序中所做的那样。

如果在命令行中没有将参数传递给父级,则子级输出将正确重定向到 child_output.txt。 但是,如果您在命令行上重定向父级的参数,例如通过调用 parent.exe > parent_output.txt,则不会重定向父级输出。 parent_output.txt 已创建,但在程序完成时为空。

#include <windows.h>

// COPY PASTED FROM Martins Mozeiko on Handmade Network:
// https://hero.handmade.network/forums/code-discussion/t/94
// these shouldn't have to be in here, but hey! What can you do?
#pragma function(memset)
void *memset(void *dest, int c, size_t count)
{
    char *bytes = (char *)dest;
    while (count--)
    {
        *bytes++ = (char)c;
    }
    return dest;
}

#pragma function(memcpy)
void *memcpy(void *dest, void *src, size_t count)
{
    char *dest8 = (char *)dest;
    char *src8 = (char *)src;
    while (count--)
    {
        *dest8++ = *src8++;
    }
    return dest;
}
/* Based on this msdn article
 * https://msdn.microsoft.com/en-us/library/windows/desktop   /ms682512(v=vs.85).aspx
 */
int main(void)
{
    // init structures for child process
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // start child process and wait for it to finish
#if 0
    char *ToBeRun = "/C child.exe > child_result.txt";
#else
    // for your convenience, so you don't have to compile a sample child.exe
    char *ToBeRun = "/C dir > child_result.txt";
#endif
    if( !CreateProcess("C:\\WINDOWS\\system32\\cmd.exe", // Module name
                       ToBeRun,     // Command line
                       NULL,        // Process handle not inheritable
                       NULL,        // Thread handle not inheritable
                       FALSE,       // Set handle inheritance to FALSE
                       0,           // No creation flags
                       NULL,        // Use parent's environment block
                       NULL,        // Use parent's starting directory 
                       &si,         // Pointer to STARTUPINFO structure
                       &pi)  // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        // create process failed
        return 1;
    }
    WaitForSingleObject(pi.hProcess, INFINITE);

    // print parent output string
    char String[] = "This is the parent's output";
    int StringLength = sizeof(String) / sizeof(String[0]);
    DWORD CharsWritten;
    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
    WriteConsole(Handle, String, StringLength, &CharsWritten, NULL);

    // close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return 0;
}

// required for compiling without CRT
void mainCRTStartup(void)
{
    int Result = main();
    ExitProcess(Result);
}

我回答了我自己的问题:简单地说,WriteConsole() 在写入重定向文件时失败。

因此,在写入重定向文件时,您需要使用恰当命名的 WriteFile()。

WriteFile() 适用于重定向输出和常规控制台输出,正如Harry Johnston所帮助指出的那样,因此您可以在这两种情况下都使用它。 这就是我所做的。

或者,尤其是在使用 Unicode 时,您可以参考MSDN 的建议并通过调用 GetConsoleMode 检查 HANDLE 是文件句柄还是控制台句柄。 如果 GetConsoleMode 返回 0,则它不是控制台。

你为什么想做这个? WriteConsole 与 WriteFile 不同,它是一个可以成为 unicode 或 ASCII 函数的宏,具体取决于是否定义了 UNICODE。

从 MSDN

ReadFile 和 WriteFile 函数或 ReadConsole 和 WriteConsole 函数使应用程序能够读取控制台输入并将控制台输出作为字符流写入。 ReadConsole 和 WriteConsole 的行为与 ReadFile 和 WriteFile 完全一样,除了它们可以用作宽字符函数(其中文本参数必须使用 Unicode)或用作 ANSI 函数(其中文本参数必须使用来自 > Windows 字符集的字符)。 需要维护一组源以支持 Unicode 或 ANSI 字符集的应用程序应使用 ReadConsole 和 WriteConsole。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM