Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Administration Rights for my Application by Token

Administration Rights for my Application by Token

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpcsharpvisual-studiotutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joschwenk666
    wrote on last edited by
    #1

    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!

    E 1 Reply Last reply
    0
    • J Joschwenk666

      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!

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      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;

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups