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

Using API to shutdown a PC

I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?
Oct 13 '07 #1
11 4748
Hello greatbarrier86,

Is your Button1 linked to OnClick event handler?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
gI've already got the code written but for some reason, when i click
gthe button, nothing occurs. Here is the code.
g>
gusing System;
gusing System.Collections.Generic;
gusing System.ComponentModel;
gusing System.Data;
gusing System.Drawing;
gusing System.Text;
gusing System.Windows.Forms;
gusing System.Runtime.InteropServices;
g[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
gpublic static extern bool InitiateSystemShutdownA(string
glpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed,
gint
gbRebootAfterShutdown);
g>
gprivate void button1_Click(object sender, System.EventArgs e)
g{
gInitiateSystemShutdownA(null,null, 30, 1, 1);
g}
gIt compiles fine, but when i click button1, nothing happens. Any
gideas?
g>
Oct 13 '07 #2
Sorry. i am somewhat of a noob...how do i figure that out?

"Michael Nemtsev" <Michael Nemtsev>, "MVP" wrote:
Hello greatbarrier86,

Is your Button1 linked to OnClick event handler?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
gI've already got the code written but for some reason, when i click
gthe button, nothing occurs. Here is the code.
g>
gusing System;
gusing System.Collections.Generic;
gusing System.ComponentModel;
gusing System.Data;
gusing System.Drawing;
gusing System.Text;
gusing System.Windows.Forms;
gusing System.Runtime.InteropServices;
g[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
gpublic static extern bool InitiateSystemShutdownA(string
glpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed,
gint
gbRebootAfterShutdown);
g>
gprivate void button1_Click(object sender, System.EventArgs e)
g{
gInitiateSystemShutdownA(null,null, 30, 1, 1);
g}
gIt compiles fine, but when i click button1, nothing happens. Any
gideas?
g>
Oct 13 '07 #3
greatbarrier86 wrote:
"Michael Nemtsev" <Michael Nemtsev>, "MVP" wrote:
>Hello greatbarrier86,

Is your Button1 linked to OnClick event handler?

Sorry. i am somewhat of a noob...how do i figure that out?
Well, one way would be to set a breakpoint on the line of code inside
the method, then run your program and click the button.

If it's hooked up correctly, you'll hit the breakpoint.

IMHO, almost every line of code you write, you should step through it in
the debugger at least once. But especially if the code doesn't seem to
be working, the very first step would be to use the debugger to see if
it's being executed at all, and if it is, whether the code is doing what
you think (including returning the value you think it should).

Only once you have confirmed all of that should you worry about what
else might be wrong.

If you don't hit the breakpoint, then one way to ensure that it's hooked
up correctly would be to, in the VS designer, go to your form with the
button, select the button, then click on the lightning-bolt icon in the
properties window to display the events. Find the Click event and see
if that method is listed as the assigned handler.

If it's not, you should be able to click on the arrow in the field, find
the method you wrote, and select it as the handler.

Pete
Oct 13 '07 #4
"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?


How do you know the call succeeded when you don't check the return value of
the API call?

Willy.
Oct 13 '07 #5
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

How do you know the call succeeded when you don't check the return value of
the API call?

Willy.
Oct 13 '07 #6
1) Change the DllImport declaration such that it returns an int value like:
public static extern int InitiateSystemShutdownA(string....

assign the return value to a variable like this:
int ret = InitiateSystemShutdownA(null,null, 30, 1, 1);

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown",SetLastError=true)]

3) Check what "InitiateSystemShutdown" can possibly return (in MSDN) and
take appropriate actions when the return is an error value.

if(ret == 0)
{
// Failed, most (but not all) Win32 API's return 0 on failure
// Call Marshall.GetLastWin32Error to get the Win32 error code
}
else
// success.

Note that your code will most probably return error code 5, which means
"Access Denied". The reason for this is that you need to enable the
SE_SHUTDOWN_NAME privileges. This again has to be done using native API
calls as this is not covered by the Framework classes.

That said, calling into Win32 through PInvoke is not something you should
start without reading the API description in the docs.

Willy.


"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:A8**********************************@microsof t.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
>"greatbarrier86" <gr************@discussions.microsoft.comwrote in
message
news:E3**********************************@microso ft.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed,
int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

How do you know the call succeeded when you don't check the return value
of
the API call?

Willy.

Oct 13 '07 #7
Willy,

Thank you for all your help. It turned out that it failed. Can you explain
to me a little more about the SE_SHUTDOWN_NAME privilege? Or at least how to
give that thread that privilege?

"Willy Denoyette [MVP]" wrote:
1) Change the DllImport declaration such that it returns an int value like:
public static extern int InitiateSystemShutdownA(string....

assign the return value to a variable like this:
int ret = InitiateSystemShutdownA(null,null, 30, 1, 1);

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown",SetLastError=true)]

3) Check what "InitiateSystemShutdown" can possibly return (in MSDN) and
take appropriate actions when the return is an error value.

if(ret == 0)
{
// Failed, most (but not all) Win32 API's return 0 on failure
// Call Marshall.GetLastWin32Error to get the Win32 error code
}
else
// success.

