Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <shellapi.h>
- #include <shlobj.h>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <stdexcept>
- #include <stdio.h>
- std::string exec(const char* cmd)
- {
- char buffer[128];
- std::string result = "";
- FILE* pipe = _popen(cmd, "r");
- if(!pipe) throw std::runtime_error("_popen() failed!");//popen for POSIX
- try
- {
- while(!feof(pipe))
- {
- if(fgets(buffer, 128, pipe) != NULL)
- {
- result += buffer;
- }
- }
- }
- catch(...)
- {
- _pclose(pipe);
- throw;
- }
- _pclose(pipe);
- return result;
- }
- LPWSTR stringToLPWSTR(const std::string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
- bool fileExist(const char *fileName)
- {
- std::ifstream infile(fileName);
- return infile.good();
- }
- int main(void)
- {
- std::string command;
- 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);
- }
- //command = " /C C:\\Windows\\SysWOW64\\bash.exe -c ls > test.txt";
- //ShellExecuteW(NULL, L"open", L"cmd.exe", stringToLPWSTR(command), NULL, SW_HIDE);
- std::cout<<exec("cmd.exe /C C:\\Windows\\SysWOW64\\bash.exe -c ls")<<std::endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement