Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <vector>
- #include <windows.h>
- #include <iostream>
- #include <fstream>
- #pragma warning(disable : 4800)
- #define BUFSIZE 4096
- int pipeCounter = -1, procInfCounter = -1;
- std::vector<HANDLE>pipeHandles;
- std::vector<PROCESS_INFORMATION>procInf;
- PROCESS_INFORMATION CreateChildProcess(char szCmdline[], HANDLE hChildStdWr)
- {
- PROCESS_INFORMATION piProcInfo;
- STARTUPINFO siStartInfo;
- bool bSuccess = FALSE;
- ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
- ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
- siStartInfo.cb = sizeof(STARTUPINFO);
- siStartInfo.hStdOutput = hChildStdWr;
- siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
- bSuccess = CreateProcess(NULL, szCmdline, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &siStartInfo, &piProcInfo);
- if(!bSuccess)
- {
- exit(1);
- }
- else
- {
- CloseHandle(hChildStdWr);
- }
- return piProcInfo;
- }
- std::string ReadFromPipe(PROCESS_INFORMATION piProcInfo, HANDLE hChildStdRd)
- {
- DWORD dwRead;
- CHAR chBuf[BUFSIZE];
- bool bSuccess = FALSE;
- std::string out = "";
- for(;;)
- {
- bSuccess=ReadFile(hChildStdRd, chBuf, BUFSIZE, &dwRead, NULL);
- if(!bSuccess || dwRead == 0)
- {
- break;
- }
- std::string s(chBuf, dwRead);
- out += s;
- }
- return out;
- }
- std::string silentCmdResult(char *command, SECURITY_ATTRIBUTES sa)
- {
- std::string commandResult;
- pipeHandles.resize(pipeHandles.size()+2);
- procInf.resize(procInf.size()+1);
- for(int i=0;i<2;i++)
- {
- //pipeHandles.push_back(NULL);
- pipeCounter++;
- }
- if(!CreatePipe(&pipeHandles.at(pipeCounter-1) , &pipeHandles.at(pipeCounter), &sa, 0))
- {
- exit(1);
- }
- if(!SetHandleInformation(pipeHandles.at(pipeCounter-1), HANDLE_FLAG_INHERIT, 0))
- {
- exit(1);
- }
- procInfCounter++;
- procInf.at(procInfCounter) = CreateChildProcess(command, pipeHandles.at(pipeCounter));
- commandResult = ReadFromPipe(procInf.at(procInfCounter), pipeHandles.at(pipeCounter-1));
- CloseHandle(procInf.at(procInfCounter).hProcess);
- CloseHandle(procInf.at(procInfCounter).hThread);
- return commandResult;
- }
- bool fileExist(const char *fileName)
- {
- std::ifstream infile(fileName);
- return infile.good();
- }
- int main(int argc, char *argv[])
- {
- if(!fileExist("C:\\Windows\\SysWOW64\\bash.exe"))
- {
- ShellExecuteW(NULL, L"open", L"cmd.exe", L" /C mklink C:\\Windows\\SysWOW64\\bash.exe C:\\Windows\\System32\\bash.exe", NULL, SW_HIDE);
- }
- SECURITY_ATTRIBUTES sa;
- sa.nLength = sizeof(SECURITY_ATTRIBUTES);
- sa.bInheritHandle = TRUE;
- sa.lpSecurityDescriptor = NULL;
- std::cout<<silentCmdResult("cmd.exe /c dism /online /get-featureinfo /featurename:Microsoft-Windows-Subsystem-Linux | findstr /R /C:\"tat\"", sa)<<std::endl;
- std::cout<<silentCmdResult("C:\\Windows\\SysWOW64\\bash.exe -c ls", sa)<<std::endl;
- std::cout<<silentCmdResult("cmd.exe /c echo lastTest", sa)<<std::endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement