Skip to content

Instantly share code, notes, and snippets.

@icholy
Created August 15, 2016 14:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icholy/f8a07551c5aa3b97521767b1a3223275 to your computer and use it in GitHub Desktop.
Save icholy/f8a07551c5aa3b97521767b1a3223275 to your computer and use it in GitHub Desktop.
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