使程序以管理员权限运行(C++)

时间:2024-02-15 19:25:24



来源网站:http://blog.csdn.net/jhui163/article/details/5873027


在看射手源代码的时候,发现这个函数,是获取管理员权限的。但是不知道效果如果,先发上来大家测试下。


void GainAdminPrivileges(UINT idd, BOOL bWait){
	CString			strCmd;
	CString			strApp;

	strCmd.Format (_T("/adminoption %d"), idd);

	CSVPToolBox svpTool;
	strApp = svpTool.GetPlayerPath();


	SHELLEXECUTEINFO execinfo;
	memset(&execinfo, 0, sizeof(execinfo));
	execinfo.lpFile			= strApp;
	execinfo.cbSize			= sizeof(execinfo);
	execinfo.lpVerb			= _T("runas");
	execinfo.fMask			= SEE_MASK_NOCLOSEPROCESS;
	execinfo.nShow			= SW_SHOWDEFAULT;
	execinfo.lpParameters	= strCmd;

	ShellExecuteEx(&execinfo);

	if(bWait)
		WaitForSingleObject(execinfo.hProcess, INFINITE);
}



下面是比较明了的代码,来自评论

void GainAdminPrivileges(CString strApp,UINT idd){  
    CString         strCmd;  
    strCmd.Format (_T("/adminoption %d"), idd);  
  
    SHELLEXECUTEINFO execinfo;  
    memset(&execinfo, 0, sizeof(execinfo));  
    execinfo.lpFile         = strApp;  
    execinfo.cbSize         = sizeof(execinfo);  
    execinfo.lpVerb         = _T("runas");  
    execinfo.fMask          = SEE_MASK_NO_CONSOLE;  
    execinfo.nShow          = SW_SHOWDEFAULT;  
    execinfo.lpParameters   = strCmd;  
  
    ShellExecuteEx(&execinfo);  
  
} 
strApp是应用程序的路径idd我传的是1,但好像传几都没问题


手动修改程序以管理员身份运行

 来源网站:http://blog.csdn.net/zhangyulin54321/article/details/8802023