473,386 Members | 1,969 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,386 software developers and data experts.

how to reboot, or shutdown the computer ?

in c++ it was ExitWindowsEx()

thanks
Nov 17 '05 #1
6 12705
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint
dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system to
shut down.
}
}

uFlags
4 = Force any applications to quit instead of prompting the user to close
them.
0 = Log off the network.
8 = Shut down the system and, if possible, turn the computer off.
2 = Perform a full reboot of the system.
1 = Shut down the system.
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OI**************@TK2MSFTNGP09.phx.gbl...
in c++ it was ExitWindowsEx()

thanks

Nov 17 '05 #2
Lebesgue wrote:
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint
dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system to
shut down.
}
}

uFlags
4 = Force any applications to quit instead of prompting the user to close
them.
0 = Log off the network.
8 = Shut down the system and, if possible, turn the computer off.
2 = Perform a full reboot of the system.
1 = Shut down the system.
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OI**************@TK2MSFTNGP09.phx.gbl...
in c++ it was ExitWindowsEx()

thanks



thanks

so there is no C# class that encapsulates this API ?
Nov 17 '05 #3
Herbert,

No, there is not. You could use the classes in the System.Management
namespace and access the method on the WMI class, but it is just as
roundabout as using the function through the P/Invoke layer.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:42**************@microsoft.com...
Lebesgue wrote:
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint
dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system
to
shut down.
}
}

uFlags
4 = Force any applications to quit instead of prompting the user to
close
them.
0 = Log off the network.
8 = Shut down the system and, if possible, turn the computer off.
2 = Perform a full reboot of the system.
1 = Shut down the system.
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OI**************@TK2MSFTNGP09.phx.gbl...
in c++ it was ExitWindowsEx()

thanks



thanks

so there is no C# class that encapsulates this API ?

Nov 17 '05 #4

"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:42**************@microsoft.com...
Lebesgue wrote:
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint
dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system
to
shut down.
}
}

uFlags
4 = Force any applications to quit instead of prompting the user to
close
them.
0 = Log off the network.
8 = Shut down the system and, if possible, turn the computer off.
2 = Perform a full reboot of the system.
1 = Shut down the system.
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OI**************@TK2MSFTNGP09.phx.gbl...
in c++ it was ExitWindowsEx()

thanks



thanks

so there is no C# class that encapsulates this API ?


Yes there is, you could use System.Management classes and the WMI class
Win32_OperatingSystem.

public static void Main() {
ManagementBaseObject outParams = null;
ManagementClass os = new ManagementClass("Win32_OperatingSystem");
os.Get();
os.Scope.Options.EnablePrivileges = true; // enables required security
privilege.
ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown");
inParams["Flags"] = "1"; // System shutdown
inParams["Reserved"] = "0";
foreach (ManagementObject mo in os.GetInstances())
outParams = mo.InvokeMethod("Win32Shutdown",
inParams, null);

Willy.
Nov 17 '05 #5

"Lebesgue" <no****@spam.jp> wrote in message
news:e5**************@TK2MSFTNGP12.phx.gbl...
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint
dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(1, 0); //this will cause the system
to
shut down.
}
}

uFlags
4 = Force any applications to quit instead of prompting the user to close
them.
0 = Log off the network.
8 = Shut down the system and, if possible, turn the computer off.
2 = Perform a full reboot of the system.
1 = Shut down the system.
"Herbert VON GRÜNENWALD" <he*******************@microsoft.com> wrote in
message news:OI**************@TK2MSFTNGP09.phx.gbl...
in c++ it was ExitWindowsEx()

thanks



Did you test your code? If you actualy did you would have noticed that this
doesn't work.
The reason is simple, you have to enable the "SeShutdown" privilege using
PInvoke, something more complicated than calling ExitWindowsEx.

Willy.

Nov 17 '05 #6

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:e3**************@tk2msftngp13.phx.gbl...
Herbert,

No, there is not. You could use the classes in the System.Management
namespace and access the method on the WMI class, but it is just as
roundabout as using the function through the P/Invoke layer.

Hope this helps.


Nicholas ,

I have to disagree, because the simple PInvoke snip is not enough to invoke
a shutdown, you need to enable the SeShutdown privilege for this to work,
and this is non-trivial using PInvoke. System.Management is definitely the
way to go.

Willy.
Nov 17 '05 #7

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

Similar topics

1
by: Steve Duke | last post by:
Here is the block of the sproc that I?ve created. All lines execute fine except for the ?exec master..xp_cmdshell @reboottc?. In order to make this work from the query analyzer I had to set the...
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 :...
3
by: Senthil | last post by:
Hi, I would like to know the code for reboot and shutdown windows xp professional using VB.Net. thanx Senthil
1
by: Haim | last post by:
Anybody knows how to prevent a shutdown command? (on Win XP). Or just a way to let my application close itself rather then being killed by the restart command. I have been working in my...
1
by: Dex | last post by:
Hello, We're developing our own small installation application in C#. At the end of the installation, we need to reboot the computer. Does anyone know how to write some C# code to...
3
by: sam | last post by:
I found reboot script at TechNet and I wonder how to apply in VB.Net? Reboot Coding ************ strComputer = "." Set objWMIService = GetObject("winmgmts:" _ &...
3
by: cj | last post by:
My program shuts itself down and reboots the pc it's running on at 2:00am twice a week and has been doing so without fail for over a year. But twice in the past month it's been found running in the...
0
by: remya1000 | last post by:
i'm using VB.NET. i wrote a Auto Reboot program. and here is the codes i tried... Code: Private Enum ShutDown1 LogOff = 0 Shutdown = 1 Reboot = 2 ...
3
by: Kate77 | last post by:
Hello, I have a strange problem, I have application running in the tray bar, for some reason I cant reboot the computer when it is running and i have to close it if i want the computer to do a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
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...

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.