Administration Rights for my Application by Token
-
Hi, because of the CRegistry class and some other classes in my Application I need to have Administration rights for it. I want to have the same effect like set the "Run programm as Administrator" checkbox in the compatibility options of my Application, but in my Visual C++ code. I`m using the Visual Studio 2008. I discovered, that AdjustTokenPrivileges is the function I probably need, but I don`t understand how to use it. I get my token using the following lines:
HANDLE hToken = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE,&hToken)) {
AfxMessageBox("Error");
}CloseHandle(hToken);
But I don`t know how to set the Privileges to the token, and I don`t understand how to set it for my whole application? Can someone help me? Thank you!
-
Hi, because of the CRegistry class and some other classes in my Application I need to have Administration rights for it. I want to have the same effect like set the "Run programm as Administrator" checkbox in the compatibility options of my Application, but in my Visual C++ code. I`m using the Visual Studio 2008. I discovered, that AdjustTokenPrivileges is the function I probably need, but I don`t understand how to use it. I get my token using the following lines:
HANDLE hToken = NULL;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE,&hToken)) {
AfxMessageBox("Error");
}CloseHandle(hToken);
But I don`t know how to set the Privileges to the token, and I don`t understand how to set it for my whole application? Can someone help me? Thank you!
Try it :) However, I hold it in a service code, that is started by SYSTEM user... (Die Funktion aktiviert eine existierende Begünstigung, verteilt aber nichts, wenn diese für den Benutzer nicht vorhanden ist)
BOOL AdjustTokenPrivileges(LPCTSTR lpName)
{
BOOL bRes = TRUE;HANDLE hToken;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
LUID luid;
if (LookupPrivilegeValue(NULL, lpName, &luid)) {
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;if (!::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN\_PRIVILEGES), NULL, NULL)) { bRes = FALSE; } } else { bRes = FALSE; } CloseHandle(hToken);
} else {
bRes = FALSE;
}return bRes;
}void Usage()
{
AdjustTokenPrivileges(SE_RESTORE_NAME);
AdjustTokenPrivileges(SE_BACKUP_NAME);
...
}virtual void BeHappy() = 0;