Using a anonymous pipe to redirect sfc /scannow , but nothing output , Why?

涛 王 76 Reputation points
2021-04-26T08:58:11.997+00:00

I started visual studio with admin and use anonymous pipe to redirect sfc /scannow output to my terminal, result nothing output,

I alse use "ping 12.15.16.45" command to replace the "sfc /scannow" , that OK,

I use CreateProcess and CreatePipe function to redirect output . Using WaitSingleObject to wait process end and ReadFile to read Process output.

demo url link : https://github.com/Huoke/WindowsTools/blob/main/test/outputRedirect.cpp

Windows development Windows API - Win32
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Song Zhu - MSFT 906 Reputation points
    2021-04-27T07:14:01.937+00:00

    I think there are two reasons why there is no output.

    The first is that the time you wait may be too short, causing the pipe to be read before the call is successful, so the output is empty.

    The second reason is that the command returns an array of rows. This results in multiple'\0' characters in the returned buffer, which allows you to truncate subsequent characters when outputting. You can read by character instead Instead of outputting the entire string directly.

    Use: WAIT_OBJECT_0 == WaitForSingleObject(piProcInfo.hProcess, 1000)

    and: for (int i = 0; i < dwRead; i++) cout << buffer[i];

    More reference: “SFC” output redirection formatting issue - Powershell / Batch


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.