-
Notifications
You must be signed in to change notification settings - Fork 45
/
notification_windows.go
135 lines (116 loc) · 3.3 KB
/
notification_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014 The dogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// https://msdn.microsoft.com/en-us/library/aa365261(v=vs.85).aspx
package main
import (
"syscall"
"unsafe"
)
var INFINITE int32 = -1
// maximum number of jobs to run at once
const (
MAXBG int32 = 4
)
// indicates the event that caused the function to return
var (
WAIT_OBJECT_0 uint32 = 0
WAIT_ABANDONED_0 uint32 = 0x00000080
WAIT_TIMEOUT uint32 = 0x00000102
WAIT_FAILED uint32 = 0xFFFFFFFF
)
// The filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values.
var (
FILE_NOTIFY_CHANGE_FILE_NAME uint32 = 0x00000001
FILE_NOTIFY_CHANGE_DIR_NAME uint32 = 0x00000002
FILE_NOTIFY_CHANGE_ATTRIBUTES uint32 = 0x00000004
FILE_NOTIFY_CHANGE_SIZE uint32 = 0x00000008
FILE_NOTIFY_CHANGE_LAST_WRITE uint32 = 0x00000010
FILE_NOTIFY_CHANGE_SECURITY uint32 = 0x00000100
)
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
var (
procFindFirstChangeNotification = kernel32.NewProc("FindFirstChangeNotificationW")
procFindNextChangeNotification = kernel32.NewProc("FindNextChangeNotification")
procFindCloseChangeNotification = kernel32.NewProc("FindCloseChangeNotification")
procWaitForMultipleObjects = kernel32.NewProc("WaitForMultipleObjects")
)
func boolToUint32(b bool) uint32 {
if b {
return 1
} else {
return 0
}
}
// HANDLE WINAPI FindFirstChangeNotification(
// _In_ LPCTSTR lpPathName,
// _In_ BOOL bWatchSubtree,
// _In_ DWORD dwNotifyFilter
// );
func FindFirstChangeNotification(pathName string, watchSubTree bool, mask uint32) (handle syscall.Handle, err error) {
r1, _, e1 := syscall.Syscall(
procFindFirstChangeNotification.Addr(),
3,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(pathName))),
uintptr(boolToUint32(watchSubTree)),
uintptr(mask))
handle = syscall.Handle(r1)
if handle == syscall.InvalidHandle {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
// BOOL WINAPI FindNextChangeNotification(
// _In_ HANDLE hChangeHandle
// );
func FindNextChangeNotification(handle syscall.Handle) (b bool) {
r1, _, _ := syscall.Syscall(
procFindNextChangeNotification.Addr(),
1,
uintptr(handle),
0,
0)
b = r1 != 0
return
}
// BOOL WINAPI FindCloseChangeNotification(
// _In_ HANDLE hChangeHandle
// );
func FindCloseChangeNotification(handle syscall.Handle) (b bool) {
// call 1
// r1, _, _ := syscall.Syscall(procFindCloseChangeNotification.Addr(), 1, uintptr(handle), 0, 0)
// call 2
r1, _, _ := procFindCloseChangeNotification.Call(uintptr(handle))
b = r1 != 0
return
}
// DWORD WINAPI WaitForMultipleObjects(
// _In_ DWORD nCount,
// _In_ const HANDLE *lpHandles,
// _In_ BOOL bWaitAll,
// _In_ DWORD dwMilliseconds
// );
func WaitForMultipleObjects(count uint32, handles *[4]syscall.Handle, waitAll bool, milliseconds int32) (status uint32, err error) {
r1, _, e1 := syscall.Syscall6(
procWaitForMultipleObjects.Addr(),
4,
uintptr(count),
uintptr(unsafe.Pointer(handles)),
uintptr(boolToUint32(waitAll)),
uintptr(milliseconds),
0,
0)
status = uint32(r1)
if status == WAIT_FAILED {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}