Last active
May 14, 2023 10:14
-
-
Save RedGreenBlue09/beb75798eac3f7883848dd0a54304a2e to your computer and use it in GitHub Desktop.
Tiny Windows API program to elevate app as Administrator.
This file contains 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
#include <Windows.h> | |
void __stdcall start() { | |
WCHAR* sCmd = GetCommandLineW(); | |
/* Skip this program name */ | |
if (*sCmd == L'\"') { | |
++sCmd; | |
while ((*sCmd != L'\"') && (*sCmd != L'\0')) ++sCmd; | |
while (*sCmd) ++sCmd; | |
} else { | |
while ((*sCmd != L' ') && (*sCmd != L'\0')) ++sCmd; | |
} | |
while (*sCmd == L' ') ++sCmd; | |
if (*sCmd == L'\0') ExitProcess(0); | |
/* Make target program name string */ | |
WCHAR* sProgName; | |
if (*sCmd == L'\"') { | |
++sCmd; | |
sProgName = sCmd; | |
while ((*sCmd != L'\"') && (*sCmd != L'\0')) ++sCmd; | |
} else { | |
sProgName = sCmd; | |
while ((*sCmd != L' ') && (*sCmd != L'\0')) ++sCmd; | |
} | |
if (*sCmd != L'\0') { | |
*sCmd = L'\0'; | |
++sCmd; | |
} | |
while (*sCmd == L' ') ++sCmd; | |
// sCmd is now args. | |
/* Elevate the target program using ShellExecuteW */ | |
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); | |
SHELLEXECUTEINFOW SEI = { 0 }; | |
SEI.cbSize = sizeof(SHELLEXECUTEINFOW); | |
SEI.fMask = SEE_MASK_DEFAULT; | |
SEI.hwnd = NULL; | |
SEI.lpVerb = L"runas"; | |
SEI.lpFile = sProgName; | |
SEI.lpParameters = sCmd; | |
SEI.lpDirectory = NULL; | |
SEI.nShow = SW_NORMAL; | |
BOOL bShlExecRet = ShellExecuteExW(&SEI); | |
INT MainProcRet = bShlExecRet ? 0 : GetLastError(); | |
CoUninitialize(); | |
ExitProcess(MainProcRet); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment