使用API​​关闭PC [英] Using API to shutdown a PC

查看:54
本文介绍了使用API​​关闭PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了代码,但出于某种原因,当我点击

按钮时,什么也没发生。这是代码。


使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Text;

使用System.Windows。表格;

使用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);

}


它编译得很好,但是当我点击button1时,没有任何反应。任何想法?

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?

推荐答案

你好greatbarrier86,


你的Button1是否链接到OnClick事件处理程序?


---

WBR,

Michael Nemtsev [.NET / C#MVP] ::博客: http://spaces.live.com/laflour


" ;对我们大多数人来说,最大的危险并不是我们的目标太高而且我们错过了它,但它太低了,我们就达不到它了。 (c)Michelangelo

g我已经编写了代码但由于某种原因,当我点击

gthe按钮时,什么也没发生。这是代码。

g>

gusing系统;

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}

g编译正常,但是当我点击button1时,没有任何反应。任何

gideas?

g>
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>


对不起。我有点像菜鸟......我怎么知道这个?


" Michael Nemtsev" < Michael Nemtsev>," MVP"写道:
Sorry. i am somewhat of a noob...how do i figure that out?

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

你好greatbarrier86,


你的Button1是否链接到OnClick事件处理程序?


---

WBR,
http://spaces.live.com/laflour


对大多数人来说是最大的危险我们并不是说我们的目标太高而且我们错过了它,但它太低了我们就达不到它了。 (c)米开朗基罗


g我已经编写了代码但由于某种原因,当我点击

gthe按钮时,什么也没发生。这是代码。

g>

gusing系统;

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}

g编译正常,但是当我点击button1时,没有任何反应。任何

gideas?

g>
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>


greatbarrier86写道:
greatbarrier86 wrote:

" Michael Nemtsev" < Michael Nemtsev>," MVP"写道:
"Michael Nemtsev" <Michael Nemtsev>, "MVP" wrote:

> Hello greatbarrier86,

你的Button1是否链接到OnClick事件处理程序?
>Hello greatbarrier86,

Is your Button1 linked to OnClick event handler?



对不起。我有点像菜鸟......我怎么知道这个?


Sorry. i am somewhat of a noob...how do i figure that out?



好​​吧,一种方法是在方法内的代码行上设置一个断点,然后运行你的程序并点击按钮。


如果它连接正确,你会点到断点。


恕我直言,几乎每一行代码你写的,你应该至少调试一次调试器。*
。但特别是如果代码似乎没有工作,那么第一步就是使用调试器来查看它是否正在执行所有,如果是的话,代码是否正在做什么

你认为(包括返回你认为应该的值)。


只有你有确认所有这些你应该担心什么

其他可能是错误的。


如果你没有碰到断点,那么确保这一点的一种方法在VS设计师的指导下,按照

按钮进入你的表格,选择按钮,然后点击闪电,它就会被正确连接



属性窗口中的螺栓图标显示事件。找到Click事件,看看

,如果该方法被列为指定的处理程序。


如果不是,你应该可以点击在该字段中的箭头,找到你写的方法
,并选择它作为处理程序。


Pete

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


这篇关于使用API​​关闭PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