Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <tchar.h>
- #include <windows.h>
- #pragma warning(disable : 4800)
- #define BUFSIZE 4096
- HANDLE g_hChildStd_OUT_Rd = NULL;
- HANDLE g_hChildStd_OUT_Wr = NULL;
- HANDLE g_hChildStd_ERR_Rd = NULL;
- HANDLE g_hChildStd_ERR_Wr = NULL;
- PROCESS_INFORMATION CreateChildProcess(char szCmdline[])//Create a child process
- {
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- bool bSuccess = FALSE;
- ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
- ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
- siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
- bSuccess = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
- CloseHandle(g_hChildStd_OUT_Wr);
- if(!bSuccess)
- {
- exit(1);
- }
- return piProcInfo;
- }
- 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
- {
- DWORD dwRead;
- CHAR chBuf[BUFSIZE];
- bool bSuccess = FALSE;
- std::string out = "";
- for(;;)
- {
- bSuccess=ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
- if(!bSuccess || dwRead == 0)
- {
- break;
- }
- std::string s(chBuf, dwRead);
- out += s;
- }
- return out;
- }
- int main(int argc, char *argv[])
- {
- std::string command;
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
- if(!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0))
- {
- exit(1);
- }
- if(!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
- {
- exit(1);
- }
- PROCESS_INFORMATION piProcInfo = CreateChildProcess("cmd.exe /c dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux | findstr /R /C:\"tat\"");
- command = ReadFromPipe(piProcInfo);
- } std::cout<<command<<std::endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement