正如 Tam\xc3\xa1s 指出的,您需要启动一个具有提升权限的新进程。我过去进行了很多搜索,但没有找到任何方法来提升当前进程的权限。
\n\n假设您的主应用程序是 App1.exe,然后您调用需要提升权限的辅助进程 App2.exe。
\n\n答:您可以在 App2.exe 中嵌入清单,但更简单的方法是创建一个名为 App2.exe.manifest 的清单文件[文本文件],其中包含以下内容,并将其放在与 App2.exe 相同的目录中。\n注意:!! 奇怪的是,如果您的应用程序名称不是 App2.exe,而是 App2_install.exe 或 App2_setup.exe(即,如果应用程序名称包含“install”或“setup”),Windows Vista / Windows 7 中将自动出现 UAC 对话框即使没有清单文件,也会要求提升权限!\n这是清单文件的示例:
\n\n<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">\n<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">\n<security>\n<requestedPrivileges>\n<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />\n</requestedPrivileges>\n</security>\n</trustInfo>\n</assembly>\nRun Code Online (Sandbox Code Playgroud)\n\nB. 您可以在 App1.exe 中使用如下代码来启动 App2.exe
\n\nQString AppToExec = qApp->applicationDirPath() + "/App2.exe";\n// Put any required parameters of App2.exe to AppParams string\nQString AppParams = "";\nif (0 != genWin32ShellExecute(AppToExec, \n "", // default verb: "open" or "exec"\n AppParams,\n false, // run hidden\n true)) // wait to finish\n{\n // (...) handle error\n}\nRun Code Online (Sandbox Code Playgroud)\n\n...最后,这是我创建的 Win32 函数 genWin32ShellExecute() 的代码,用于在 Win32 O/S 上使用 QT 时启动进程或打开文档:
\n\n标题:
\n\n#ifdef Q_OS_WIN // Implement genWin32ShellExecute() especially for UAC\n #include "qt_windows.h"\n #include "qwindowdefs_win.h"\n #include <shellapi.h>\n\nint genWin32ShellExecute(QString AppFullPath,\n QString Verb,\n QString Params,\n bool ShowAppWindow,\n bool WaitToFinish);\n#endif\nRun Code Online (Sandbox Code Playgroud)\n\n消费者保护计划:
\n\n// Execute/Open the specified Application/Document with the given command\n// line Parameters\n// (if WaitToFinish == true, wait for the spawn process to finish)\n//\n// Verb parameter values:\n// "" The degault verb for the associated AppFullPath\n// "edit" Launches an editor and opens the document for editing.\n// "find" Initiates a search starting from the specified directory.\n// "open" Launches an application. If this file is not an executable file, its associated application is launched.\n// "print" Prints the document file.\n// "properties" Displays the object\'s properties.\n//\n// Ret: 0 = success\n// <0 = error\n#ifdef Q_OS_WIN\nint genWin32ShellExecute(QString AppFullPath,\n QString Verb,\n QString Params,\n bool ShowAppWindow,\n bool WaitToFinish)\n{\n int Result = 0;\n\n // Setup the required structure\n SHELLEXECUTEINFO ShExecInfo;\n memset(&ShExecInfo, 0, sizeof(SHELLEXECUTEINFO));\n ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);\n ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;\n ShExecInfo.hwnd = NULL;\n ShExecInfo.lpVerb = NULL;\n if (Verb.length() > 0)\n ShExecInfo.lpVerb = reinterpret_cast<const WCHAR *>(Verb.utf16());\n ShExecInfo.lpFile = NULL;\n if (AppFullPath.length() > 0)\n ShExecInfo.lpFile = reinterpret_cast<const WCHAR *>(AppFullPath.utf16());\n ShExecInfo.lpParameters = NULL;\n if (Params.length() > 0)\n ShExecInfo.lpParameters = reinterpret_cast<const WCHAR *>(Params.utf16());\n ShExecInfo.lpDirectory = NULL;\n ShExecInfo.nShow = (ShowAppWindow ? SW_SHOW : SW_HIDE);\n ShExecInfo.hInstApp = NULL;\n\n // Spawn the process\n if (ShellExecuteEx(&ShExecInfo) == FALSE)\n {\n Result = -1; // Failed to execute process\n } else if (WaitToFinish)\n {\n WaitForSingleObject(ShExecInfo.hProcess, INFINITE);\n }\n\n return Result;\n}\n#endif\nRun Code Online (Sandbox Code Playgroud)\n