C# InitiateSystemShutdown呼叫不工作

C# InitiateSystemShutdown呼叫不工作,c#,winapi,C#,Winapi,我正在编写一个应用程序,需要重新启动运行代码的Windows计算机 在.NET中似乎没有API来执行此操作,因此我在Win32 API中查找了此操作,它被称为InitiateSystemShutdown。extern声明如下所示: [DllImport("advapi32.dll")] public static extern bool InitiateSystemShutdown(string Machinename, string Message, long Timeout, int For

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

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

[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服务内运行,并在管理员帐户下运行。我尝试将其作为本地系统运行,但没有效果。

请参阅MSDN中的以下示例:


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

请参阅MSDN中的以下示例:

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