Add Support for Safely Elevating Administrator Privileges (#1036)
This commit is contained in:
parent
6a914f7016
commit
c2fba6f651
4 changed files with 120 additions and 5 deletions
|
|
@ -12,6 +12,13 @@ target_link_libraries(dxgi-info
|
|||
${PLATFORM_LIBRARIES})
|
||||
target_compile_options(dxgi-info PRIVATE ${SUNSHINE_COMPILE_OPTIONS})
|
||||
|
||||
add_executable(elevator elevator.cpp)
|
||||
set_target_properties(elevator PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(elevator
|
||||
shell32
|
||||
${PLATFORM_LIBRARIES})
|
||||
target_compile_options(elevator PRIVATE ${SUNSHINE_COMPILE_OPTIONS})
|
||||
|
||||
add_executable(audio-info audio.cpp)
|
||||
set_target_properties(audio-info PROPERTIES CXX_STANDARD 17)
|
||||
target_link_libraries(audio-info
|
||||
|
|
@ -36,3 +43,4 @@ target_link_libraries(ddprobe
|
|||
d3d11
|
||||
${PLATFORM_LIBRARIES})
|
||||
target_compile_options(ddprobe PRIVATE ${SUNSHINE_COMPILE_OPTIONS})
|
||||
|
||||
|
|
|
|||
70
tools/elevator.cpp
Normal file
70
tools/elevator.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include <Windows.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @file elevator.cpp
|
||||
* @brief A simple command line utility to run a given command with administrative privileges.
|
||||
*
|
||||
* This utility helps run a command with administrative privileges on Windows
|
||||
* by leveraging the ShellExecuteExW function. The program accepts a command
|
||||
* and optional arguments, then attempts to run the command with elevated
|
||||
* privileges. If successful, it waits for the process to complete and
|
||||
* returns the exit code of the launched process.
|
||||
*
|
||||
* @example
|
||||
* To run the command prompt with administrative privileges, execute the following command:
|
||||
* elevator.exe cmd
|
||||
*
|
||||
* To run a command, such as 'ipconfig /flushdns', with administrative privileges, execute:
|
||||
* elevator.exe cmd /C "ipconfig /flushdns"
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
// Check if the user provided at least one argument (the command to run)
|
||||
if(argc < 2) {
|
||||
std::cout << "Usage: " << argv[0] << " <command> [arguments]" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Convert the command and arguments from char* to wstring for use with ShellExecuteExW
|
||||
std::wstring command = std::wstring(argv[1], argv[1] + strlen(argv[1]));
|
||||
std::wstring arguments;
|
||||
|
||||
// Concatenate the remaining arguments (if any) into a single wstring
|
||||
for(int i = 2; i < argc; ++i) {
|
||||
arguments += std::wstring(argv[i], argv[i] + strlen(argv[i]));
|
||||
if(i < argc - 1) {
|
||||
arguments += L" ";
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare the SHELLEXECUTEINFOW structure with the necessary information
|
||||
SHELLEXECUTEINFOW info = { sizeof(SHELLEXECUTEINFOW) };
|
||||
info.lpVerb = L"runas"; // Request elevation
|
||||
info.lpFile = command.c_str();
|
||||
info.lpParameters = arguments.empty() ? nullptr : arguments.c_str();
|
||||
info.nShow = SW_SHOW;
|
||||
info.fMask = SEE_MASK_NOCLOSEPROCESS; // So we can wait for the process to finish
|
||||
|
||||
// Attempt to execute the command with elevation
|
||||
if(!ShellExecuteExW(&info)) {
|
||||
std::cout << "Error: ShellExecuteExW failed with code " << GetLastError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Wait for the launched process to finish
|
||||
WaitForSingleObject(info.hProcess, INFINITE);
|
||||
|
||||
DWORD exitCode = 0;
|
||||
|
||||
// Retrieve the exit code of the launched process
|
||||
if(!GetExitCodeProcess(info.hProcess, &exitCode)) {
|
||||
std::cout << "Error: GetExitCodeProcess failed with code " << GetLastError() << std::endl;
|
||||
}
|
||||
|
||||
// Close the process handle
|
||||
CloseHandle(info.hProcess);
|
||||
|
||||
// Return the exit code of the launched process
|
||||
return exitCode;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue