Skip to content

Instantly share code, notes, and snippets.

@chitoku-k
Last active September 6, 2023 03:31
Show Gist options
  • Save chitoku-k/13d17f53d18f55419b7dc51d77766a7a to your computer and use it in GitHub Desktop.
Save chitoku-k/13d17f53d18f55419b7dc51d77766a7a to your computer and use it in GitHub Desktop.
Pause the console using GetConsoleProcessList to determine whether process is started by explorer.exe
//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zmain_windows.go main.go
//sys GetConsoleProcessList(processList []uint32) (processCount uint32, err error)
//sys SetConsoleTitle(consoleTitle string) (err error) = SetConsoleTitleW
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
err := SetConsoleTitle("Awesome Title")
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
pids := make([]uint32, 2)
count, err := GetConsoleProcessList(pids)
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
if count < 2 {
br := bufio.NewReader(os.Stdin)
br.ReadString('\n')
return
}
}
// Code generated by 'go generate'; DO NOT EDIT.
package main
import (
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var _ unsafe.Pointer
// Do the interface allocations only once for common
// Errno values.
const (
errnoERROR_IO_PENDING = 997
)
var (
errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
errERROR_EINVAL error = syscall.EINVAL
)
// errnoErr returns common boxed Errno values, to prevent
// allocations at runtime.
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return errERROR_EINVAL
case errnoERROR_IO_PENDING:
return errERROR_IO_PENDING
}
// TODO: add more here, after collecting data on the common
// error values see on Windows. (perhaps when running
// all.bat?)
return e
}
var (
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetConsoleProcessList = modkernel32.NewProc("GetConsoleProcessList")
procSetConsoleTitleW = modkernel32.NewProc("SetConsoleTitleW")
)
func GetConsoleProcessList(processList []uint32) (processCount uint32, err error) {
var _p0 *uint32
if len(processList) > 0 {
_p0 = &processList[0]
}
r0, _, e1 := syscall.Syscall(procGetConsoleProcessList.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(processList)), 0)
processCount = uint32(r0)
if processCount == 0 {
err = errnoErr(e1)
}
return
}
func SetConsoleTitle(consoleTitle string) (err error) {
var _p0 *uint16
_p0, err = syscall.UTF16PtrFromString(consoleTitle)
if err != nil {
return
}
return _SetConsoleTitle(_p0)
}
func _SetConsoleTitle(consoleTitle *uint16) (err error) {
r1, _, e1 := syscall.Syscall(procSetConsoleTitleW.Addr(), 1, uintptr(unsafe.Pointer(consoleTitle)), 0, 0)
if r1 == 0 {
err = errnoErr(e1)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment