-
Notifications
You must be signed in to change notification settings - Fork 0
/
netuse.go
84 lines (76 loc) · 1.85 KB
/
netuse.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
package netresource
import (
"unsafe"
"golang.org/x/sys/windows"
)
var procWNetAddConnection2W = mpr.NewProc("WNetAddConnection2W")
var procWNetCancelConnection = mpr.NewProc("WNetCancelConnection2W")
// WNetAddConnection2 makes a connection to a network resource
// and can redirect a local device to the network resource
// remote - UNC-Path like `\\localhost\C$`
// local - local-drive like `X:`
// user - username. When it is "", default username is used.
// pass - password. When it is "", default passwors is used.
func WNetAddConnection2(remote, local, user, pass string) (err error) {
var rs NetResource
rs.localName, err = windows.UTF16PtrFromString(local)
if err != nil {
return
}
rs.remoteName, err = windows.UTF16PtrFromString(remote)
if err != nil {
return
}
var _user *uint16
if user == "" {
_user = nil
} else {
_user, err = windows.UTF16PtrFromString(user)
if err != nil {
return
}
}
var _pass *uint16
if pass == "" {
_pass = nil
} else {
_pass, err = windows.UTF16PtrFromString(pass)
if err != nil {
return
}
}
rc, _, err := procWNetAddConnection2W.Call(
uintptr(unsafe.Pointer(&rs)),
uintptr(unsafe.Pointer(_pass)),
uintptr(unsafe.Pointer(_user)),
0)
if rc != 0 {
return err
}
return nil
}
// WNetCancelConnection2 cancels an existing network connection.
// update - true: updates connection as not a persistent one
// force - true: disconnect even if open process exists.
func WNetCancelConnection2(name string, update bool, force bool) error {
_name, err := windows.UTF16PtrFromString(name)
if err != nil {
return err
}
var _update uintptr
if update {
_update = CONNECT_UPDATE_PROFILE
}
var _force uintptr
if force {
_force = 1
}
rc, _, err := procWNetCancelConnection.Call(
uintptr(unsafe.Pointer(_name)),
_update,
_force)
if rc != 0 {
return err
}
return nil
}