InitiateSystemShutdown调用不起作用 [英] InitiateSystemShutdown call not working
问题描述
我正在编写一个应用程序,该应用程序需要重新启动运行代码的Windows计算机.
I'm writing an application that will need to reboot the Windows machine the code is running on.
.NET中似乎没有API可以执行此操作,因此我为此查找了Win32 API,并将其称为InitiateSystemShutdown.extern声明如下:
There didn't appear to be an API within .NET to do this, so I looked up the the Win32 API for this and it is called InitiateSystemShutdown. The extern declaration is given below:
[DllImport("advapi32.dll")]
public static extern bool InitiateSystemShutdown(string Machinename, string
Message, long Timeout, int ForceAppsClosed, int RebootAfterShutdown);
然后我尝试使用以下参数调用此操作系统例程:
I then try to call this operating system routine with the following arguments:
InitiateSystemShutdown(null, null, 30, 1, 1);
但是,它总是返回false.因此,我调用了Marshal.GetLastWin32Error方法,它返回了错误代码1008.该错误代码的消息是:
However, this always returns false. So I call the Marshal.GetLastWin32Error method and it returns an error code of 1008. This error code's message is:
尝试引用不存在的令牌."
"An attempt was made to reference a token that does not exist."
该代码在Windows Service内部运行,并在Administrator帐户下运行.我尝试过将其作为本地系统运行,但没有效果.
The code is running inside a Windows Service and is running under the Administrator account. I've tried running it as Local System and that had not effect.
推荐答案
请参见MSDN中的以下示例:.
See the following example in MSDN: Displaying the Shutdown Dialog Box.
您需要为线程获取正确的特权.
You need to get the correct privileges for your thread.
这篇关于InitiateSystemShutdown调用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!