-
Notifications
You must be signed in to change notification settings - Fork 12
/
namedpipeapi.go
99 lines (88 loc) · 2.34 KB
/
namedpipeapi.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
//go:build windows
package winapi
import (
"errors"
"syscall"
"time"
"unsafe"
)
// Define the dwOpenMode values for CreateNamedPipe
const (
PIPE_ACCESS_INBOUND = 0x00000001
PIPE_ACCESS_OUTBOUND = 0x00000002
PIPE_ACCESS_DUPLEX = 0x00000003
)
// Define the Named Pipe End flags for GetNamedPipeInfo
const (
PIPE_CLIENT_END = 0x00000000
PIPE_SERVER_END = 0x00000001
)
// Define the dwPipeMode values for CreateNamedPipe
const (
PIPE_WAIT = 0x00000000
PIPE_NOWAIT = 0x00000001
PIPE_READMODE_BYTE = 0x00000000
PIPE_READMODE_MESSAGE = 0x00000002
PIPE_TYPE_BYTE = 0x00000000
PIPE_TYPE_MESSAGE = 0x00000004
PIPE_ACCEPT_REMOTE_CLIENTS = 0x00000000
PIPE_REJECT_REMOTE_CLIENTS = 0x00000008
)
// Define the well known values for CreateNamedPipe nMaxInstances
const PIPE_UNLIMITED_INSTANCES = 255
func ConnectNamedPipe(hNamedPipe HANDLE, po *OVERLAPPED) (err error) {
r1, _, e1 := syscall.Syscall(procConnectNamedPipe.Addr(), 2,
uintptr(hNamedPipe), uintptr(unsafe.Pointer(po)), 0)
if r1 == 0 {
wec := WinErrorCode(e1)
if wec != 0 {
err = wec
} else {
err = errors.New("ConnectNamedPipe failed.")
}
}
return
}
func CreateNamedPipe(
name string,
openMode uint32,
pipeMode uint32,
maxInstances uint32,
outBufferSize uint32,
inBufferSize uint32,
defaultTimeOut time.Duration,
sa *SECURITY_ATTRIBUTES) (h HANDLE, err error) {
pName, err := syscall.UTF16PtrFromString(name)
if err != nil {
return
}
dto := uint32(uint64(defaultTimeOut) / 1e6)
h, err = _CreateNamedPipe(pName, openMode, pipeMode, maxInstances,
outBufferSize, inBufferSize, dto, sa)
return
}
func _CreateNamedPipe(pName *uint16, dwOpenMode uint32, dwPipeMode uint32,
nMaxInstances uint32, nOutBufferSize uint32, nInBufferSize uint32,
nDefaultTimeOut uint32, pSecurityAttributes *SECURITY_ATTRIBUTES) (h HANDLE, err error) {
r1, _, e1 := syscall.Syscall9(procCreateNamedPipe.Addr(), 8,
uintptr(unsafe.Pointer(pName)),
uintptr(dwOpenMode),
uintptr(dwPipeMode),
uintptr(nMaxInstances),
uintptr(nOutBufferSize),
uintptr(nInBufferSize),
uintptr(nDefaultTimeOut),
uintptr(unsafe.Pointer(pSecurityAttributes)),
0)
if h == INVALID_HANDLE_VALUE {
wec := WinErrorCode(e1)
if wec != 0 {
err = wec
} else {
err = errors.New("CreateNamedPipe failed.")
}
} else {
h = HANDLE(r1)
}
return
}