Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Замена для hidcon, функциональный аналог + возврат PID дочернего процесса в STDOUT, без блокировки основного потока
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <algorithm>
- #include <iterator>
- #include <string>
- #include <sstream>
- #include <vector>
- bool fileExist(const char* path) {
- if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path) && GetLastError() == ERROR_FILE_NOT_FOUND) {
- return false;
- }
- return true;
- }
- std::vector<std::string> getSystemPath() {
- std::vector<std::string> result;
- char* path = getenv("PATH");
- std::stringstream ss(path);
- std::string token;
- while (std::getline(ss, token, ';')) {
- result.push_back(token);
- }
- return result;
- }
- std::string getAppName(int argc, char* argv[]) {
- if (argc <= 0) {
- return "";
- }
- std::string appName = argv[0];
- const size_t i = appName.find_last_of("\\/");
- if (i != std::string::npos) {
- appName.erase(0, i + 1);
- }
- return appName;
- }
- std::string getTargetPath(int argc, char* argv[]) {
- if (argc <= 1) {
- return "";
- }
- if (fileExist(argv[1])) {
- return argv[1];
- }
- std::string targetPath = argv[1];
- std::vector<std::string> systemPath = getSystemPath();
- for (size_t i = 0; i < systemPath.size(); i++) {
- std::string newPath = systemPath[i] + "\\" + targetPath;
- if (fileExist(newPath.c_str())) {
- return newPath;
- }
- }
- return argv[1];
- }
- std::string getTargetArgs(int argc, char* argv[]) {
- if (argc <= 2) {
- return "";
- }
- std::string targetArgs = argv[2];
- for (int i = 3; i < argc; i++) {
- targetArgs += " ";
- targetArgs += argv[i];
- }
- return targetArgs;
- }
- bool recursion(const std::string &appName, const std::string &targetPath) {
- std::string targetName = targetPath;
- const size_t i = targetName.find_last_of("\\/");
- if (i != std::string::npos) {
- targetName.erase(0, i + 1);
- }
- if (appName == targetName) {
- return true;
- }
- return false;
- }
- bool spawnProcess(const std::string& targetPath, const std::string& targetArgs) {
- // additional information
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- // set the size of the structures
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi, sizeof(pi));
- std::string command = "\"" + targetPath + "\" " + targetArgs;
- // start the program up
- if (CreateProcess(NULL, // the path
- const_cast<LPSTR>(command.c_str()), // Command line
- NULL, // Process handle not inheritable
- NULL, // Thread handle not inheritable
- FALSE, // Set handle inheritance to FALSE
- CREATE_NO_WINDOW | DETACHED_PROCESS, // No creation flags
- NULL, // Use parent's environment block
- NULL, // Use parent's starting directory
- &si, // Pointer to STARTUPINFO structure
- &pi // Pointer to PROCESS_INFORMATION structure
- )) {
- fprintf(stdout, "%lu\n", pi.dwProcessId);
- fflush(stdout);
- // Close process and thread handles.
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- return true;
- } else {
- DWORD errorMessageID = GetLastError();
- LPSTR messageBuffer = nullptr;
- size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)& messageBuffer, 0, NULL);
- std::string message(messageBuffer, size);
- LocalFree(messageBuffer);
- fprintf(stderr, "%lu\nProblem with spawning process: %s\n", errorMessageID, message.c_str());
- return false;
- }
- }
- int main(int argc, char* argv[]) {
- std::string appName = getAppName(argc, argv);
- if (appName.empty()) {
- fprintf(stderr, "0\nOoops, can't determine app name, something really wrong here!\n");
- return 1;
- }
- std::string targetPath = getTargetPath(argc, argv);
- if (targetPath.empty()) {
- fprintf(stderr, "0\nTarget app path empty, nothing to start.\n");
- return 1;
- }
- if (recursion(appName, targetPath)) {
- fprintf(stderr, "0\nPossible recursion detected, aborting.\n");
- return 1;
- }
- std::string targetArgs = getTargetArgs(argc, argv);
- if (!spawnProcess(targetPath, targetArgs)) {
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement