-
Notifications
You must be signed in to change notification settings - Fork 0
/
metricoactivate_windows.go
47 lines (36 loc) · 1.04 KB
/
metricoactivate_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
// Copyright 2018 wyDay, LLC. All rights reserved.
//go:build windows
// +build windows
package metricoactivate
/*
#cgo CFLAGS: -I .
#include "TurboActivate.h"
*/
import "C"
import (
"unicode/utf16"
"unsafe"
)
// TAStrPtrType is the data type of string pointers that will be passed
// to the TurboActivate library on this particular platform.
type TAStrPtrType *C.WCHAR
// getTAStrPtr gets the cwstring on Windows.
func getTAStrPtr(s string) TAStrPtrType {
wstr := utf16.Encode([]rune(s))
p := C.calloc(C.size_t(len(wstr)+1), 2)
pp := (*[1 << 30]uint16)(p)
copy(pp[:], wstr)
return (TAStrPtrType)(p)
}
// getTAStrBufferPtr allocates and returns a buffer of the string length (including null)
func getTAStrBufferPtr(strLen C.size_t) TAStrPtrType {
p := C.calloc(strLen, 2)
return (TAStrPtrType)(p)
}
// stringFromTAStrPtr converts ptr to a Go string
func stringFromTAStrPtr(cwstr TAStrPtrType) string {
ptr := unsafe.Pointer(cwstr)
sz := C.wcslen((*C.wchar_t)(ptr))
wstr := (*[1<<30 - 1]uint16)(ptr)[:sz:sz]
return string(utf16.Decode(wstr))
}