0

I am administrator, so... lol

Ancient Dragon did u use C++ code for that line of code or just run it in cmd? cause my cmd works fine cant get anything working through C++.

0

Maybe you could try out the following program, and post back its output. It might give some clue about what is not working.

#include <windows.h>
#include <stdio.h>

BOOL SetPrivilege
(
    HANDLE hToken,          // access token handle
    LPCTSTR lpszPrivilege,  // name of privilege to enable/disable
    BOOL bSetPrivilege      // to enable or disable privilege
) 
{
    TOKEN_PRIVILEGES tp;
    LUID luid;

    if (!LookupPrivilegeValue( 
        NULL,            // lookup privilege on local system
        lpszPrivilege,   // privilege to lookup 
        &luid ) )        // receives LUID of privilege
    {
        printf("LookupPrivilegeValue error: %lu\n", GetLastError()); 
        return FALSE; 
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;

    if (bSetPrivilege)
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else
        tp.Privileges[0].Attributes = 0;

    if (!AdjustTokenPrivileges(
        hToken, 
        FALSE, 
        &tp, 
        sizeof(TOKEN_PRIVILEGES), 
        (PTOKEN_PRIVILEGES) NULL, 
        (PDWORD) NULL))
    { 
        printf("AdjustTokenPrivileges error: %lu\n", GetLastError()); 
        return FALSE; 
    } 

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    {
        printf("The token does not have the specified privilege.\n");
        return FALSE;
    } 

    printf("Privilege was set ...\n");

    return TRUE;
}

int main()
{
    HANDLE hToken;

    if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES, &hToken))
    {
        printf("OpenProcessToken: error %lu\n" , GetLastError());
        return 0;
    }

    if(SetPrivilege(hToken, "SeShutdownPrivilege", TRUE))
    {
        if(!ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER))
        {
             printf("ExitWindowsEx: error %lu\n" , GetLastError());
        }
        else
        {
              printf("Shutdown initiated ...\n");
        }
    }

    CloseHandle(hToken);

    return 0;
}
1

I think the user will have to be logged in under an Administrative account for that to work.

Thats the thing with vista. Even IF you are an administrator, that doesnt mean you run programs as administrator. Programs always run with the lease privileges required.

All an admin account means on vista with UAC on is that if it needs to, it has the ablity to elevate its privilege level.

E.g take visual studio 2005 for example. On vista you need to physically right click and go "Run as administrator" or else the debiugger and IIS deployment and some other things wont work. Thats becase the program was written before vista so it doesnt know how to ask for an elevation of privileges

Thats why installers etc... come up with a UAC prompt (yes, even if you are an admin user), thats the request to raise thier execution level.

I know that to make it always start with full admin privileges you need to modify the manifest file (thats how i got shutdown in my VB.NET app to work)

app.manifest should be like this:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
                <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
                </requestedPrivileges>
        </security>
    </trustInfo>
</asmv1:assembly>

e.g

http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed-under-windows-7

Votes + Comments
good idea :)
0

I am administrator, so... lol

Ancient Dragon did u use C++ code for that line of code or just run it in cmd? cause my cmd works fine cant get anything working through C++.

I ran it as a c++ program. If it doesn't work for you then probably a difference in the way our computers are set up. For example, I've turned off the User Account Control because I hated all those security questions every time I wanted to do something.,

0

thats what im saying.

With UAC on an admin user is not an admin user all the time. Unless they get a prompt by means of a program asking for an elevation of privileges, it will always run with the lowest rights required. Thats the point of UAC

So unless you right your program correctly, if a user who has UAC on just double clicks it, it wont shut the machine down. It will either have to be modified, or the user will have to right click it , choose run as administrator, and then click OK at the prompts

0

mitrmkar - Red bar while compiling on line:

if(!ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER))

I think it is the compiler?
Anyone else using codeblocks?
It always used to work?
Maybe needs a plugin or something similar?

*RAWR* this is doing my head in :|

0

I am administrator, so... lol
cause my cmd works fine cant get anything working through C++.

Did you get the UAC prompt when you ran the shutdown.exe command? If you did, then you'll need a manifest like jbennet showed.

I think the user will have to be logged in under an Administrative account for that to work.

I think it is rather a Policy Configuration issue, i.e. "Shut down the system" - user right, which is assigned on a per-account/group basis.

0

mitrmkar - Red bar while compiling on line:

if(!ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER))

I think it is the compiler?
Anyone else using codeblocks?
It always used to work?
Maybe needs a plugin or something similar?

*RAWR* this is doing my head in :|

Did your compiler give you an error message? If yes, what was it?

Did you #include <windows.h> ? Does your compiler know where windows.h is installed?

0

mitrmkar - Red bar while compiling on line:

if(!ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_OTHER|SHTDN_REASON_MINOR_OTHER))

I think it is the compiler?
Anyone else using codeblocks?
It always used to work?
Maybe needs a plugin or something similar?

*RAWR* this is doing my head in :|

Maybe your codeblocks is old enough to not to have the following definitions

#ifndef SHTDN_REASON_MAJOR_OTHER
#define SHTDN_REASON_MAJOR_OTHER           0x00000000
#define SHTDN_REASON_MINOR_OTHER           0x00000000
#endif// ndef SHTDN_REASON_MAJOR_OTHER
#ifndef EWX_POWEROFF
#define EWX_POWEROFF           0x00000008
#endif// ndef EWX_POWEROFF

Just add those defines (above main()) and retry.

0

it's telling some error like "SHTDN_REASON_MAJOR_OPERATINGSYSTEM"
is undefined first use this function.
So for this which header file we have to include.
As for knowledge i am working with c++.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.