|
| 1 | +package cpy3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "syscall" |
| 5 | + "unsafe" |
| 6 | +) |
| 7 | + |
| 8 | +type Pointer = uintptr |
| 9 | +type SIZE_T = uintptr |
| 10 | + |
| 11 | +const sizeOfUintPtr = unsafe.Sizeof(uintptr(0)) |
| 12 | + |
| 13 | +func uintptrToBytes(u uintptr) []byte { |
| 14 | + return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:] |
| 15 | +} |
| 16 | + |
| 17 | +//func CStrToGoStr(ustr uintptr)string{ |
| 18 | +// return copyStr(ustr,G_strlen(ustr)) |
| 19 | +//} |
| 20 | + |
| 21 | +func CBytesToGoBytes(ustr uintptr, n int) []byte { |
| 22 | + return copyBytes(ustr, n) |
| 23 | +} |
| 24 | + |
| 25 | +// 这种跟copyStr3基本一样,只是用go来处理了 |
| 26 | +func copyBytes(src uintptr, strLen int) []byte { |
| 27 | + if strLen == 0 { |
| 28 | + return nil |
| 29 | + } |
| 30 | + str := make([]byte, strLen) |
| 31 | + for i := 0; i < strLen; i++ { |
| 32 | + str[i] = *(*byte)(unsafe.Pointer(src + uintptr(i))) |
| 33 | + } |
| 34 | + return str |
| 35 | +} |
| 36 | + |
| 37 | +// 这种跟copyStr3基本一样,只是用go来处理了 |
| 38 | +func copyStr(src uintptr, strLen int) string { |
| 39 | + if strLen == 0 { |
| 40 | + return "" |
| 41 | + } |
| 42 | + str := make([]uint8, strLen) |
| 43 | + for i := 0; i < strLen; i++ { |
| 44 | + str[i] = *(*uint8)(unsafe.Pointer(src + uintptr(i))) |
| 45 | + } |
| 46 | + return string(str) |
| 47 | +} |
| 48 | + |
| 49 | +// Go的string转换为Lazarus的string |
| 50 | +func GoStrToCStr(s string) uintptr { |
| 51 | + if s == "" { |
| 52 | + return 0 |
| 53 | + } |
| 54 | + return uintptr(unsafe.Pointer(StringToUTF8Ptr(s))) |
| 55 | +} |
| 56 | + |
| 57 | +// Go的string转换为Lazarus的string |
| 58 | +func GoByteToCPtr(b []byte) uintptr { |
| 59 | + if len(b) == 0 { |
| 60 | + return 0 |
| 61 | + } |
| 62 | + return uintptr(unsafe.Pointer(&b[0])) |
| 63 | +} |
| 64 | + |
| 65 | +// 字符串到UTF8指针 |
| 66 | +func StringToUTF8Ptr(s string) *uint8 { |
| 67 | + temp := []byte(s) |
| 68 | + utf8StrArr := make([]uint8, len(temp)+1) // +1是因为Lazarus中PChar为0结尾 |
| 69 | + copy(utf8StrArr, temp) |
| 70 | + return &utf8StrArr[0] |
| 71 | +} |
| 72 | + |
| 73 | +// 字符串到UTF16指针 |
| 74 | +func StringToUTF16Ptr(s string) uintptr { |
| 75 | + p, _ := syscall.UTF16PtrFromString(s) |
| 76 | + return uintptr(unsafe.Pointer(p)) |
| 77 | +} |
| 78 | + |
| 79 | +// UTF16指针到字符串 |
| 80 | +func UTF16PtrToString(p uintptr) string { |
| 81 | + l := LstrlenW(p) |
| 82 | + if l == 0 { |
| 83 | + return "" |
| 84 | + } |
| 85 | + buff := make([]uint16, l) |
| 86 | + Memcpy(uintptr(unsafe.Pointer(&buff[0])), p, uintptr(l*2)) |
| 87 | + return syscall.UTF16ToString(buff) |
| 88 | +} |
| 89 | + |
| 90 | +func LstrlenW(lpString uintptr) int32 { |
| 91 | + r, _, _ := _lstrlenW.Call(lpString) |
| 92 | + return int32(r) |
| 93 | +} |
| 94 | +func Lstrlen(lpString uintptr) int32 { |
| 95 | + r, _, _ := _lstrlen.Call(lpString) |
| 96 | + return int32(r) |
| 97 | +} |
| 98 | +func Memcpy(dest, src Pointer, count SIZE_T) Pointer { |
| 99 | + r, _, _ := _memcpy.Call(dest, src, count) |
| 100 | + return r |
| 101 | +} |
0 commit comments