├── .gitignore ├── APP.EXE.manifest ├── README.md └── app.go /.gitignore: -------------------------------------------------------------------------------- 1 | ### Go template 2 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 3 | *.o 4 | *.a 5 | *.so 6 | 7 | # Folders 8 | _obj 9 | _test 10 | 11 | # Architecture specific extensions/prefixes 12 | *.[568vq] 13 | [568vq].out 14 | 15 | *.cgo1.go 16 | *.cgo2.c 17 | _cgo_defun.c 18 | _cgo_gotypes.go 19 | _cgo_export.* 20 | 21 | _testmain.go 22 | 23 | *.exe 24 | *.test 25 | *.prof 26 | 27 | # Created by .ignore support plugin (hsz.mobi) 28 | 29 | app.syso 30 | app.exe -------------------------------------------------------------------------------- /APP.EXE.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | Golang example of using a manifest file to prompt "Run as administrator" 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # run-as-admin 2 | Golang example of using a manifest file to prompt "Run as administrator" 3 | 4 | ## Build app with manifest 5 | 6 | Install [rsrc](https://github.com/akavel/rsrc) 7 | tool for embedding binary resources in Go programs 8 | 9 | go get github.com/akavel/rsrc 10 | 11 | Generates a .syso file with embedded resources 12 | 13 | rsrc -manifest app.exe.manifest -o app.syso 14 | 15 | Build executable 16 | 17 | GOOS="windows" GOARCH="386" go build -o app.exe 18 | 19 | 20 | ## Reference 21 | 22 | [how-to-ask-for-administer-privileges-on-windows-with-go](http://stackoverflow.com/questions/31558066/how-to-ask-for-administer-privileges-on-windows-with-go) 23 | 24 | [check-if-application-is-running-as-administrator-in-golang](http://stackoverflow.com/questions/27366298/check-if-application-is-running-as-administrator-in-golang) 25 | 26 | 27 | # Without a manifest 28 | 29 | See [this technique](https://stackoverflow.com/a/59147866/639133), "...run as a standard user in most cases, and only elevate when needed. I use this in command line tools where most functions don't need admin rights" 30 | 31 | -------------------------------------------------------------------------------- /app.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("You should have seen a dialogue promting for permissions") 9 | fmt.Println("") 10 | fmt.Println("Press any key to continue") 11 | fmt.Scanln() 12 | } 13 | 14 | --------------------------------------------------------------------------------