Check for admin permission

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Check for admin permission

Post by Wanderer82 »

Hello

I'm using Shellexecute with the "runas" parameter so that a file is executed and asking for admin permission. Problem is: if the user denies permission the program doesn't continue because I have a wait loop for something to happen which will only happen if the program is executed. So I need to check somehow whether the user gave permission or not.

Thanks,
Thomas
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Check for admin permission

Post by doublemax »

Doesn't ShellExecuteEx return false in that case?
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check for admin permission

Post by Wanderer82 »

I'm using ShellExecute() only.

ShellExecute(NULL, L"runas", L"explorer2.exe", NULL, NULL, SW_SHOWNORMAL);

How would I have to change this to use ShellExecuteEx()?
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Check for admin permission

Post by doublemax »

ShellExecute should work too. Just check its return value:
https://docs.microsoft.com/en-us/window ... turn-value
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check for admin permission

Post by Wanderer82 »

Hm, the documentation says that ShellExecute returns a value of 32 or smaller if an error occurred or also there are some return values given as SE_ERR_ACCESSDENIED.

But when I write: if(ShellExecute(NULL, L"runas", L"explorer2.exe", NULL, NULL, SW_SHOWNORMAL) > 32)

I get the error:
ISO C++ forbids comparison between pointer and integer [-fpermissive]|
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Check for admin permission

Post by doublemax »

Cast the return value to INT_PTR like it says in the link i posted.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check for admin permission

Post by Wanderer82 »

I'm afraid I don't know to do that. I don't know what that means "return value to INT_PTR". I mean, I create a variable

INT_PTR variable;

And then I wirte variable=ShellExecute(xy); ?

Actually I tried it this way;

Code: Select all

int result=(int)ShellExecute(NULL, L"runas", L"explorer2.exe", NULL, NULL, SW_SHOWNORMAL);
    if(result > 32)
    {
    while ((!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\file explorer.lnk"))
            && (!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\explorer.lnk"))
            && (!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\file explorer (1).lnk")))
    {
        Sleep(500);
    }
    }
    else
    {
        wxMessageBox("Es wurden keine Admin-Rechte gewährt.\nTaskleisten-Einrichtung wird abgebrochen.");
        return;
    }
    Sleep(500);
    
But then the program isn't even executed and the user can't even give admin permission... it returns an error right away.

EDIT: Now I tried this but still the program doesn't come to the execution before the error is already thrown:

Code: Select all

    INT_PTR result=(int)ShellExecute(NULL, L"runas", L"explorer2.exe", NULL, NULL, SW_SHOWNORMAL);
    if(result > 32)
    {
    while ((!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\file explorer.lnk"))
            && (!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\explorer.lnk"))
            && (!FileExists("c:\\users\\" + _User.Username + "\\appdata\\roaming\\microsoft\\internet explorer\\quick launch\\user pinned\\taskbar\\file explorer (1).lnk")))
    {
        Sleep(500);
    }
    }
    else
    {
        wxMessageBox("Es wurden keine Admin-Rechte gewährt.\nTaskleisten-Einrichtung wird abgebrochen.");
        return;
    }
    Sleep(500);
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check for admin permission

Post by Wanderer82 »

Sorry, found the mistakes. I changed a path and the program didn't find the file to execute anymore ;-)
Post Reply