golang windows 判断是否锁屏:

package osapi

import (
	"syscall"
	"unsafe"

	"github.com/lxn/win"
)

const (
	DESKTOP_SWITCHDESKTOP = 0x0100 // The access to the desktop
)

// get desktop locked status
func ScreenIsLocked() bool {

	// load user32.dll only once
	user32 := win.MustLoadLibrary("user32.dll")

	openDesktop := win.MustGetProcAddress(user32, "OpenDesktopW")
	closeDesktop := win.MustGetProcAddress(user32, "CloseDesktop")
	switchDesktop := win.MustGetProcAddress(user32, "SwitchDesktop")

	var lpdzDesktopPtr uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("Default"))) //string
	var dwFloatsPtr uintptr = 0                                                               //uint32
	var fInheritPtr uintptr = 0                                                               //boolean
	var dwDesiredAccessPtr uintptr = uintptr(DESKTOP_SWITCHDESKTOP)                           //uint32

	r1, _, _ := syscall.Syscall6(openDesktop, 4, lpdzDesktopPtr, dwFloatsPtr, fInheritPtr, dwDesiredAccessPtr, 0, 0)
	if r1 == 0 {
		panic("get desktop locked status error")
	}

	res, _, _ := syscall.Syscall(switchDesktop, 1, r1, 0, 0)
	// clean up
	syscall.Syscall(closeDesktop, 1, r1, 0, 0)

	return res != 1
}
posted on 2016-02-27 21:35  一头野兽  阅读(832)  评论(0编辑  收藏  举报