469,362 Members | 2,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

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 4523
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Allan Bredahl | last post: by
3 posts views Thread by newsgroups.jd | last post: by
1 post views Thread by Titeuf | last post: by
3 posts views Thread by Chris Knievel | last post: by
3 posts views Thread by IdleBrain | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.