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("user 32.dll", SetLastError=tr ue)]
static extern bool ExitWindowsEx(u int uFlags, uint dwReason);
[DllImport("adva pi32.dll", SetLastError=tr ue)]
static extern bool AdjustTokenPriv ileges(IntPtr TokenHandle, bool
DisableAllPrivi leges, ref TOKEN_PRIVILEGE S NewState, UInt32 BufferLength,
IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("adva pi32.dll", SetLastError=tr ue)]
static extern bool OpenProcessToke n(IntPtr ProcessHandle, UInt32
DesiredAccess,
ref IntPtr TokenHandle);
[DllImport("kern el32.dll")]
static extern IntPtr GetCurrentProce ss();
[DllImport("adva pi32.dll")]
static extern bool LookupPrivilege Value(string lpSystemName, string
lpName, ref LUID lpLuid);
//-----------------------------------------------
struct LUID
{
public long LowPart;
public long HighPart;
}
struct TOKEN_PRIVILEGE S
{
public int PrivilegeCount;
public LUID_AND_ATTRIB UTES Privileges;
}
struct LUID_AND_ATTRIB UTES
{
public LUID Luid;
public long Attributes;
}
//-------------------------------------------------
private bool ExitWindows(uin t m_Flag)
{
const int TOKEN_QUERY = 0x0008;
const int TOKEN_ADJUST_PR IVILEGE = 0x0020;
const long SE_PRIVILEGE_EN ABLED = 0x00000002L;
const string SE_SHUTDOWN_NAM E = "SeShutdownPriv ilege";
IntPtr hToken = IntPtr.Zero;
TOKEN_PRIVILEGE S tkp = new TOKEN_PRIVILEGE S();
if( !OpenProcessTok en(GetCurrentPr ocess(), TOKEN_ADJUST_PR IVILEGE |
TOKEN_QUERY, ref hToken) )
return false;
if( !LookupPrivileg eValue(null, SE_SHUTDOWN_NAM E, ref tkp.Privileges. Luid) )
throw new SystemException (Marshal.GetLas tWin32Error().T oString());
tkp.PrivilegeCo unt = 1;
tkp.Privileges. Attributes = SE_PRIVILEGE_EN ABLED;
if( !AdjustTokenPri vileges(hToken, false, ref tkp, 0, IntPtr.Zero,
IntPtr.Zero) )
return false;
if( !ExitWindowsEx( m_Flag, 0) )
throw new SystemException (Marshal.GetLas tWin32Error().T oString());
return true;
} 3 3312
"Mohammad-Reza" <Mo**********@d iscussions.micr osoft.com> wrote in message
news:18******** *************** ***********@mic rosoft.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("user 32.dll", SetLastError=tr ue)] static extern bool ExitWindowsEx(u int uFlags, uint dwReason);
[DllImport("adva pi32.dll", SetLastError=tr ue)] static extern bool AdjustTokenPriv ileges(IntPtr TokenHandle, bool DisableAllPrivi leges, ref TOKEN_PRIVILEGE S NewState, UInt32 BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("adva pi32.dll", SetLastError=tr ue)] static extern bool OpenProcessToke n(IntPtr ProcessHandle, UInt32 DesiredAccess, ref IntPtr TokenHandle);
[DllImport("kern el32.dll")] static extern IntPtr GetCurrentProce ss();
[DllImport("adva pi32.dll")] static extern bool LookupPrivilege Value(string lpSystemName, string lpName, ref LUID lpLuid); //-----------------------------------------------
struct LUID { public long LowPart; public long HighPart; }
struct TOKEN_PRIVILEGE S { public int PrivilegeCount; public LUID_AND_ATTRIB UTES Privileges; }
struct LUID_AND_ATTRIB UTES { public LUID Luid; public long Attributes; }
//-------------------------------------------------
private bool ExitWindows(uin t m_Flag) { const int TOKEN_QUERY = 0x0008; const int TOKEN_ADJUST_PR IVILEGE = 0x0020; const long SE_PRIVILEGE_EN ABLED = 0x00000002L; const string SE_SHUTDOWN_NAM E = "SeShutdownPriv ilege";
IntPtr hToken = IntPtr.Zero; TOKEN_PRIVILEGE S tkp = new TOKEN_PRIVILEGE S();
if( !OpenProcessTok en(GetCurrentPr ocess(), TOKEN_ADJUST_PR IVILEGE | TOKEN_QUERY, ref hToken) ) return false;
if( !LookupPrivileg eValue(null, SE_SHUTDOWN_NAM E, ref tkp.Privileges. Luid) ) throw new SystemException (Marshal.GetLas tWin32Error().T oString());
tkp.PrivilegeCo unt = 1; tkp.Privileges. Attributes = SE_PRIVILEGE_EN ABLED;
if( !AdjustTokenPri vileges(hToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero) ) return false;
if( !ExitWindowsEx( m_Flag, 0) ) throw new SystemException (Marshal.GetLas tWin32Error().T oString()); 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_PRIVILEGE S
{
public int PrivilegeCount;
public LUID_AND_ATTRIB UTES Privileges;
}
struct LUID_AND_ATTRIB UTES
{
public LUID Luid;
public long Attributes;
}
Willy.
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_Operating System 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 WindowsControll er 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.
Hey,
This could be a solution to your problem
(If you don't use Windows XP, you might have to look for this)
ControlPanel->Administrati ve 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**********@d iscussions.micr osoft.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("user 32.dll", SetLastError=tr ue)] static extern bool ExitWindowsEx(u int uFlags, uint dwReason);
[DllImport("adva pi32.dll", SetLastError=tr ue)] static extern bool AdjustTokenPriv ileges(IntPtr TokenHandle, bool DisableAllPrivi leges, ref TOKEN_PRIVILEGE S NewState, UInt32 BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("adva pi32.dll", SetLastError=tr ue)] static extern bool OpenProcessToke n(IntPtr ProcessHandle, UInt32 DesiredAccess, ref IntPtr TokenHandle);
[DllImport("kern el32.dll")] static extern IntPtr GetCurrentProce ss();
[DllImport("adva pi32.dll")] static extern bool LookupPrivilege Value(string lpSystemName, string lpName, ref LUID lpLuid); //-----------------------------------------------
struct LUID { public long LowPart; public long HighPart; }
struct TOKEN_PRIVILEGE S { public int PrivilegeCount; public LUID_AND_ATTRIB UTES Privileges; }
struct LUID_AND_ATTRIB UTES { public LUID Luid; public long Attributes; }
//-------------------------------------------------
private bool ExitWindows(uin t m_Flag) { const int TOKEN_QUERY = 0x0008; const int TOKEN_ADJUST_PR IVILEGE = 0x0020; const long SE_PRIVILEGE_EN ABLED = 0x00000002L; const string SE_SHUTDOWN_NAM E = "SeShutdownPriv ilege";
IntPtr hToken = IntPtr.Zero; TOKEN_PRIVILEGE S tkp = new TOKEN_PRIVILEGE S();
if( !OpenProcessTok en(GetCurrentPr ocess(), TOKEN_ADJUST_PR IVILEGE | TOKEN_QUERY, ref hToken) ) return false;
if( !LookupPrivileg eValue(null, SE_SHUTDOWN_NAM E, ref tkp.Privileges. Luid) ) throw new SystemException (Marshal.GetLas tWin32Error().T oString());
tkp.PrivilegeCo unt = 1; tkp.Privileges. Attributes = SE_PRIVILEGE_EN ABLED;
if( !AdjustTokenPri vileges(hToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero) ) return false;
if( !ExitWindowsEx( m_Flag, 0) ) throw new SystemException (Marshal.GetLas tWin32Error().T oString()); return true; }
--
Regards,
Nurchi BECHED This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 this would be simple enough if the ASP didn't have any user input,
but it does.
|
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 logged
in my alert file for the shutdown or startup, however, the databases
are down and when I manually start them up, the alert log reveals a
crash recovery. I've verified the settings in the registry for
automatic shutdown and startup. I've...
|
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 : Shutdown/reboot/logoff.
I have tried this :
AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf
|
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 send mail code is
executed, other necessary Windows Services have been terminated before
it can actually send the mail. I've updated my
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services registry
DependOnService value including SMPTSVC and...
|
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 as I find appropriate.
I am using the socket asynchronously by calling the BeingSend and
BeginReceive calls. I would like to be able to call shutdown and close
asynchronously if possible.
| |
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 talk to
the UPS over a special device driver.
Through this device driver I can detect that the UPS is going to notify
Windows 2000 server to shut down. So I start doing a graceful
termination. But Windows shuts down pretty quickly and there...
|
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, and I'm baffled. I've done
some more Google searches and have come up with the same thing. A portion of
the code is below. I call the HookSessionEnding routine in the Load event of
the main form, but the shutdown event code never fires. I'm baffled....
|
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 Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
to save the data before the shutdown.
The problem is that i can't open excel. Windows is prompting that it can't
close my programm....
|
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 tries to
shutdown Windows, my application doesn't quit and neither does
Windows. Of course, if the user first click of my application and
then tries to shutdown, there is no problem.
To solve this problem, I overrode WndProc of the main form,...
|
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)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |