none
Catching Button Press RRS feed

  • Question

  • I am working with a C++ app that requires elevation using UAC. My question is is it possible to capture which button of UAC(Yes/No)was pressed on the UAC. I need this info to trigger another custom action in my app. 
    Thursday, September 19, 2019 8:07 PM

All replies

  • I am working with a C++ app that requires elevation using UAC. My question is is it possible to capture which button of UAC(Yes/No)was pressed on the UAC. I need this info to trigger another custom action in my app. 

    Why do you need to determine how the UAC consent prompt was answered?  Your process can determine if it is running with elevated privilege by examining its token.  GetTokenInformation to retrieve TokenElevationType or TokenElevation



    • Edited by RLWA32 Thursday, September 19, 2019 8:33 PM
    Thursday, September 19, 2019 8:30 PM
  • Hello,

    Thank you for posting here.

    Try to use the following code to determine whether the process is started as an administrator. The following code is based on what RLWA32's method.

    #include <windows.h>
    #pragma   warning(disable: 4996)
    /// @brief	Determine whether the process is running as an administrator
    /// @param[in]	dwProcessId : Process ID
    /// @param[in]	bCurrentProcess : Is it the current process
    /// @remark	DwProcessId is invalid when bCurrentProcess is true
    /// @return	returns true when you are Administrator
    bool IsRunAsAdministrator(DWORD dwProcessId, bool bCurrentProcess)
    {
    	bool bIsElevated = false;
    	HANDLE hToken = NULL;
    
    	UINT16 uWinVer = LOWORD(GetVersion());
    	uWinVer = MAKEWORD(HIBYTE(uWinVer), LOBYTE(uWinVer));
    
    	if (uWinVer < 0x0600)//Judge System Version
    		return (false);
    
    	do
    	{
    		HANDLE hProcess = NULL;
    		if (bCurrentProcess)
    		{
    			hProcess = GetCurrentProcess();
    		}
    		else
    		{
    			hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
    		}
    
    		if (!hProcess)
    		{
    			bIsElevated = false;
    			break;
    		}
    
    		HANDLE hToken = NULL;
    		if (!::OpenProcessToken(hProcess, TOKEN_QUERY, &hToken))
    		{
    			bIsElevated = false;
    			::CloseHandle(hProcess);
    			break;
    		}
    
    		TOKEN_ELEVATION elevation;
    		DWORD dwRetLen = 0;
    		if (!::GetTokenInformation(hToken, TokenElevation, &elevation, sizeof(elevation), &dwRetLen))
    		{
    			bIsElevated = false;
    			::CloseHandle(hToken);
    			::CloseHandle(hProcess);
    			break;
    		}
    
    		if (dwRetLen == sizeof(elevation))
    		{
    			bIsElevated = elevation.TokenIsElevated;
    		}
    		::CloseHandle(hToken);
    		::CloseHandle(hProcess);
    
    	} while (false);
    
    	return bIsElevated;
    }

    Best Regards,

    Suarez Zhou


    Friday, September 20, 2019 2:54 AM