如何将system()函数的输出重定向到变量

How do I redirect the output of a system() function to a variable?

本文关键字:输出 重定向 变量 函数 system      更新时间:2023-10-16

我有这个代码:

system("echo %username%");

我想将它的结果重定向到一个变量,比如uname

我该怎么做?

我知道WinAPI,但我想这样做。

快速、丑陋和肮脏的方法是将输出重定向到一个文件,然后读取该文件。

system("echo %username% > someFile.txt");

更详细的方法是通过以下命令行使用CreateProcess API:cmd.exe /c echo %username%

API允许您指定自定义标准输入和标准输出。您可以为标准输出创建一个管道,如下所示:

HANDLE g_hChildStd_OUT_Wr = NULL;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
saAttr.bInheritHandle = TRUE; 
saAttr.lpSecurityDescriptor = NULL; 
// Create a pipe for the child process's STDOUT. 
// 
if ( !CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) ) return -1;

然后在CreateProcess API中使用此管道。类似这样的东西:

TCHAR szCmdline[]=TEXT("cmd.exe /c echo %username%");
PROCESS_INFORMATION piProcInfo; 
STARTUPINFO siStartInfo;
// Set up members of the PROCESS_INFORMATION structure. 
// 
memset( &piProcInfo, 0, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure. 
// This structure specifies the STDIN and STDOUT handles for redirection.
//
memset( &siStartInfo, 0, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO); 
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
// Create the child process. 
//    
bSuccess = CreateProcess(NULL, 
      szCmdline,     // command line 
      NULL,          // process security attributes 
      NULL,          // primary thread security attributes 
      TRUE,          // handles are inherited 
      0,             // creation flags 
      NULL,          // use parent's environment 
      NULL,          // use parent's current directory 
      &siStartInfo,  // STARTUPINFO pointer 
      &piProcInfo);  // receives PROCESS_INFORMATION 

然后从管道中读取类似的内容:

DWORD dwRead, dwWritten; 
CHAR chBuf[BUFSIZE]; 
BOOL bSuccess = FALSE;
HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
for (;;) 
{ 
      bSuccess = ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
      if( ! bSuccess || dwRead == 0 ) break; 
}

进程将异步运行,因此您需要知道进程何时终止并进行正确的清理。因此,为了使这项工作发挥作用,这里有很多细节需要学习。

一个完整的例子可以在这里找到:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx

如果目标只是获取用户名,您是否考虑过getenvgetenv( "username" )将直接返回。

否则,如果你有更多的事情要做,并且希望结果在一个文件中。。。传递给system的字符串将传递给命令解释器,因此您可以在cmd.exe中的命令行中执行的任何操作都将在system中起作用:文件重定向、管道到另一个进程等。