-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
231 lines (199 loc) · 6.7 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
// import "C"
import (
"fmt"
"path"
"runtime"
"syscall"
"unicode/utf16"
"unsafe"
)
//安全加载
var D = syscall.NewLazyDLL("tqsllib2.dll")
//匹配错误
var (
errString = ""
errNoStation = "The selected station location could not be found"
)
func main() {
ret, _, err := D.NewProc("tqsl_init").Call()
if err != nil {
e := err.(syscall.Errno)
println(err.Error(), "errno =", e)
}
if ret != 0 {
fmt.Println("tqsl_init err")
return
}
fmt.Println("tqsl_init done")
var major int
var minor int
ret, _, _ = D.NewProc("tqsl_getVersion").Call(uintptr(unsafe.Pointer(&major)), uintptr(unsafe.Pointer(&minor)))
fmt.Printf("tqsl_getVersion ret=%d major=%d minor=%d \n", ret, major, minor)
errToString()
ret, _, _ = D.NewProc("tqsl_getConfigVersion").Call(uintptr(unsafe.Pointer(&major)), uintptr(unsafe.Pointer(&minor)))
fmt.Printf("tqsl_getConfigVersion ret=%d major=%d minor=%d \n", ret, major, minor)
errToString()
// var serial int32
// ret, _, _ = D.NewProc("tqsl_getSerialFromTQSLFile").
// Call(strPtr("BG5UWQ.tq6"), uintptr(unsafe.Pointer(&serial)), 0, 0, 0, 0, 0)
// fmt.Printf("tqsl_getSerialFromTQSLFile ret=%d serial=%v \n", ret, serial)
// errToString()
var userdata unsafe.Pointer
//暂时不处理回调信息
callback := syscall.NewCallback(func(r int, t uintptr, v uintptr) (ret uintptr) {
// fmt.Printf("from callback: %v\n", r)
// runtime.Gosched()
return 0
})
ret, _, _ = D.NewProc("tqsl_importTQSLFile").
Call(strPtr("BG5UWQ.tq6"), callback, uintptr(unsafe.Pointer(&userdata)))
fmt.Printf("tqsl_importTQSLFile ret=%d userinfo=%v \n", ret, userdata)
errToString()
var locp unsafe.Pointer
var callsign = "BG5UWQ"
var locationName = "emin"
// var location_name = make([]byte, 256)
ret, _, _ = D.NewProc("tqsl_getStationLocation").Call(uintptr(unsafe.Pointer(&locp)), strPtr(locationName))
fmt.Printf("tqsl_getStationLocation ret=%d %v \n", ret, locp)
errString = errToString()
if errString == errNoStation {
ret, _, _ = D.NewProc("tqsl_initStationLocationCapture").Call(uintptr(unsafe.Pointer(&locp)))
fmt.Printf("tqsl_initStationLocationCapture ret=%d \n", ret)
errToString()
ret, _, _ = D.NewProc("tqsl_setStationLocationCaptureName").Call(uintptr(unsafe.Pointer(locp)), strPtr(locationName))
fmt.Printf("tqsl_setStationLocationCaptureName ret=%d \n", ret)
errToString()
ret, _, _ = D.NewProc("tqsl_saveStationLocationCapture").Call(uintptr(unsafe.Pointer(locp)), 0)
fmt.Printf("tqsl_saveStationLocationCapture ret=%d \n", ret)
errToString()
// var locationNum int
// ret, _, _ = D.NewProc("tqsl_getNumLocationField").Call(uintptr(unsafe.Pointer(locp)), uintptr(unsafe.Pointer(&locationNum)))
// fmt.Printf("tqsl_getNumLocationField ret=%d numf=%v \n", ret, locationNum)
// errToString()
// if locationNum != 0 {
// var i int = 0
// for {
// if i == locationNum {
// break
// }
// var locationLable = make([]byte, 256)
// ret, _, _ = D.NewProc("tqsl_getLocationFieldDataLabel").
// Call(uintptr(unsafe.Pointer(locp)), intPtr(i-1), uintptr(unsafe.Pointer(&locationLable[0])), 256)
// temp := string(locationLable[:256])
// //Call Sign / DXCC Entity / Grid Square / ITU Zone / CQ Zone / IOTA ID
// fmt.Printf("tqsl_getLocationFieldDataLabel ret=%d temp=%v \n", ret, temp)
// errToString()
// i++
// }
// }
}
ret, _, _ = D.NewProc("tqsl_getLocationCallSign").
Call(uintptr(unsafe.Pointer(locp)), strPtr(callsign), intPtr(len(callsign)+1))
fmt.Printf("tqsl_getLocationCallSign ret=%d \n", ret)
errToString()
var dxcc int
ret, _, _ = D.NewProc("tqsl_getLocationDXCCEntity").
Call(uintptr(unsafe.Pointer(locp)), uintptr(unsafe.Pointer(&dxcc)))
fmt.Printf("tqsl_getLocationDXCCEntity ret=%d %v \n", ret, dxcc)
errToString()
var tqslCertList **unsafe.Pointer
var nCert unsafe.Pointer
ret, _, _ = D.NewProc("tqsl_selectCertificates").
Call(uintptr(unsafe.Pointer(&tqslCertList)), uintptr(unsafe.Pointer(&nCert)), strPtr(callsign), 0, 0, 0, 0)
fmt.Printf("tqsl_selectCertificates ret=%d \n", ret)
errToString()
var tqslConvp unsafe.Pointer
ret, _, _ = D.NewProc("tqsl_beginADIFConverter").
Call(uintptr(unsafe.Pointer(&tqslConvp)),
strPtr("test.adi"), uintptr(unsafe.Pointer(tqslCertList)),
uintptr(unsafe.Pointer(nCert)), uintptr(unsafe.Pointer(locp)))
fmt.Printf("tqsl_beginADIFConverter ret=%d %v \n", ret, tqslConvp)
errToString()
ret, _, _ = D.NewProc("tqsl_setConverterAllowDuplicates").
Call(uintptr(tqslConvp), 0)
fmt.Printf("tqsl_setConverterAllowDuplicates ret=%d \n", ret)
errToString()
// var app = make([]byte, 256)
// var p1 uintptr
// ret, _, _ = D.NewProc("tqsl_setConverterAppName").
// Call(uintptr(tqslConvp), strPtr("h"))
// fmt.Printf("tqsl_setConverterAppName ret=%d app\n", ret)
// errToString()
for {
ret, _, _ = D.NewProc("tqsl_getConverterGABBI").
Call(uintptr(tqslConvp))
fmt.Printf("tqsl_getConverterGABBI ret=%d \n", ret)
errToString()
if ret == 0 {
break
} else {
p := (*byte)(unsafe.Pointer(ret))
data := make([]byte, 0)
for *p != 0 {
data = append(data, *p)
ret += unsafe.Sizeof(byte(0))
p = (*byte)(unsafe.Pointer(ret))
}
fmt.Println(string(data))
}
}
ret, _, _ = D.NewProc("tqsl_converterCommit").
Call(uintptr(tqslConvp))
fmt.Printf("tqsl_converterCommit ret=%d \n", ret)
errToString()
ret, _, _ = D.NewProc("tqsl_endConverter").
Call(uintptr(unsafe.Pointer(&tqslConvp)))
fmt.Printf("tqsl_endConverter ret=%d \n", ret)
errToString()
ret, _, _ = D.NewProc("tqsl_endStationLocationCapture").
Call(uintptr(unsafe.Pointer(&locp)))
fmt.Printf("tqsl_endStationLocationCapture ret=%d \n", ret)
errToString()
}
func setDir() {
ret, _, _ := D.NewProc("tqsl_setDirectory").
Call(strPtr(getCurrentAbPathByCaller() + "\\data"))
fmt.Printf("tqsl_setDirectory ret=%d \n", ret)
errToString()
}
func UintPtrToString(r uintptr) string {
p := (*uint16)(unsafe.Pointer(r))
if p == nil {
return ""
}
n, end, add := 0, unsafe.Pointer(p), unsafe.Sizeof(*p)
for *(*uint16)(end) != 0 {
end = unsafe.Add(end, add)
n++
}
return string(utf16.Decode(unsafe.Slice(p, n)))
}
func errToString() string {
ret, _, _ := D.NewProc("tqsl_getErrorString").Call()
p := (*byte)(unsafe.Pointer(ret))
data := make([]byte, 0)
for *p != 0 {
data = append(data, *p)
ret += unsafe.Sizeof(byte(0))
p = (*byte)(unsafe.Pointer(ret))
}
fmt.Println(string(data))
return string(data)
}
// 获取字符串的指针
func strPtr(s string) uintptr {
return uintptr(unsafe.Pointer(syscall.StringBytePtr(s)))
}
// 获取数字的指针
func intPtr(n int) uintptr {
return uintptr(n)
}
func getCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(filename)
}
return abPath
}