tags:

views:

271

answers:

2

I'm writing an application that will need to reboot the Windows machine the code is running on.

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

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."

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.

A: 

This answer in the MSDN Forums may help you.

Lucero
+2  A: 

see this example in msdn: http://msdn.microsoft.com/en-us/library/aa376861(VS.85).aspx

you need to get the correct privileges for your thread.

best regards, don

Don Dickinson