執行cmd命令,獲取輸出結果。
方式一:
void GetConsoleResult()
{
FILE* pPipe = _popen("cmd /c tasklist |findstr /i \"cmd.exe\"", "r");
std::string strOut;
if (pPipe)
{
/* Read pipe until end of file, or an error occurs. */
char psBuffer[10] = {0};
while (fgets(psBuffer, 10, pPipe))
{
strOut.append(psBuffer);
printf(psBuffer);
}
/* Close pipe and print return value of pPipe. */
if (feof(pPipe))
{
printf("\nProcess returned %d\n", _pclose(pPipe));
}
else
{
printf("Error: Failed to read the pipe to the end.\n");
}
}
}