Home > Backend Development > Golang > How to Programmatically Request Administrator Privileges in Windows with Go?

How to Programmatically Request Administrator Privileges in Windows with Go?

Linda Hamilton
Release: 2024-11-08 20:37:02
Original
1001 people have browsed it

How to Programmatically Request Administrator Privileges in Windows with Go?

Programmatically Request Administrator Privileges in Windows with Go

Running applications without administrative privileges can limit their functionality. This article provides a solution to the challenge of automating the process of requesting and obtaining administrator permissions in Windows using Go.

To illustrate the issue, consider the following code that attempts to write to a file in the Windows directory:

package main

import (
    "fmt"
    "io/ioutil"
    "time"
)

func main() {
    err := ioutil.WriteFile("C:/Windows/test.txt", []byte("TESTING!"), 0644)
    if err != nil {
        fmt.Println(err.Error())
        time.Sleep(time.Second * 3)
    }
}
Copy after login

If you compile and run this code, it will fail with the error "Access is denied." This is because the process is not running with elevated privileges.

To resolve this issue, you can implement a technique that detects whether you are running as an administrator and, if not, relaunches the application with a UAC (User Account Control) prompt. This will allow the application to run as a standard user most of the time, only elevating when necessary.

package main

import (
    "fmt"
    "golang.org/x/sys/windows"
    "os"
    "syscall"
    "time"
)

func main() {
    // if not elevated, relaunch by shellexecute with runas verb set
    if !amAdmin() {
        runMeElevated()
    }

    time.Sleep(10 * time.Second)
}

func runMeElevated() {
    verb := "runas"
    exe, _ := os.Executable()
    cwd, _ := os.Getwd()
    args := strings.Join(os.Args[1:], " ")

    verbPtr, _ := syscall.UTF16PtrFromString(verb)
    exePtr, _ := syscall.UTF16PtrFromString(exe)
    cwdPtr, _ := syscall.UTF16PtrFromString(cwd)
    argPtr, _ := syscall.UTF16PtrFromString(args)

    var showCmd int32 = 1 //SW_NORMAL

    err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd)
    if err != nil {
        fmt.Println(err)
    }
}

func amAdmin() bool {
    _, err := os.Open("\\.\PHYSICALDRIVE0")
    if err != nil {
        fmt.Println("admin no")
        return false
    }
    fmt.Println("admin yes")
    return true
}
Copy after login

This solution provides a convenient way to automatically elevate your application's privileges when necessary, without the need for a manifest or any manual user action like right-clicking and selecting "Run as administrator."

The above is the detailed content of How to Programmatically Request Administrator Privileges in Windows with Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template