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;
}
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;
}