Created
October 23, 2013 03:15
-
-
Save horsley/7112070 to your computer and use it in GitHub Desktop.
windows下5秒关闭屏幕
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ScreenCloser project main.go | |
package main | |
import ( | |
"syscall" | |
"time" | |
) | |
const ( | |
SC_MONITORPOWER = 0xF170 | |
WM_SYSCOMMAND = 274 | |
HWND_BROADCAST = 0xFFFF | |
) | |
func main() { | |
time.Sleep(time.Second * 5) | |
libuser32 := MustLoadLibrary("user32.dll") | |
sendMessage := MustGetProcAddress(libuser32, "SendMessageW") | |
syscall.Syscall6(sendMessage, 4, HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2, 0, 0) | |
} | |
func MustLoadLibrary(name string) uintptr { | |
lib, err := syscall.LoadLibrary(name) | |
if err != nil { | |
panic(err) | |
} | |
return uintptr(lib) | |
} | |
func MustGetProcAddress(lib uintptr, name string) uintptr { | |
addr, err := syscall.GetProcAddress(syscall.Handle(lib), name) | |
if err != nil { | |
panic(err) | |
} | |
return uintptr(addr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment