-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add Support for Safely Elevating Administrator Privileges #1036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1da92fb
Adds support for elevating processes that require it. It will retry t…
Nonary 8bf2882
Removing debug code
Nonary 33b7f1f
Placing code inside IF block now, since it wa previously doing work r…
Nonary 65c22a2
Updated the code to utilize an elevation tool and added explanation t…
Nonary 74ae9ac
I guess these are the lint fixes?
Nonary 7151745
Adding line break
Nonary cce7bbe
Removing trailing space
Nonary 449801c
Adding documentation
Nonary 619b9ca
removing unused import
Nonary 7f17670
sorting the headers in alphebetical order
Nonary 45983bb
Improving the grammar of this code comment.
Nonary 8aef4e2
Shifting it closer alphabetically instead of chronologically
Nonary b5b01f2
Changing null to nullptr
Nonary 8e9979c
Remove extra line
Nonary 0beb940
Final touches for nullptr changes
Nonary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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[]) { | ||
Nonary marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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; | ||
ReenigneArcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.