digitalmars.D.learn - File Exception, Access is denied - prompting for administrator's
Hello everyone, I'm Windows 10 as of right now, I have this situation where I would like to update /hosts file, however sometimes I do not have the required permissions, since sometimes I forget to run the .d script with administrator's rights. Is there any way to prompt for administrator's rights, to save the additional effort of lots of clicks? import std.file; void main(){ append("C:/Windows/System32/drivers/etc/hosts", "hahaha"); } std.file.FileException std\file.d(859): C:/Windows/System32/drivers/etc/hosts: Access is denied. ---------------- 0x00404223 0x00402295 0x00403751 0x004035EB 0x00402DE7 0x75688494 in BaseThreadInitThunk 0x773741C8 in RtlAreBitsSet 0x77374198 in RtlAreBitsSet
Jun 04 2019
On Tuesday, 4 June 2019 at 16:30:41 UTC, BoQsc wrote:Is there any way to prompt for administrator's rights, to save the additional effort of lots of clicks?Looking at: https://stackoverflow.com/questions/31844584/is-there-an-api-call-to-prompt-user-for-uac-elevation You can try something like this: ``` import core.sys.windows; SHELLEXECUTEINFO shex; shex.cbSize = shex.sizeof; shex.lpVerb = "runas"; // runas, open shex.lpFile = "rdmd"; shex.lpParameters = `"--eval=copy("""test.txt""","""C:/Program Files/test.txt""")"`; shex.nShow = SW_SHOW; //SW_HIDE; shex.fMask = SEE_MASK_NOCLOSEPROCESS; BOOL retshx = ShellExecuteEx(&shex); import core.Thread: Thread, msecs; // Give it 5 seconds to finish if (retshx) { int count = 0; DWORD exitCode; do { if (!GetExitCodeProcess(shex.hProcess, &exitCode)) break; if (count++ < 50) { Thread.sleep(100.msecs); } else break; } while (exitCode != STATUS_WAIT_0); // STILL_ACTIVE CloseHandle(shex.hProcess); } ``` Here I use rdmd with --eval, but you can use any program that does the file handling you want. Note that lpParameters has weird escape rules: parameters are space separated unless quoted with ", and to get actual quotes you use three of them like """.
Jun 04 2019