gpt4 book ai didi

c++ - 如何在 C++ 文件中保存执行的输出命令行

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:51 26 4
gpt4 key购买 nike

我有这段代码用于在 C++ 中执行 CMD 行

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}

// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // 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
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}

我想在文件中保存执行的输出。但是如何呢?

最佳答案

在 CreateProcess 中,您传递一个 STARTUPINFO 结构。您可以在 si.dwFlags 中设置 STARTF_USESTDHANDLES,然后使用有效的文件描述符填充 hStdInput、hStdOutput 和 hStdError 字段,特别是 hStdOutput 应该是先前打开的文件的句柄(由成功的 CreateFile 返回),然后它将接收启动进程的标准输出。

编辑:

这是一个刻薄的答案,因为它需要做更多的工作才能让它发挥作用:您需要使用正确的 SECURITY_ATTRIBUTES 创建该文件,并且必须在 CreateProcess 中将 Set handle inheritance 设置为 TRUE .所以这样做也是纯粹主义者的噩梦。

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags |=STARTF_USESTDHANDLES ;
si.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
si.hStdError=GetStdHandle(STD_ERROR_HANDLE);
SECURITY_ATTRIBUTES sa;
ZeroMemory( &sa, sizeof(sa) );
sa.nLength=sizeof(sa);
sa.bInheritHandle=TRUE;
si.hStdOutput=CreateFile ("log.txt", GENERIC_READ|GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
ZeroMemory( &pi, sizeof(pi) );

if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
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
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle (si.hStdOutput);
}

关于c++ - 如何在 C++ 文件中保存执行的输出命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996502/

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