Rebooting the PC from code
-
Anyone know how to reboot the pc that the application is running on using code? Cheers Kev
You could PInvoke one of these functions[^].
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Anyone know how to reboot the pc that the application is running on using code? Cheers Kev
-
That's great, i can get the pc to log the user off no problems but im still having problems rebooting it. I know why, i dont know how to set the privileges for the current process.
The calling process must have the SE_SHUTDOWN_NAME privilege
I'm reading through the MSDN docs trying to figure it out but the only one i've found so far is written using C++ code, and the classes are not the same in C#. If you could help me set the privileges that would be great! Cheers Kev -
That's great, i can get the pc to log the user off no problems but im still having problems rebooting it. I know why, i dont know how to set the privileges for the current process.
The calling process must have the SE_SHUTDOWN_NAME privilege
I'm reading through the MSDN docs trying to figure it out but the only one i've found so far is written using C++ code, and the classes are not the same in C#. If you could help me set the privileges that would be great! Cheers Kev -
I'd suggest you taking a look at www.pinvoke.net and convert the appropriate API calls by yourself. This should help you a bit. I've not done this in C# myself yet. regards
-
Got it sorted now, thanks. Found an article that had already converted the API calls to C# and used them. Cheers Kev
HI, can you give me the code of Rebooting regards sanjeev email: sanjeevsinghh@gmail.com
-
Anyone know how to reboot the pc that the application is running on using code? Cheers Kev
-
HI, can you give me the code of Rebooting regards sanjeev email: sanjeevsinghh@gmail.com
Here's some code for converting the functions you need to reboot with.
[DllImport("user32.dll")]
private static extern bool ExitWindowsEx(uint uFlags, uint dwReason);[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );Then you just need to call ExitWindowsEx, see above for links tyo the MSDN documentation for it. Kev