-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·146 lines (126 loc) · 4.18 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
//go:generate goversioninfo -icon=assets/WiFi-export-import.ico -manifest=assets/goversioninfo.exe.manifest
package main
import (
"fmt"
"os/exec"
"runtime"
"syscall"
"time"
"github.com/rodrigocfd/windigo/ui"
"github.com/rodrigocfd/windigo/win"
"github.com/rodrigocfd/windigo/win/co"
"github.com/rodrigocfd/windigo/win/com/com"
"github.com/rodrigocfd/windigo/win/com/com/comco"
"github.com/rodrigocfd/windigo/win/com/shell"
"github.com/rodrigocfd/windigo/win/com/shell/shellco"
)
var Version = "dev"
func main() {
runtime.LockOSThread()
myWindow := NewMyWindow() // instantiate
myWindow.wnd.RunAsMain() // ...and run
}
// This struct represents our main window.
type MyWindow struct {
wnd ui.WindowMain
btnExport ui.Button
btnImportAllUsers ui.Button
lblInfo ui.Static
}
func (me *MyWindow) exportProfilesFunction() {
fod := shell.NewIFileOpenDialog(
com.CoCreateInstance(
shellco.CLSID_FileOpenDialog, nil,
comco.CLSCTX_INPROC_SERVER,
shellco.IID_IFileOpenDialog),
)
fod.SetOptions(fod.GetOptions() | shellco.FOS_PICKFOLDERS)
isDirectorySelected := fod.Show(me.wnd.Hwnd())
if isDirectorySelected {
results := fod.ListResultDisplayNames(shellco.SIGDN_FILESYSPATH)
if len(results) > 0 {
cmd := exec.Command("netsh", "wlan", "export", "profile", "key=clear", fmt.Sprintf("folder=%s", results[0]))
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
stdout, err := cmd.Output()
if err != nil {
me.wnd.Hwnd().MessageBox(err.Error(), "Error during exporting", co.MB_ICONERROR)
} else {
me.wnd.Hwnd().MessageBox(string(stdout), "Profiles exported", co.MB_ICONINFORMATION)
}
}
}
fod.Release()
}
func (me *MyWindow) importProfilesFunction() {
fod := shell.NewIFileOpenDialog(
com.CoCreateInstance(
shellco.CLSID_FileOpenDialog, nil,
comco.CLSCTX_INPROC_SERVER,
shellco.IID_IFileOpenDialog),
)
fod.SetOptions(fod.GetOptions() | shellco.FOS_ALLOWMULTISELECT)
fod.SetFileTypes([]shell.FilterSpec{
{Name: "XML files", Spec: "*.xml"},
{Name: "All files", Spec: "*.*"},
})
isDirectorySelected := fod.Show(me.wnd.Hwnd())
if isDirectorySelected {
filePaths := fod.ListResultDisplayNames(shellco.SIGDN_FILESYSPATH)
if len(filePaths) > 0 {
stdOutAll := ""
errAll := ""
for _, filePath := range filePaths {
cmd := exec.Command("netsh", "wlan", "add", "profile", fmt.Sprintf("filename=%s", filePath), "user=all")
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
stdout, err := cmd.CombinedOutput()
if err != nil {
errAll = errAll + "Error during importing profile: " + filePath + "\n\n" + err.Error() + "\n" + string(stdout)
} else {
stdOutAll = stdOutAll + string(stdout)
}
}
if stdOutAll != "" {
me.wnd.Hwnd().MessageBox(string(stdOutAll), "Profiles sucessfully imported", co.MB_ICONINFORMATION)
}
if errAll != "" {
me.wnd.Hwnd().MessageBox(errAll, "Errors during importing profiles", co.MB_ICONERROR)
}
}
}
fod.Release()
}
// Creates a new instance of our main window.
func NewMyWindow() *MyWindow {
wnd := ui.NewWindowMain(
ui.WindowMainOpts().
Title("Wifi-export-import").
ClientArea(win.SIZE{Cx: 220, Cy: 142}).
IconId(2), // ID of icon resource, see resources folder
)
me := &MyWindow{
wnd: wnd,
btnExport: ui.NewButton(wnd,
ui.ButtonOpts().
Text("&Export WiFi profiles").
Position(win.POINT{X: 10, Y: 8}).
Size(win.SIZE{Cx: 200, Cy: 39}),
),
btnImportAllUsers: ui.NewButton(wnd,
ui.ButtonOpts().
Text("&Import WiFi profiles").
Position(win.POINT{X: 10, Y: 48}).
Size(win.SIZE{Cx: 200, Cy: 39}),
),
lblInfo: ui.NewStatic(wnd,
ui.StaticOpts().
Text(fmt.Sprintf(" WiFi-export-import (%s)\n 2022-%d Jan Taczanowski\n taczanowski.net", Version, time.Now().Year())).
Position(win.POINT{X: 10, Y: 91}).
Size(win.SIZE{Cx: 200, Cy: 62}),
),
}
me.btnExport.On().BnClicked(me.exportProfilesFunction)
me.btnImportAllUsers.On().BnClicked(func() {
me.importProfilesFunction()
})
return me
}