Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Rebooting the PC from code

Rebooting the PC from code

Scheduled Pinned Locked Moved C#
tutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    exhaulted
    wrote on last edited by
    #1

    Anyone know how to reboot the pc that the application is running on using code? Cheers Kev

    C L E 3 Replies Last reply
    0
    • E exhaulted

      Anyone know how to reboot the pc that the application is running on using code? Cheers Kev

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You could PInvoke one of these functions[^].


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      1 Reply Last reply
      0
      • E exhaulted

        Anyone know how to reboot the pc that the application is running on using code? Cheers Kev

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        That should do the trick: clickedy regards

        E 1 Reply Last reply
        0
        • L Lost User

          That should do the trick: clickedy regards

          E Offline
          E Offline
          exhaulted
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • E exhaulted

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            E 1 Reply Last reply
            0
            • L Lost User

              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

              E Offline
              E Offline
              exhaulted
              wrote on last edited by
              #6

              Got it sorted now, thanks. Found an article that had already converted the API calls to C# and used them. Cheers Kev

              K 1 Reply Last reply
              0
              • E exhaulted

                Got it sorted now, thanks. Found an article that had already converted the API calls to C# and used them. Cheers Kev

                K Offline
                K Offline
                ksanju1000
                wrote on last edited by
                #7

                HI, can you give me the code of Rebooting regards sanjeev email: sanjeevsinghh@gmail.com

                E 1 Reply Last reply
                0
                • E exhaulted

                  Anyone know how to reboot the pc that the application is running on using code? Cheers Kev

                  E Offline
                  E Offline
                  eggie5
                  wrote on last edited by
                  #8

                  Look at the win API function ExitWindowEx. /\ |_ E X E GG

                  1 Reply Last reply
                  0
                  • K ksanju1000

                    HI, can you give me the code of Rebooting regards sanjeev email: sanjeevsinghh@gmail.com

                    E Offline
                    E Offline
                    exhaulted
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups