473,761 Members | 10,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d, int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?
Oct 13 '07 #1
11 4778
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.Collecti ons.Generic;
gusing System.Componen tModel;
gusing System.Data;
gusing System.Drawing;
gusing System.Text;
gusing System.Windows. Forms;
gusing System.Runtime. InteropServices ;
g[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
gpublic static extern bool InitiateSystemS hutdownA(string
glpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d,
gint
gbRebootAfterSh utdown);
g>
gprivate void button1_Click(o bject sender, System.EventArg s e)
g{
gInitiateSystem ShutdownA(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.Collecti ons.Generic;
gusing System.Componen tModel;
gusing System.Data;
gusing System.Drawing;
gusing System.Text;
gusing System.Windows. Forms;
gusing System.Runtime. InteropServices ;
g[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
gpublic static extern bool InitiateSystemS hutdownA(string
glpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d,
gint
gbRebootAfterSh utdown);
g>
gprivate void button1_Click(o bject sender, System.EventArg s e)
g{
gInitiateSystem ShutdownA(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.mi crosoft.comwrot e in message
news:E3******** *************** ***********@mic rosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d, int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 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.mi crosoft.comwrot e in message
news:E3******** *************** ***********@mic rosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d, int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 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 InitiateSystemS hutdownA(string ....

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

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" ,SetLastError=t rue)]

3) Check what "InitiateSystem Shutdown" 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.GetLas tWin32Error 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_NAM E 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.mi crosoft.comwrot e in message
news:A8******** *************** ***********@mic rosoft.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
>"greatbarrier8 6" <gr************ @discussions.mi crosoft.comwrot e in
message
news:E3******* *************** ************@mi crosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d,
int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 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_NAM E 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 InitiateSystemS hutdownA(string ....

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

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" ,SetLastError=t rue)]

3) Check what "InitiateSystem Shutdown" 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.GetLas tWin32Error 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_NAM E 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.mi crosoft.comwrot e in message
news:A8******** *************** ***********@mic rosoft.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
"greatbarrier86 " <gr************ @discussions.mi crosoft.comwrot e in
message
news:E3******** *************** ***********@mic rosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d,
int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 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 GetLastWin32Err or 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 InitiateSystemS hutdownA(string ....

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

2) Make sure the GetLastError returns something meaningful, by setting the
SetLastError to true.
[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" ,SetLastError=t rue)]

3) Check what "InitiateSystem Shutdown" 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.GetLas tWin32Error 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_NAM E 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.mi crosoft.comwrot e in message
news:A8******** *************** ***********@mic rosoft.com...
Heh...like i said, i'm a noob :)

How do i check the return value?

"Willy Denoyette [MVP]" wrote:
"greatbarrier86 " <gr************ @discussions.mi crosoft.comwrot e in
message
news:E3******** *************** ***********@mic rosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d,
int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 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.Manageme nt 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.mi crosoft.comwrot e in message
news:E3******** *************** ***********@mic rosoft.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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

[DllImport("adva pi32.dll",Entry Point="Initiate SystemShutdown" )]
public static extern bool InitiateSystemS hutdownA(string
lpMachineName, string lpMessage, int dwTimeout, int bForceAppsClose d, int
bRebootAfterShu tdown);

private void button1_Click(o bject sender, System.EventArg s e)
{
InitiateSystemS hutdownA(null,n ull, 30, 1, 1);
}

It compiles fine, but when i click button1, nothing happens. Any ideas?
Oct 13 '07 #10

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

Similar topics

0
1908
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
2
11415
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, OpenProcessToken, LookupPrivilegeValue, AdjustTokenPrivilegese, ExitWindowsEx. I am trying to avoid using any API calls if possible and to use managed code instead. I couldn't find any easy way of doing this but searching the Internet with...
3
10518
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, so im sure it has room for improvement Assumptions: list of computers in C:\shutdownvbs\computers.txt
1
2422
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
2300
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....
5
17413
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
1086
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 shutdown is completed or not, As this method InvokeMethod("Win32Shutdown", inParams, null); is asynchronous. not only for shutdown, i want the status for restart also. once the restart is completed how to know that , that is done. Plz any...
3
4288
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)
0
9521
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, 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...
0
9333
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
Oralloy
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...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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,...
1
7324
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6599
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.