473,543 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to reboot, or shutdown the computer ?

in c++ it was ExitWindowsEx()

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

class Class1
{
[DllImport("user 32.dll")]
static extern bool ExitWindowsEx(u int 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************ *******@microso ft.com> wrote in
message news:OI******** ******@TK2MSFTN GP09.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("user 32.dll")]
static extern bool ExitWindowsEx(u int 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************ *******@microso ft.com> wrote in
message news:OI******** ******@TK2MSFTN GP09.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.Manageme nt
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.co m

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

class Class1
{
[DllImport("user 32.dll")]
static extern bool ExitWindowsEx(u int 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************ *******@microso ft.com> wrote in
message news:OI******** ******@TK2MSFTN GP09.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************ *******@microso ft.com> wrote in
message news:42******** ******@microsof t.com...
Lebesgue wrote:
In C#, it is still the same ExitWindowsEx API call.

class Class1
{
[DllImport("user 32.dll")]
static extern bool ExitWindowsEx(u int 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************ *******@microso ft.com> wrote in
message news:OI******** ******@TK2MSFTN GP09.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.Manageme nt classes and the WMI class
Win32_Operating System.

public static void Main() {
ManagementBaseO bject outParams = null;
ManagementClass os = new ManagementClass ("Win32_Operati ngSystem");
os.Get();
os.Scope.Option s.EnablePrivile ges = true; // enables required security
privilege.
ManagementBaseO bject inParams = os.GetMethodPar ameters("Win32S hutdown");
inParams["Flags"] = "1"; // System shutdown
inParams["Reserved"] = "0";
foreach (ManagementObje ct mo in os.GetInstances ())
outParams = mo.InvokeMethod ("Win32Shutdown ",
inParams, null);

Willy.
Nov 17 '05 #5

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

class Class1
{
[DllImport("user 32.dll")]
static extern bool ExitWindowsEx(u int 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************ *******@microso ft.com> wrote in
message news:OI******** ******@TK2MSFTN GP09.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.c om> wrote in
message news:e3******** ******@tk2msftn gp13.phx.gbl...
Herbert,

No, there is not. You could use the classes in the System.Manageme nt
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.Manageme nt 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
4245
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 MSSQLSERVER service to have the same credentials as the thin clients we are trying to shut down, that means the service is running as an...
0
1891
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
3
1189
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
1543
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 organization on an application that sometimes gets killed by an installation that restarts the computer. I want to prevent this from happening and display...
1
5134
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 programmatically reboot the computer? (Note: The computer will be Win2k, WinXP, or Win2k3, nothing else.) Thanks.
3
2217
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:" _ & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _ strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery _ ("SELECT * FROM Win32_OperatingSystem")
3
1690
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 AM with an unhandled exception on the screen. As best I can tell this exception occurs while it's trying to reboot the pc. This has me thinking...
0
1433
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 ForcedLogOff = 4
3
1471
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 reboot or shutdown. the application is not doing anything special, all it does is checking a web site every hour using timer. anyone saw this...
0
7351
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7691
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 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...
0
5887
agi2029
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...
1
5276
isladogs
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...
0
4896
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3392
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1819
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
1
973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
639
bsmnconsultancy
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...

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.