Search code examples
c++windowswinapiapplication-shutdown

How to turn off pc via windows API?


I never programmed a winapi so i have a little problem here .

I need turn off my pc from my application .

I found this example link text then i found this example how to change privileges link text

But i have problem how to get that parameter HANDLE hToken // access token handle

I think i need to make it in the next order to get the parameter OpenProcessToken LookupPrivilegeValue AdjustTokenPrivileges but there are a lot parameters that i have no idea what to do with them .

maybe you have jere some example how i get that HANDLE hToken parameter to make that work .

By the way I already saw the following post link text

Thanks a lot all you .


Solution

  • This is a bit much for the comments on Daniel's answer, so I'll put it here.

    It looks like your main issue at this point is that your process isn't running with the priveleges required to perform a system shutdown.

    The docs for ExitWindowsEx contain this line:

    To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.

    They also have some example code. In a pinch, you can just copy that.