028 利用ShellExecuteEx手动提升用户特权,以管理员权限来运行程序。win7会有UAC弹窗

Post date: 2015/3/25 上午 06:03:42

//1

#include <stdio.h> #include<windows.h> #include<tchar.h> int _tmain(int argc,TCHAR* argv[]) { SHELLEXECUTEINFO sei={sizeof(SHELLEXECUTEINFO)}; sei.lpVerb=TEXT("runas"); sei.lpFile=TEXT("cmd.exe");//add application which you want to run as administrator here sei.nShow=SW_SHOWNORMAL;//without this,the windows will be hiden if(!ShellExecuteEx(&sei)){ DWORD dwStatus=GetLastError(); if(dwStatus==ERROR_CANCELLED){ printf("提升權限被用戶拒絕\n"); } else if(dwStatus==ERROR_FILE_NOT_FOUND){ printf("所要執行的文件沒有找到\n"); } } return 0; }

//2

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); }