Archived

This topic is now archived and is closed to further replies.

Homer Simpson

Setting 2 privilegies

Recommended Posts

Hi i was playing with my VC++ and then i Tryied to do a program that shuts the computer down, then i do it later i try to set 2 privilegies at the same function i did it and it compiled linked, but when it executes i got a error , I tryied to debug i´ve searched on MSDN but I can´t figure out what is wrong can anyone help me??? VC++ 6.0 WinXP Pro
    
#include <windows.h>

const char g_szClassName[] = "myWindowClass";
BOOL MySystemShutdown( LPTSTR lpMsg );
// Step 4: the Window Procedure

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:

            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:

            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
MySystemShutdown(NULL);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


BOOL MySystemShutdown( LPTSTR lpMsg )
{
  HANDLE hToken;              // handle to process token 

   TOKEN_PRIVILEGES tkp;       // pointer to token structure 


   BOOL fResult;               // system shutdown flag 

 
   // Get the current process token handle so we can get shutdown 

   // privilege. 

 
 if (!OpenProcessToken(GetCurrentProcess(), 
      TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
    return FALSE; 
 
   // Get the LUID for shutdown privilege. 

 
  LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
        &tkp.Privileges[0].Luid); 
  LookupPrivilegeValue(NULL, SE_DEBUG_NAME, 
       &tkp.Privileges[1].Luid); 
 
   tkp.PrivilegeCount = 2;  // one privilege to set    

 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
 tkp.Privileges[1].Attributes = SE_PRIVILEGE_ENABLED;
 
   // Get shutdown privilege for this process. 

 
  AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
     (PTOKEN_PRIVILEGES) NULL, 0); 
 
   // Cannot test the return value of AdjustTokenPrivileges. 

 
  if (GetLastError() != ERROR_SUCCESS) 
     return FALSE; 
 
   // Display the shutdown dialog box and start the countdown. 

 
   fResult = InitiateSystemShutdown( 
      NULL,    // shut down local computer 

      lpMsg,   // message for user

      20,      // time-out period 

      FALSE,   // ask user to close apps 

      TRUE);   // reboot after shutdown 

 
   if (!fResult) 
      return FALSE; 
 
   // Disable shutdown privilege. 

 
  tkp.Privileges[0].Attributes = 0; 
  tkp.Privileges[1].Attributes = 0; 
  AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), 
     (PTOKEN_PRIVILEGES) NULL, 0); 
 
   return TRUE; 
} 

    
Thanx in advance [edit] Oh the SE_DEBUG_NAME can be any other privilegie [edited by - Homer Simpson on July 14, 2002 12:15:42 AM]

Share this post


Link to post
Share on other sites