Shutdown problem

G

Guest

Hi everybody
I want to shutdown my computer with my program.
I searched in MSDN to find a .NET method that can do that but did'nt find
anything but ExitWindowsEx Api.
I write following codes to execute this api but every time i use it, I see
an error message.
Error:1314 " A required privilege is not held by the client. ".
Please tell me what did I wrong.
Thanks in advance.

[DllImport("user32.dll", SetLastError=true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool
DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength,
IntPtr PreviousState, IntPtr ReturnLength);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32
DesiredAccess,
ref IntPtr TokenHandle);

[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll")]
static extern bool LookupPrivilegeValue(string lpSystemName, string
lpName, ref LUID lpLuid);
//-----------------------------------------------

struct LUID
{
public long LowPart;
public long HighPart;
}

struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}

struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public long Attributes;
}

//-------------------------------------------------

private bool ExitWindows(uint m_Flag)
{
const int TOKEN_QUERY = 0x0008;
const int TOKEN_ADJUST_PRIVILEGE = 0x0020;
const long SE_PRIVILEGE_ENABLED = 0x00000002L;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();

if( !OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGE |
TOKEN_QUERY, ref hToken) )
return false;

if( !LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tkp.Privileges.Luid) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());

tkp.PrivilegeCount = 1;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;

if( !AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero,
IntPtr.Zero) )
return false;

if( !ExitWindowsEx(m_Flag, 0) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());
return true;
}
 
W

Willy Denoyette [MVP]

Mohammad-Reza said:
Hi everybody
I want to shutdown my computer with my program.
I searched in MSDN to find a .NET method that can do that but did'nt find
anything but ExitWindowsEx Api.
I write following codes to execute this api but every time i use it, I see
an error message.
Error:1314 " A required privilege is not held by the client. ".
Please tell me what did I wrong.
Thanks in advance.

[DllImport("user32.dll", SetLastError=true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool
DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength,
IntPtr PreviousState, IntPtr ReturnLength);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32
DesiredAccess,
ref IntPtr TokenHandle);

[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll")]
static extern bool LookupPrivilegeValue(string lpSystemName, string
lpName, ref LUID lpLuid);
//-----------------------------------------------

struct LUID
{
public long LowPart;
public long HighPart;
}

struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}

struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public long Attributes;
}

//-------------------------------------------------

private bool ExitWindows(uint m_Flag)
{
const int TOKEN_QUERY = 0x0008;
const int TOKEN_ADJUST_PRIVILEGE = 0x0020;
const long SE_PRIVILEGE_ENABLED = 0x00000002L;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();

if( !OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGE |
TOKEN_QUERY, ref hToken) )
return false;

if( !LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref
tkp.Privileges.Luid) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());

tkp.PrivilegeCount = 1;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;

if( !AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero,
IntPtr.Zero) )
return false;

if( !ExitWindowsEx(m_Flag, 0) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());
return true;
}


long in .NET is 64 bit, so, all long's in this part of the code should be
int's!

struct LUID
{
public long LowPart;
public long HighPart;
}

struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}

struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public long Attributes;
}

Willy.
 
E

Elp

Hi everybody
I want to shutdown my computer with my program.
I searched in MSDN to find a .NET method that can do that but did'nt find
anything but ExitWindowsEx Api.

You can also use WMI to do that using the Win32Shutdown
method of the Win32_OperatingSystem class. You need to have WMI installed
on the machine running your application though. It's built-in under Windows
ME, 2000 and above but needs to be installed separately under Windows 98
and NT 4.
I write following codes to execute this api but every time i use it, I see
an error message.
Error:1314 " A required privilege is not held by the client. ".
Please tell me what did I wrong.

You can also use the WindowsController library that already contains all
the needed interop declaration if you want to be sure that you have made no
mistakes in your code:
http://www.mentalis.org/soft/class.qpx?id=7

If you still get this error using this library, then your application may
not have the rights to shutdown your computer. Others may be more
knowledgeable than me to find a solution for this privilege problem.
 
N

Nurchi BECHED

Hey,

This could be a solution to your problem
(If you don't use Windows XP, you might have to look for this)
ControlPanel->Administrative tools->Microsoft .NET Framework 1.1 Wizards
Just play with it...
There was an example from Microsoft website
showing how .NET security works:
You have a simple application that creates
a text file on your C: drive.
When you compile and run the program locally,
it works just fine.
Now, put it on any website, download and run,
there is a security exception.

Your program for some reason might not have enough
priveleges to shutdown your PC

Just play with "Trust an assembly"
Choose your program and set the appropriate permissions...

I hope this helps.
Good luck.


Hi everybody
I want to shutdown my computer with my program.
I searched in MSDN to find a .NET method that can do that but did'nt find
anything but ExitWindowsEx Api.
I write following codes to execute this api but every time i use it, I
see
an error message.
Error:1314 " A required privilege is not held by the client. ".
Please tell me what did I wrong.
Thanks in advance.

[DllImport("user32.dll", SetLastError=true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool
DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 BufferLength,
IntPtr PreviousState, IntPtr ReturnLength);

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32
DesiredAccess,
ref IntPtr TokenHandle);

[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll")]
static extern bool LookupPrivilegeValue(string lpSystemName, string
lpName, ref LUID lpLuid);
//-----------------------------------------------

struct LUID
{
public long LowPart;
public long HighPart;
}

struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges;
}

struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public long Attributes;
}

//-------------------------------------------------

private bool ExitWindows(uint m_Flag)
{
const int TOKEN_QUERY = 0x0008;
const int TOKEN_ADJUST_PRIVILEGE = 0x0020;
const long SE_PRIVILEGE_ENABLED = 0x00000002L;
const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();

if( !OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGE |
TOKEN_QUERY, ref hToken) )
return false;

if( !LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref
tkp.Privileges.Luid) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());

tkp.PrivilegeCount = 1;
tkp.Privileges.Attributes = SE_PRIVILEGE_ENABLED;

if( !AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero,
IntPtr.Zero) )
return false;

if( !ExitWindowsEx(m_Flag, 0) )
throw new SystemException(Marshal.GetLastWin32Error().ToString());
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top