Advertisement
Combreal

subProcPipe01.cpp

Jun 6th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <tchar.h>
  2. #include <windows.h>
  3. #pragma warning(disable : 4800)
  4. #define BUFSIZE 4096
  5. HANDLE g_hChildStd_OUT_Rd = NULL;
  6. HANDLE g_hChildStd_OUT_Wr = NULL;
  7. HANDLE g_hChildStd_ERR_Rd = NULL;
  8. HANDLE g_hChildStd_ERR_Wr = NULL;
  9.  
  10. PROCESS_INFORMATION CreateChildProcess(char szCmdline[])//Create a child process
  11. {
  12.     PROCESS_INFORMATION piProcInfo;
  13.     STARTUPINFO siStartInfo;
  14.     bool bSuccess = FALSE;
  15.     ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
  16.     ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  17.     siStartInfo.cb = sizeof(STARTUPINFO);
  18.     siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
  19.     siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  20.     bSuccess = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
  21.     CloseHandle(g_hChildStd_OUT_Wr);
  22.     if(!bSuccess)
  23.     {
  24.         exit(1);
  25.     }
  26.     return piProcInfo;
  27. }
  28.  
  29. std::string ReadFromPipe(PROCESS_INFORMATION piProcInfo)//Read output from the child process's pipe for STDOUT and write to the parent process's pipe for STDOUT
  30. {
  31.     DWORD dwRead;
  32.     CHAR chBuf[BUFSIZE];
  33.     bool bSuccess = FALSE;
  34.     std::string out = "";
  35.     for(;;)
  36.     {
  37.         bSuccess=ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
  38.         if(!bSuccess || dwRead == 0)
  39.         {
  40.             break;
  41.         }
  42.         std::string s(chBuf, dwRead);
  43.         out += s;
  44.     }
  45.     return out;
  46. }
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.     std::string command;
  51.     SECURITY_ATTRIBUTES sa;
  52.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  53.     sa.bInheritHandle = TRUE;
  54.     sa.lpSecurityDescriptor = NULL;
  55.     if(!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
  56.     {
  57.         exit(1);
  58.     }
  59.     if(!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
  60.     {
  61.         exit(1);
  62.     }
  63.     PROCESS_INFORMATION piProcInfo = CreateChildProcess("cmd.exe /c dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux | findstr /R /C:\"tat\"");
  64.     command = ReadFromPipe(piProcInfo);
  65. }   std::cout<<command<<std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement