To run an executable as administrator using the SeIncreaseWorkingSetPrivilege privilege, you typically need to enable the privilege in your process and then use it to launch the executable with elevated permissions. However, SeIncreaseWorkingSetPrivilege is not directly related to running executables as administrator—it's a privilege that allows a process to increase its working set size, which is not typically used for elevation.
Instead, you should use the SeDebugPrivilege or SeImpersonatePrivilege (if applicable) or rely on standard elevation methods like ShellExecute with runas or CreateProcessWithLogonW. Here’s how you can properly elevate a process:
Method 1: Using ShellExecute with runas (Simplest)
#include <windows.h>
#include <shellapi.h>
int main() {
ShellExecute(NULL, "runas", "C:\\Path\\To\\Your\\Executable.exe", NULL, NULL, SW_SHOWNORMAL);
return 0;
}
This will prompt the UAC (User Account Control) dialog to request admin rights.
Method 2: Using CreateProcessWithLogonW (Advanced)
If you need to run a process as a different user (including admin), you can use:
#include <windows.h>
#include <stdio.h>
int main() {
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessWithLogonW(
L"Administrator", // Username (or "SYSTEM" for services)
L"Domain", // Domain (or NULL for local)
L"Password", // Password (or NULL if no password)
LOGON_WITH_PROFILE, // Logon flags
NULL, // Application name (NULL if in command line)
L"C:\\Path\\To\\Your\\Executable.exe", // Command line
CREATE_NO_WINDOW, // Process creation flags
NULL, // Environment block
NULL, // Current directory
&si, // STARTUPINFO
&pi // PROCESS_INFORMATION
)) {
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
} else {
printf("Failed to start process. Error: %d\n", GetLastError());
}
return 0;
}
Note: This requires the password of the admin account, which is not secure.
Method 3: Enabling Privileges (If Needed for Other Reasons)
If you still need to enable SeIncreaseWorkingSetPrivilege (or other privileges), you can do so like this:
#include <windows.h>
#include <stdio.h>
void EnablePrivilege(LPCTSTR privilegeName) {
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
printf("OpenProcessToken failed: %d\n", GetLastError());
return;
}
LookupPrivilegeValue(NULL, privilegeName, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0)) {
printf("AdjustTokenPrivileges failed: %d\n", GetLastError());
CloseHandle(hToken);
return;
}
CloseHandle(hToken);
}
int main() {
EnablePrivilege(SE_INCREASE_WORKING_SET_NAME);
// Now you can use the privilege, but it won't help with elevation.
return 0;
}
Important Notes:
- UAC Must Be Enabled: Modern Windows requires UAC to be enabled for elevation.
- Security Considerations: Hardcoding admin credentials is unsafe. Use secure methods like
ShellExecute with runas.
- Alternatives: If you're writing a service, consider using
CreateProcessAsUser with a service account.
For most cases, Method 1 (ShellExecute with runas) is the simplest and most secure way to elevate a process.