使用golang代码关闭windows

ijnw1ujt  于 2023-08-01  发布在  Go
关注(0)|答案(2)|浏览(157)

我正在做一个程序,自动化一些繁琐的任务,为我,程序完成后,我想关闭窗口。我知道这可以在例如C#中完成
我如何使用golang关闭窗口?

wgx48brx

wgx48brx1#

syscall包中没有“关闭操作系统”功能,因为所有操作系统都没有为此提供通用接口。
注意:有一个syscall.Shutdown()功能,但那是shutdown a socket,而不是关闭操作系统。
最简单的方法是使用os/exec包执行shutdown命令,例如:

if err := exec.Command("cmd", "/C", "shutdown", "/s").Run(); err != nil {
    fmt.Println("Failed to initiate shutdown:", err)
}

字符串
上面的命令启动了一个关机序列,通常需要1分钟才能真正关闭系统(并且有空间使用shutdown /a中止它)。您可以为shutdown命令提供不同的参数,以便不等待1分钟,而是立即继续:shutdown /t 0 /s(执行shutdown /?以获取所有选项的列表)。
还有一个Windows API调用来关闭系统:ExitWindowsEx()。它有两个参数,第一个是定义关机类型的标志(0x08表示Shuts down the system and turns off the power.),第二个是提供关机的原因。要从Go调用它,你可以这样做:

user32 := syscall.MustLoadDLL("user32")
defer user32.Release()

exitwin := user32.MustFindProc("ExitWindowsEx")

r1, _, err := exitwin.Call(0x08, 0)
if r1 != 1 {
    fmt.Println("Failed to initiate shutdown:", err)
}


但是要知道你需要SE_SHUTDOWN_NAME权限才能调用ExitWindowsEx(),否则你会得到一个错误消息:

Failed to initiate shutdown: A required privilege is not held by the client.


请参见此示例how to acquire the required privilege

1cosmwyk

1cosmwyk2#

谢谢你的帖子,非常有帮助。下面是执行重新引导完整函数。它完全遵循前面提到的Microsoft示例。应该有助于节省计算结构体的时间:

import (
    "fmt"
    "syscall"
    "unsafe"
)

// error is nil on success
func reboot() error {

    user32 := syscall.MustLoadDLL("user32")
    defer user32.Release()

    kernel32 := syscall.MustLoadDLL("kernel32")
    defer kernel32.Release()

    advapi32 := syscall.MustLoadDLL("advapi32")
    defer advapi32.Release()

    ExitWindowsEx := user32.MustFindProc("ExitWindowsEx")
    GetCurrentProcess := kernel32.MustFindProc("GetCurrentProcess")
    GetLastError := kernel32.MustFindProc("GetLastError")
    OpenProdcessToken := advapi32.MustFindProc("OpenProcessToken")
    LookupPrivilegeValue := advapi32.MustFindProc("LookupPrivilegeValueW")
    AdjustTokenPrivileges := advapi32.MustFindProc("AdjustTokenPrivileges")

    currentProcess, _, _ := GetCurrentProcess.Call()

    const tokenAdjustPrivileges = 0x0020
    const tokenQuery = 0x0008
    var hToken uintptr

    result, _, err := OpenProdcessToken.Call(currentProcess, tokenAdjustPrivileges|tokenQuery, uintptr(unsafe.Pointer(&hToken)))
    if result != 1 {
        fmt.Println("OpenProcessToken(): ", result, " err: ", err)
        return err
    }
    //fmt.Println("hToken: ", hToken)

    const SeShutdownName = "SeShutdownPrivilege"

    type Luid struct {
        lowPart  uint32 // DWORD
        highPart int32  // long
    }
    type LuidAndAttributes struct {
        luid       Luid   // LUID
        attributes uint32 // DWORD
    }

    type TokenPrivileges struct {
        privilegeCount uint32 // DWORD
        privileges     [1]LuidAndAttributes
    }

    var tkp TokenPrivileges

    result, _, err = LookupPrivilegeValue.Call(uintptr(0), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(SeShutdownName))), uintptr(unsafe.Pointer(&(tkp.privileges[0].luid))))
    if result != 1 {
        fmt.Println("LookupPrivilegeValue(): ", result, " err: ", err)
        return err
    }
    //fmt.Println("LookupPrivilegeValue luid: ", tkp.privileges[0].luid)

    const SePrivilegeEnabled uint32 = 0x00000002

    tkp.privilegeCount = 1
    tkp.privileges[0].attributes = SePrivilegeEnabled

    result, _, err = AdjustTokenPrivileges.Call(hToken, 0, uintptr(unsafe.Pointer(&tkp)), 0, uintptr(0), 0)
    if result != 1 {
        fmt.Println("AdjustTokenPrivileges() ", result, " err: ", err)
        return err
    }

    result, _, _ = GetLastError.Call()
    if result != 0 {
        fmt.Println("GetLastError() ", result)
        return err
    }

    const ewxForceIfHung = 0x00000010
    const ewxReboot = 0x00000002
    const shutdownReasonMajorSoftware = 0x00030000

    result, _, err = ExitWindowsEx.Call(ewxReboot|ewxForceIfHung, shutdownReasonMajorSoftware)
    if result != 1 {
        fmt.Println("Failed to initiate reboot:", err)
        return err
    }

    return nil
}

字符串

相关问题