digitalmars.D.learn - How can I make executeShell ask for Admin Elevation?
- Marcone (2/2) Jul 11 2020 I don't want start program with admin elevation, but ask user for
- Kagamin (2/2) Jul 12 2020 You call ShellExecute with "runas" verb:
- Marcone (3/5) Jul 12 2020 Please give me a litle simple example ruining this command:
- Marcone (2/4) Jul 12 2020 I need return code and stdout.
- Marcone (4/6) Jul 12 2020 How can I get return code ans stdout of this code:
- Kagamin (3/3) Jul 12 2020 Well, you can elevate your own program and tell it to run that
- =?UTF-8?Q?Ali_=c3=87ehreli?= (35/37) Jul 12 2020 Here is a hacky solution that attempts the command and fails back to
- Marcone (2/8) Jul 12 2020 I need for Windows, it not work on windows.
- Marcone (6/8) Jul 12 2020 alias runas = compose!(x => to!bool((cast(int) x) > 32), x =>
- aberba (2/10) Jul 13 2020 Can you put it into a small dub package?
- Marcone (3/15) Jul 13 2020 I do not know how to do that. Observation: Remove the code
- Dominikus Dittes Scherkl (6/11) Jul 14 2020 Use backticks instead of doublequotes, than you don't have to
I don't want start program with admin elevation, but ask user for admin permission when some function is called.
Jul 11 2020
You call ShellExecute with "runas" verb: https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
Jul 12 2020
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-shellexecuteaPlease give me a litle simple example ruining this command: "netsh winhttp set proxy 127.0.0.1:8888"
Jul 12 2020
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-shellexecuteaI need return code and stdout.
Jul 12 2020
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
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
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
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
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
On Sunday, 12 July 2020 at 21:50:06 UTC, Ali Çehreli wrote:On 7/11/20 7:10 PM, Marcone wrote:I need for Windows, it not work on windows.[...]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.) [...]
Jul 12 2020
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
On Monday, 13 July 2020 at 00:57:02 UTC, Marcone wrote:On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:Can you put it into a small dub package?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 13 2020
On Monday, 13 July 2020 at 12:28:24 UTC, aberba wrote:On Monday, 13 July 2020 at 00:57:02 UTC, Marcone wrote:I do not know how to do that. Observation: Remove the code .WaitForSingleObject (WAIT_TIMEOUT)On Sunday, 12 July 2020 at 02:10:11 UTC, Marcone wrote:Can you put it into a small dub package?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 13 2020
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