-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwindows.go
68 lines (56 loc) · 1.67 KB
/
windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
// +build windows
package main
import (
"syscall"
"unsafe"
)
// Useful link: https://github.com/gonutz/w32
type (
DWORD uint32
HANDLE uintptr
HWND HANDLE
)
const (
SW_HIDE = 0
)
var (
kernel32, _ = syscall.LoadLibrary("kernel32.dll")
getConsoleWindow, _ = syscall.GetProcAddress(kernel32, "GetConsoleWindow")
getCurrentProcessId, _ = syscall.GetProcAddress(kernel32, "GetCurrentProcessId")
user32, _ = syscall.LoadLibrary("user32.dll")
getWindowThreadProcessId, _ = syscall.GetProcAddress(user32, "GetWindowThreadProcessId")
showWindowAsync, _ = syscall.GetProcAddress(user32, "ShowWindowAsync")
)
func GetConsoleWindow() HWND {
ret, _, _ := syscall.Syscall(uintptr(getConsoleWindow), 0, 0, 0, 0)
return HWND(ret)
}
func GetWindowThreadProcessId(hwnd HWND) (HANDLE, DWORD) {
var processId DWORD
ret, _, _ := syscall.Syscall(uintptr(getWindowThreadProcessId), 2,
uintptr(hwnd),
uintptr(unsafe.Pointer(&processId)), 0)
return HANDLE(ret), processId
}
func GetCurrentProcessId() DWORD {
id, _, _ := syscall.Syscall(uintptr(getCurrentProcessId), 0, 0, 0, 0)
return DWORD(id)
}
func ShowWindowAsync(hwnd HWND, cmdshow int) bool {
ret, _, _ := syscall.Syscall(uintptr(showWindowAsync), 2,
uintptr(hwnd),
uintptr(cmdshow), 0)
return ret != 0
}
func hideConsole() {
console := GetConsoleWindow()
if console != 0 {
_, consoleProcID := GetWindowThreadProcessId(console)
if GetCurrentProcessId() == consoleProcID {
ShowWindowAsync(console, SW_HIDE)
}
}
}