www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I make executeShell ask for Admin Elevation?

reply Marcone <marcone email.com> writes:
I don't want start program with admin elevation, but ask user for 
admin permission when some function is called.
Jul 11 2020
next sibling parent reply Kagamin <spam here.lot> writes:
You call ShellExecute with "runas" verb: 
https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
Jul 12 2020
next sibling parent Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 12:31:17 UTC, Kagamin wrote:
 You call ShellExecute with "runas" verb: 
 https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
Please give me a litle simple example ruining this command: "netsh winhttp set proxy 127.0.0.1:8888"
Jul 12 2020
prev sibling parent Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 12:31:17 UTC, Kagamin wrote:
 You call ShellExecute with "runas" verb: 
 https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
I need return code and stdout.
Jul 12 2020
prev sibling next sibling parent reply Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:
 I don't want start program with admin elevation, but ask user 
 for admin permission when some function is called.
How can I get return code ans stdout of this code: ShellExecute(null, "runas", "cmd", "/c netsh winhttp set proxy 127.0.0.1:1234" , null, SW_HIDE);
Jul 12 2020
parent reply Kagamin <spam here.lot> writes:
Well, you can elevate your own program and tell it to run that 
command, collect the result and send it back through e.g. shared 
memory.
Jul 12 2020
parent reply Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 17:19:08 UTC, Kagamin wrote:
 Well, you can elevate your own program and tell it to run that 
 command, collect the result and send it back through e.g. 
 shared memory.
I don't want start with elevation, becouse program must be avaliable even user is not admin. Only ask for admin if normal usar ask for a command that need elevation.
Jul 12 2020
parent reply Kagamin <spam here.lot> writes:
I mean runas your own program.
Jul 12 2020
parent Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 17:28:01 UTC, Kagamin wrote:
 I mean runas your own program.
I need a function that ask for admin at runtime only for one function
Jul 12 2020
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 7/11/20 7:10 PM, Marcone wrote:
 I don't want start program with admin elevation, but ask user for admin 
 permission when some function is called.
Here is a hacky solution that attempts the command and fails back to asking the password. It should work on POSIX systems. (Tested on Linux.) import std.stdio; import std.process; import std.format; import std.algorithm; // Copied the interface from executeShell auto executeShellSudo(scope const(char)[] command, const string[string] env = null, Config config = Config.none, size_t maxOutput = size_t.max, scope const(char)[] workDir = null, string shellPath = nativeShell) { // First assume the user is super user: auto suCommand = format!"sudo --non-interactive %s"(command); auto execute() { return executeShell(suCommand, env, config, maxOutput, workDir, shellPath); } auto result = execute(); if ((result.status == 1) && (result.output == "sudo: a password is required\n")) { // Have sudo ask for password. (Alternatively, sudo can be invoked // with the --askpass switch.) suCommand = format!"sudo %s"(command); result = execute(); } return result; } void main() { auto result = executeShellSudo("cat /dev/null"); writeln(result); } Ali
Jul 12 2020
parent Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 21:50:06 UTC, Ali Çehreli wrote:
 On 7/11/20 7:10 PM, Marcone wrote:
 [...]
Here is a hacky solution that attempts the command and fails back to asking the password. It should work on POSIX systems. (Tested on Linux.) [...]
I need for Windows, it not work on windows.
Jul 12 2020
prev sibling parent reply Marcone <marcone email.com> writes:
On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:
 I don't want start program with admin elevation, but ask user 
 for admin permission when some function is called.
alias runas = compose!(x => to!bool((cast(int) x) > 32), x => ShellExecute(null, "runas", "cmd", cast(wchar*) "/c \"cd /d %s && %s\"".format(getcwd(), x).to!wstring, null, SW_HIDE).WaitForSingleObject(WAIT_TIMEOUT)); runas("netsh winhttp set proxy 127.0.0.1:9666")
Jul 12 2020
parent reply aberba <karabutaworld gmail.com> writes:
On Monday, 13 July 2020 at 00:57:02 UTC, Marcone wrote:
 On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:
 I don't want start program with admin elevation, but ask user 
 for admin permission when some function is called.
alias runas = compose!(x => to!bool((cast(int) x) > 32), x => ShellExecute(null, "runas", "cmd", cast(wchar*) "/c \"cd /d %s && %s\"".format(getcwd(), x).to!wstring, null, SW_HIDE).WaitForSingleObject(WAIT_TIMEOUT)); runas("netsh winhttp set proxy 127.0.0.1:9666")
Can you put it into a small dub package?
Jul 13 2020
parent reply Marcone <marcone email.com> writes:
On Monday, 13 July 2020 at 12:28:24 UTC, aberba wrote:
 On Monday, 13 July 2020 at 00:57:02 UTC, Marcone wrote:
 On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:
 I don't want start program with admin elevation, but ask user 
 for admin permission when some function is called.
alias runas = compose!(x => to!bool((cast(int) x) > 32), x => ShellExecute(null, "runas", "cmd", cast(wchar*) "/c \"cd /d %s && %s\"".format(getcwd(), x).to!wstring, null, SW_HIDE).WaitForSingleObject(WAIT_TIMEOUT)); runas("netsh winhttp set proxy 127.0.0.1:9666")
Can you put it into a small dub package?
I do not know how to do that. Observation: Remove the code .WaitForSingleObject (WAIT_TIMEOUT)
Jul 13 2020
parent Dominikus Dittes Scherkl <dominikus scherkl.de> writes:
On Monday, 13 July 2020 at 19:32:33 UTC, Marcone wrote:
 alias runas = compose!(x => to!bool((cast(int) x) > 32), x => 
 ShellExecute(null, "runas", "cmd", cast(wchar*) "/c \"cd /d %s 
 && %s\"".format(getcwd(), x).to!wstring, null, 
 SW_HIDE).WaitForSingleObject(WAIT_TIMEOUT));

 runas("netsh winhttp set proxy 127.0.0.1:9666")
Use backticks instead of doublequotes, than you don't have to escape the doublequotes within the string. So: alias runas = compose!(x => to!bool((cast(int) x) > 32), x => ShellExecute(null, "runas", "cmd", cast(wchar*)`/c "cd /d %s && %s"`.format(getcwd(), x).to!wstring, null, SW_HIDE);
Jul 14 2020