A small note (1): Set to restart the computer, auto-start

Source: Internet
Author: User

"Set Restart PC"

Using API Functions ExitWindowsEx
BOOL WINAPI ExitWindowsEx (
_in_ UINT Uflags,
_in_ DWORD Dwreason
);
"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winuser%2fexitwindowsex); K ( ExitWindowsEx); K (devlang-c%2b%2b); K (targetos-windows) &rd=true
Returns a value other than 0 if the method executes successfully
In fact, MSDN is an example, most of the MSDN examples are a very valuable resource ~
Direct copy one over, change the place on the line:

#include <windows.h>
#pragma comment (lib, "User32.lib")
#pragma comment (lib, "Advapi32.lib")
BOOL Mysystemshutdown ()
{
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_reboot | Ewx_force,
Shtdn_reason_major_operatingsystem |
Shtdn_reason_minor_upgrade |
shtdn_reason_flag_planned))
return FALSE;
Reboot was successful
return TRUE;
}

Before using Exitwindosex, there are some other functions to use, why?
The ExitWindowsEx function is a "high-risk" action, and the system will "need to confirm" whether our program really wants to do this.
So we're letting the system know, "we're really going to use this function to do high-risk action."
In the system, this is called permission, we have to give our own program "right", so there will be a bunch of code above.
OpenProcessToken: Open Current Process token
"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winbase%2fopenprocesstoken); K (OpenProcessToken); K (devlang-c%2b%2b); K (targetos-windows) &rd=true
Lookupprivilegevalue: The program has the appropriate permissions (with the system said: I want a permission)
"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (winbase% 2FLookupPrivilegeValue); K (lookupprivilegevalue); K (devlang-c%2b%2b); K (targetos-windows) &rd=true
AdjustTokenPrivileges: App permissions (with system say: I'm sure to use this permission)
"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (winbase% 2FAdjustTokenPrivileges); K (adjusttokenprivileges); K (devlang-c%2b%2b); K (targetos-windows) &rd=true

"Auto-start "

There are 6 places in the registry that can be written to boot information,
One of the locations: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Steps:
The first step is to open the registry and use the API function RegOpenKeyEx
LONG WINAPI RegOpenKeyEx (
_in_ HKEY HKEY,
_in_opt_ LPCTSTR Lpsubkey,
_in_ DWORD Uloptions,
_in_ Regsam samdesired,
_out_ Phkey Phkresult
);
"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winreg%2fregopenkeyex); K ( RegOpenKeyEx); K (devlang-c%2b%2b); K (targetos-windows) &rd=true
If the method executes successfully, the return value is: error_success

HKEY HKEY = {0};
Open the Registry
RegOpenKeyEx (HKEY_LOCAL_MACHINE, L "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0, KEY_WRITE, &hKey);

The second step, write to the registry, using the API function RegSetValueEx
LONG WINAPI RegSetValueEx (
_in_ HKEY HKEY,
_in_opt_ LPCTSTR Lpvaluename,
_reserved_ DWORD Reserved,
_in_ DWORD dwtype,
_in_ Const BYTE *lpdata,
_in_ DWORD cbdata
);

"MSDN"
Https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=ZH-CN&k=k (Winreg%2fregsetvalueex); K ( RegSetValueEx); K (devlang-c%2b%2b); K (targetos-windows) &rd=true
If the method executes successfully, the return value is: error_success

Char Szpath[maxbyte] = {0};
GetModuleFileNameA (nullptr, szpath, maxbyte);
Write information
RegSetValueEx (HKey, L "Rebootdemo", 0, REG_SZ, (byte*) szpath, strlen (szpath));


And then you can! Done.

A small note (1): Set to restart the computer, auto-start

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.