child_output.txt" > parent_output.txt parent 称呼 ch-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:10:04 25 4
gpt4 key购买 nike

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

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

parent 称呼 child 。子输出到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's advice并通过调用 GetConsoleMode 检查 HANDLE 是否是文件句柄或控制台句柄。如果 GetConsoleMode 返回 0,则它不是控制台。

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

From MSDN :

The ReadFile and WriteFile functions, or the ReadConsole and WriteConsole functions, enable an application to read console input and write console output as a stream of characters. ReadConsole and WriteConsole behave exactly like ReadFile and WriteFile except that they can be used either as wide-character functions (in which text arguments must use Unicode) or as ANSI functions (in which text arguments must use characters from the> Windows character set). Applications that need to maintain a single set of sources to support either Unicode or the ANSI character set should use ReadConsole and WriteConsole.

关于c - 需要帮助在 Windows 上严重重定向计时器程序的输入和输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41916416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com