static int RestartAsAdmin(LPCWSTR lpCmdLine, LPCWSTR cwd)
{
wchar_t myPath[MAX_PATH];
if (!GetModuleFileNameW(nullptr, myPath, _countof(myPath) - 1)) {
return 0;
}
SHELLEXECUTEINFO shExInfo = {0};
shExInfo.cbSize = sizeof(shExInfo);
shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExInfo.hwnd = 0;
shExInfo.lpVerb = L"runas"; /* Operation to perform */
shExInfo.lpFile = myPath; /* Application to start */
shExInfo.lpParameters = lpCmdLine; /* Additional parameters */
shExInfo.lpDirectory = cwd;
shExInfo.nShow = SW_NORMAL;
shExInfo.hInstApp = 0;
/* annoyingly the actual elevated updater will disappear behind other
* windows :( */
AllowSetForegroundWindow(ASFW_ANY);
/* if the admin is a different user, save the path to the user's
* appdata so we can load the correct manifest */
CoTaskMemPtr<wchar_t> pOut;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData,
KF_FLAG_DEFAULT, nullptr, &pOut);
if (hr == S_OK)
SetEnvironmentVariable(L"OBS_USER_APPDATA_PATH", pOut);
if (ShellExecuteEx(&shExInfo)) {
DWORD exitCode;
WaitForSingleObject(shExInfo.hProcess, INFINITE);
if (GetExitCodeProcess(shExInfo.hProcess, &exitCode)) {
if (exitCode == 1) {
return exitCode;
}
}
CloseHandle(shExInfo.hProcess);
}
return 0;
}
c++ 管理员启动程序
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 思路 作为windows服务,要想在用户桌面端启动一个程序,而且是以管理员身份运行的,这个思路其实跟其他语言一样的...
- 微软在Windows Vista开始引入了UAC(用户帐户控制)新技术(点击这儿了解什么是UAC)。当程序执行时需...
- 什么是好的c/c++程序员?是不是懂得很多技术细节?还是懂底层编程?还是编程速度比较快?我觉得都不是。对于一些技术...
- 什么是好的c/c++程序员?是不是懂得很多技术细节?还是懂底层编程?还是编程速度比较快?我觉得都不是。对于一些技术...