473,395 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Shutdown problem

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().ToStri ng());

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().ToStri ng());
return true;
}
Nov 16 '05 #1
3 3285

"Mohammad-Reza" <Mo**********@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com...
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().ToStri ng());

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().ToStri ng());
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.
Nov 16 '05 #2
Elp
On Wed, 15 Dec 2004 11:39:02 -0800, Mohammad-Reza wrote:
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.
Nov 16 '05 #3
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.
On Wed, 15 Dec 2004 11:39:02 -0800, Mohammad-Reza
<Mo**********@discussions.microsoft.com> wrote:
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().ToStri ng());

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().ToStri ng());
return true;
}


--
Regards,
Nurchi BECHED
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jonathan Heath | last post by:
Hi all, I have created an ASP script that enters data into an Access Database. My problem is that I'd like this script to run when the computer is shutdown or the user logs off. I think...
7
by: Lynn | last post by:
I'm running Oracle 8.1.7.4.1 on W2K and have the services for my oracle instances set to automatically startup and shutdown with the W2K server. Unfortunately, neither is working. Nothing gets...
0
by: Allan Bredahl | last post by:
Hi All I am trying to construct an application that is able to cancel a machine shutdown, reboot or logoff. And after performing some stuff to perform the original shutdown order :...
8
by: Bill Sonia | last post by:
I've written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that for OnShutDown, once my...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
6
by: carbon_dragon | last post by:
Ok, so here is the problem. I'm working on a headless server program implemented as a .NET C# Console project. There is a UPS mounted to this server (though not a windows compliant UPS). I can only...
2
by: Paul Steele | last post by:
Some time ago I tracked down the code for detecting the shutdown event within a C# program. I tested it, it worked, and I moved on. However, I just discovered that the code is no longer working,...
3
by: Chris Knievel | last post by:
Hi, i am trying to save some data in a excel spreadsheet, whenever the programm shuts down. Mostly this happens when a user is logging off or shuts the computer down. im am using the Private Sub...
2
by: Sin Jeong-hun | last post by:
I created a windows form application. It has a Threading.Timer and when the timer ticks it does some work and show a popup window. The problem is that while this program is running if the user...
3
by: IdleBrain | last post by:
Gurus, I am trying to delay Windows Shutdown/Restart to perfrom cleanup and I am using the following code: protected override void WndProc(ref Message ex)
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.