Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 12, 2020 How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
I don't want start program with admin elevation, but ask user for admin permission when some function is called. |
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | You call ShellExecute with "runas" verb: https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea |
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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"
|
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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.
|
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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);
|
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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. |
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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.
|
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | I mean runas your own program. |
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | 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
|
July 12, 2020 Re: How can I make executeShell ask for Admin Elevation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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
|
Copyright © 1999-2021 by the D Language Foundation