std::wstring As_Last_N_Chars_Removed(std::wstring str, unsigned int const& number_of_times){ for (unsigned int i = 0; i < number_of_times; ++i){ if (!str.empty()){ str.pop_back(); }else{ return str; } } return str; }
int DeleteFolder(std::wstring path) { if (path.back() == '\\') path.pop_back(); path.push_back('\0'); SHFILEOPSTRUCT fos = {0}; fos.wFunc = FO_DELETE; fos.pFrom = path.c_str(); fos.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; return SHFileOperation(&fos); }
int CPython::ExecFunc(const std::vector<std::wstring> &argv, std::wstring &err, HANDLE hfile) { std::vector<std::wstring>::const_iterator itr, eitr; itr = argv.cbegin(); eitr = argv.cend(); err = *itr; if (argv.size() <= 0) { err = text("No python command function found"); return 1; } err.push_back(text('(')); for (++itr; itr != eitr; ++itr) { err.append(*itr); err.push_back(text(',')); } if (err.back() == text(',')) err.pop_back(); err.push_back(text(')')); Addtolist(1,1,text("Execute Function: %s"),err.c_str()); int id = 0; FILE *pfile = nullptr; DWORD count = 0; PyObject *poldout, *polderr, *pnewout, *pnewerr; if (DuplicateHandle ( GetCurrentProcess(), hfile, GetCurrentProcess(), &hfile, 0, false, DUPLICATE_SAME_ACCESS )) { id = open_osfhandle((intptr_t)hfile, _O_WRONLY); pfile = fdopen(id,"w"); setvbuf(pfile,nullptr,_IONBF,1024); poldout = PySys_GetObject("stdout"); polderr = PySys_GetObject("stderr"); pnewout = PyFile_FromFile(pfile, "logger", "w", nullptr); pnewerr = PyFile_FromFile(pfile, "logger", "w", nullptr); PySys_SetObject("stdout", pnewout); PySys_SetObject("stderr", pnewerr); } else poldout = polderr = pnewout = pnewerr = nullptr; std::wstring_convert<std::codecvt_utf8_utf16<wchar>> cvt; std::string str = cvt.to_bytes(err); std::size_t pos = str.find(text('.')); if (pos != std::string::npos) { std::string mod = str.substr(0, pos); PyObject* dest = PyImport_ImportModule(mod.c_str()); PyObject* main = PyImport_AddModule("__main__"); PyObject_SetAttrString(main, mod.c_str(), dest); } str.insert(0, "print "); int irslt = PyRun_SimpleString(str.c_str()); if (irslt != 0) err = text("Internal error that PyRun_SimpleString fail"); else err = text("Execute python function successfully .."); if (pnewout != nullptr) PySys_SetObject("stdout", poldout); if (pnewerr != nullptr) PySys_SetObject("stderr", polderr); if (pfile != nullptr) fclose(pfile); return irslt; }