Skip to content

Commit

Permalink
making flow clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
remibettan committed Feb 8, 2023
1 parent 4d9b3f0 commit 17565cd
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions common/metadata-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,54 +198,47 @@ namespace rs2
return result == TRUE;
}

bool elevate_to_admin()
bool enable_metadata_with_new_admin_process()
{
if (!is_running_as_admin())
wchar_t szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
{
wchar_t szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
SHELLEXECUTEINFO sei = { sizeof(sei) };

sei.lpVerb = L"runas";
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpFile = szPath;
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
auto cmd_line = get_command_line_param();
std::wstring wcmd(cmd_line.begin(), cmd_line.end());
sei.lpParameters = wcmd.c_str();

if (!ShellExecuteEx(&sei))
{
SHELLEXECUTEINFO sei = { sizeof(sei) };

sei.lpVerb = L"runas";
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpFile = szPath;
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
auto cmd_line = get_command_line_param();
std::wstring wcmd(cmd_line.begin(), cmd_line.end());
sei.lpParameters = wcmd.c_str();

if (!ShellExecuteEx(&sei))
{
auto errstr = std::system_category().message(GetLastError());
std::string msg = "Unable to elevate to admin privilege to enable metadata! " + errstr;
rs2::log(RS2_LOG_SEVERITY_WARN, msg.c_str());
return false;
}
else
{
WaitForSingleObject(sei.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(sei.hProcess, &exitCode);
CloseHandle(sei.hProcess);
if (exitCode)
throw std::runtime_error("Failed to set metadata registry keys!");
// returning false here so that the instance that runs "not as admin"
// will not even try to do the writing to registry job
// This job is done by the "run as admin" instance.
return false;
}
auto errstr = std::system_category().message(GetLastError());
std::string msg = "Unable to elevate to admin privilege to enable metadata! " + errstr;
rs2::log(RS2_LOG_SEVERITY_WARN, msg.c_str());
return false;
}
else
{
rs2::log(RS2_LOG_SEVERITY_WARN, "Unable to fetch module name!");
WaitForSingleObject(sei.hProcess, INFINITE);
DWORD exitCode = 0;
GetExitCodeProcess(sei.hProcess, &exitCode);
CloseHandle(sei.hProcess);
if (exitCode)
throw std::runtime_error("Failed to set metadata registry keys!");
// returning false here so that the instance that runs "not as admin"
// will not even try to do the writing to registry job
// This job is done by the "run as admin" instance.
return false;
}
}
else
{
return true;
rs2::log(RS2_LOG_SEVERITY_WARN, "Unable to fetch module name!");
return false;
}
}

Expand Down Expand Up @@ -293,7 +286,7 @@ namespace rs2

void enable_metadata() override
{
if (elevate_to_admin()) // Elevation to admin was succesful?
if (is_running_as_admin())
{
std::vector<device_id> dids;

Expand Down Expand Up @@ -354,6 +347,10 @@ namespace rs2
});
if (failure) throw std::runtime_error("Unable to write to metadata registry key!");
}
else
{
enable_metadata_with_new_admin_process();
}
}
};
#endif
Expand Down

0 comments on commit 17565cd

Please sign in to comment.