// ------提升权限------
// Initialize the structure.
SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) }; sei.fMask = SEE_MASK_NOCLOSEPROCESS; //不可少,否则不会等待安装结束
(详见SHELLEXECUTEINFO定义: HANDLE hProcess; // out, valid when SEE_MASK_NOCLOSEPROCESS specified)
// Ask for privileges elevation.
sei.lpVerb = TEXT("runas");
// Create a Command Prompt from which you will be able to start
// other elevated applications.
sei.lpFile = szFile;
sei.lpParameters = szCmdline;
sei.lpDirectory = szWorking;
// Don't forget this parameter; otherwise, the window will be hidden.
sei.nShow = SW_SHOWNORMAL;
if (!ShellExecuteEx(&sei)) {
DWORD dwStatus = GetLastError();
if (dwStatus == ERROR_CANCELLED) {
// The user refused to allow privileges elevation.
UpdateMessage(_T("用户拒绝安装,安装失败。"));
}
else if (dwStatus == ERROR_FILE_NOT_FOUND) {
// The file defined by lpFile was not found and
// an error message popped up.
UpdateMessage(_T("安装包不存在,请检查!"));
}