InitiateSystemShutdown调用不起作用

我正在编写一个应用程序,需要重新启动运行代码的Windows机器。

在.NET中似乎没有一个API来执行此操作,所以我为此查找了Win32 API,它被称为InitiateSystemShutdown。 外部声明如下:

[DllImport("advapi32.dll")] public static extern bool InitiateSystemShutdown(string Machinename, string Message, long Timeout, int ForceAppsClosed, int RebootAfterShutdown); 

然后我尝试使用以下参数调用此操作系统例程:

 InitiateSystemShutdown(null, null, 30, 1, 1); 

但是,这总是返回false。 所以我调用Marshal.GetLastWin32Error方法,它返回错误代码1008.此错误代码的消息是:

“试图引用一个不存在的令牌。”

代码在Windows服务中运行,并在Administrator帐户下运行。 我已经尝试将其作为本地系统运行并且没有效果。

请参阅MSDN中的以下示例: 显示“关闭”对话框 。

您需要为您的线程获得正确的权限。