Question

I followed this article for elevating a process, however in my code below (pretty much a copy currently), when debugging, I get an infinite number of shells being created. The line it happens on is indicated.

I've looked at the MSDN article here but this hasn't given me much insight. Please advise what I'm doing wrong?

I'm new to c++.

wchar_t szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
{
    // Launch itself as admin
    SHELLEXECUTEINFO sei = { sizeof(sei) };
    sei.lpVerb = L"runas";
    sei.lpFile = szPath;
    sei.hwnd = NULL;
    sei.nShow = SW_NORMAL;
    if (!ShellExecuteEx(&sei)) //get infinite shells here
    {
        DWORD dwError = GetLastError();
        if (dwError == ERROR_CANCELLED)
        {
            // The user refused to allow privileges elevation.
            std::cout << "User did not allow elevation" << std::endl;
        }
    }
    else
    {
        //other lines of code omitted.
    }       
}   
Was it helpful?

Solution

Roger is right: you are launching a copy of the program that launches a copy of the program etc. etc. - what is missing is the IsAppRunningAsAdminMode() function from the article you are linking: you should call it first and then try to launch a new elevated copy of the program only if it returns false - i.e. only if the current execution program is not elevated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top