Note that your code will most probably return error code 5, which means
"Access Denied". The reason for this is that you need to enable the
SE_SHUTDOWN_NAME privileges. This again has to be done using native API
calls as this is not covered by the Framework classes.

That said, calling into Win32 through PInvoke is not something you should
start without reading the API description in the docs.

Willy.


"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:A8**********************************@microsof t.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
"greatbarrier86" <gr************@discussions.microsoft.comwrote in
message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed,
int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

How do you know the call succeeded when you don't check the return value
of
the API call?

Willy.


Oct 13 '07 #8
Willy, I used the GetLastWin32Error and it returned "The Parameter is
Incorrect." I'm a little confused.

"Willy Denoyette [MVP]" wrote:
1) Change the DllImport declaration such that it returns an int value like:
public static extern int InitiateSystemShutdownA(string....

assign the return value to a variable like this:
int ret = InitiateSystemShutdownA(null,null, 30, 1, 1);

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown",SetLastError=true)]

3) Check what "InitiateSystemShutdown" can possibly return (in MSDN) and
take appropriate actions when the return is an error value.

if(ret == 0)
{
// Failed, most (but not all) Win32 API's return 0 on failure
// Call Marshall.GetLastWin32Error to get the Win32 error code
}
else
// success.

Note that your code will most probably return error code 5, which means
"Access Denied". The reason for this is that you need to enable the
SE_SHUTDOWN_NAME privileges. This again has to be done using native API
calls as this is not covered by the Framework classes.

That said, calling into Win32 through PInvoke is not something you should
start without reading the API description in the docs.

Willy.


"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:A8**********************************@microsof t.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
"greatbarrier86" <gr************@discussions.microsoft.comwrote in
message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed,
int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

How do you know the call succeeded when you don't check the return value
of
the API call?

Willy.


Oct 13 '07 #9
I haven't read the whole list of messages so I'm not sure if anyone
suggested that you use the System.Management assembly. Just yesterday I
wrote the code for this:
http://www.geekpedia.com/code36_Shut...ng-Csharp.html

The reason your original code doesn't work is that it doesn't get the
required privileges. My code does that.

Let me know if you have questions.

Andrew

"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?
Oct 13 '07 #10
Ooo..that's nice...thanks!!

"Andrew P." wrote:
I haven't read the whole list of messages so I'm not sure if anyone
suggested that you use the System.Management assembly. Just yesterday I
wrote the code for this:
http://www.geekpedia.com/code36_Shut...ng-Csharp.html

The reason your original code doesn't work is that it doesn't get the
required privileges. My code does that.

Let me know if you have questions.

Andrew

"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:E3**********************************@microsof t.com...
I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

Oct 14 '07 #11
Andrew,
Sure this is a better way to shutdown or restart, the reason I didn't
mention System.Management is because it is not exactly what the
"InitiateSystemShutdown" is doing.
InitiateSystemShutdown allows:
1) the caller to abort the shutdown request (it's asynchronous),
2) the user to close the running applications and save the applications
state,
3) to specify a time-out before the actual shutdown takes places and,
4) specify a message to the user.

Note that WMI on Vista has the Win32_OperatingSystem Win32ShutdownTracker
method that has nearly the same functionality plus more.

Willy.

"Andrew P." <an**********@geekpedia.comwrote in message
news:OL**************@TK2MSFTNGP02.phx.gbl...
>I haven't read the whole list of messages so I'm not sure if anyone
suggested that you use the System.Management assembly. Just yesterday I
wrote the code for this:
http://www.geekpedia.com/code36_Shut...ng-Csharp.html

The reason your original code doesn't work is that it doesn't get the
required privileges. My code does that.

Let me know if you have questions.

Andrew

"greatbarrier86" <gr************@discussions.microsoft.comwrote in
message news:E3**********************************@microsof t.com...
>I've already got the code written but for some reason, when i click the
button, nothing occurs. Here is the code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

[DllImport("advapi32.dll",EntryPoint="InitiateSyste mShutdown")]
public static extern bool InitiateSystemShutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);

private void button1_Click(object sender, System.EventArgs e)
{
InitiateSystemShutdownA(null,null, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?

Oct 14 '07 #12

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

Similar topics

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
2
by: Brian Worth | last post by:
I have just upgraded from VB 4.0 to VB .NET 2002. One program under VB 4.0 was able to shut down or restart the (windows XP) machine using a series of API calls. (Getlasterror, GetCurrentProcess,...
3
by: newsgroups.jd | last post by:
Im sure there is a better way to do this - just posting what I did to get it to work. Please feel free to comment with suggestions - this was my first vb.net program and I am not a programmer,...
1
by: Titeuf | last post by:
Hi, I work under VS2003 and I want to know how get the shutdown reason code ? On msdn nothing found...No api :( Have you an idea ? Thank's
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...
5
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
0
by: DKn | last post by:
Hi, I am doing shutdown for a remote system using this method InvokeMethod("Win32Shutdown", inParams, null); in C#.Net 2.0. Once the shutdown is done, how to get the status , whether the...
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)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...

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.