Skip to content

Instantly share code, notes, and snippets.

@tfl
Created June 20, 2013 09:36
Show Gist options
  • Save tfl/5821496 to your computer and use it in GitHub Desktop.
Save tfl/5821496 to your computer and use it in GitHub Desktop.
Elevate.exe is a small win32 console utility for Windows Vista or Windows 7/8 to help start other utilities with elevated rights but without having to start them from a batchfile
/*
*
* Torsten Flammiger, 2012
* (C) AS IS
*
*/
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
void print_help()
{
cout << "*****************************************************************" << endl;
cout << "elevate.exe, v1.0 - (C) 2012, Torsten Flammiger (tfl@netfg.net)" << endl;
cout << "*****************************************************************" << endl;
cout << "" << endl;
cout << "Elevate.exe is a small win32 console utility for Windows Vista" << endl;
cout << "or Windows 7/8 to help start other utilities with elevated rights" << endl;
cout << "but without having to start them from a batchfile (where you can" << endl;
cout << "simply right click and start the batch with administrative rights)." << endl;
cout << "Just confirm the UAC prompt and you are done..." << endl;
cout << "" << endl;
cout << "Usage:" << endl;
cout << "elevate.exe <program [with optional arguments]>" << endl;
cout << "" << endl;
cout << "Example:" << endl;
cout << "elevate.exe REG ADD HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System /v EnableLUA /t REG_DWORD /d 1 /f" << endl;
cout << "" << endl;
cout << "Note: dont use a double backslash when working with the registry or path names." << endl;
}
std::wstring to_wstring(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
int main(int argc, const char* argv[])
{
int Result = 0;
if(argc == 1)
{
print_help();
return Result;
}
// create a std::string to hold the commandline params
stringstream args;
args << "/C ";
// exclude the program itself from the list of options
for(int i=1; i<argc; i++) {
args << argv[i] << " ";
}
// convert it into std::wstring
wstring params = to_wstring(args.str());
SHELLEXECUTEINFO ShExecInfo;
memset(&ShExecInfo, 0, sizeof(SHELLEXECUTEINFO));
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = _T("runas");
ShExecInfo.lpFile = _T("cmd.exe");
//ShExecInfo.lpParameters = _T("/C REG ADD HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System /v EnableLUA /t REG_DWORD /d 0 /f");
ShExecInfo.lpParameters = params.c_str();
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
wcout << "Executing cmd.exe with command line: " << params << endl;
if (ShellExecuteEx(&ShExecInfo) == FALSE)
{
Result = -1; // Failed to execute process
}
return Result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment