mercredi 13 septembre 2023

Using ShellExecute without UAC prompt in C++ Windows

I have a c++ windows application that has two potential users. Depending on which user is logged in they may or may not have full admin access. We now need to modify a config file dynamically. This file needs admin access to modify. I am using ShellExecute to run a separate process which handles the modification. The issue is the UAC prompt appears and requires user interaction. This is not satisfactory. Is it possible to do this in my application.

int ElevatedExecution(const std::string& program)
{
    int res{};
    SHELLEXECUTEINFO info = {0};
    info.cbSize = sizeof(SHELLEXECUTEINFO);
    info.fMask = SEE_MASK_NOCLOSEPROCESS;
    info.lpVerb = "runas";
    info.lpFile = program.c_str();
    info.lpParameters = NULL;
    info.nShow = SW_SHOWNORMAL;

    if (::ShellExecuteEx(&info) == TRUE)
    {
        ::WaitForSingleObject(info.hProcess, INFINITE);
        DWORD ret_val = 0;
        if (::GetExitCodeProcess(info.hProcess, &ret_val))
        {
            res = static_cast<int>(ret_val);
        }
    }
    return res;
}

Aucun commentaire:

Enregistrer un commentaire