Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/windirstat/lua-winreg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/win_privileges.c')
-rw-r--r--src/win_privileges.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/win_privileges.c b/src/win_privileges.c
new file mode 100644
index 0000000..b6abf88
--- /dev/null
+++ b/src/win_privileges.c
@@ -0,0 +1,48 @@
+#include <windows.h>
+
+
+BOOL win_setprivilege(const TCHAR * privilege, BOOL bEnable, HANDLE hToken){
+ TOKEN_PRIVILEGES tpPrevious;
+ TOKEN_PRIVILEGES tp;
+ DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
+ LUID luid;
+ HANDLE hTokenUsed;
+
+ // if no token specified open process token
+ if(hToken == 0){
+ if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hTokenUsed)){
+ return FALSE;
+ }
+ }else hTokenUsed = hToken;
+
+ if (!LookupPrivilegeValue(NULL, privilege, &luid )){
+ if (hToken == 0)
+ CloseHandle(hTokenUsed);
+ return FALSE;
+ }
+
+ tp.PrivilegeCount = 1;
+ tp.Privileges[0].Luid = luid;
+ tp.Privileges[0].Attributes = 0;
+
+ if (!AdjustTokenPrivileges(hTokenUsed, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &tpPrevious, &cbPrevious)){
+ if (hToken == 0)
+ CloseHandle(hTokenUsed);
+ return FALSE;
+ }
+
+ tpPrevious.PrivilegeCount = 1;
+ tpPrevious.Privileges[0].Luid = luid;
+
+ if (bEnable)
+ tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
+ else
+ tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
+
+ if (!AdjustTokenPrivileges(hTokenUsed, FALSE, &tpPrevious, cbPrevious, NULL, NULL)){
+ if (hToken == 0)
+ CloseHandle(hTokenUsed);
+ return FALSE;
+ }
+ return TRUE;
+}