Created
August 15, 2016 14:03
-
-
Save icholy/f8a07551c5aa3b97521767b1a3223275 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lpcwstr | |
// #include <windows.h> | |
// #include <wchar.h> | |
// #include <WinNT.h> | |
import "C" | |
import ( | |
"unicode/utf16" | |
"unsafe" | |
) | |
const maxRunes = 1<<30 - 1 | |
func Decode(cwstr C.LPCWSTR) string { | |
ptr := unsafe.Pointer(cwstr) | |
sz := C.wcslen((*C.wchar_t)(ptr)) | |
wstr := (*[maxRunes]uint16)(ptr)[:sz:sz] | |
return string(utf16.Decode(wstr)) | |
} | |
func Encode(s string) C.LPCWSTR { | |
wstr := utf16.Encode([]rune(s)) | |
wstr = append(wstr, 0x00) | |
return (C.LPCWSTR)(unsafe.Pointer(&wstr[0])) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment