Last active
October 28, 2015 09:56
-
-
Save tmplinshi/48196e26f0864003ed42 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
; utf8_to_utf16.lsp | |
; http://www.newlispfanclub.alh.net/forum/viewtopic.php?f=9&t=3656&p=18353#p18353 | |
; | |
; Example: | |
; (import "user32.dll" "MessageBoxW") | |
; (MessageBoxW 0 (utf8->16 "中文") (utf8->16 "title") 0) | |
(constant 'SIZEOF_WCHAR 2) ; assumption | |
(constant 'CP_UTF8 65001) | |
(define (utf8->16 lpMultiByteStr , cchWideChar lpWideCharStr ret) | |
(if (not MultiByteToWideChar) | |
(begin | |
(import "kernel32.DLL" "MultiByteToWideChar") | |
) | |
) | |
; calculate the size of buffer (in WCHAR's) | |
(setq cchWideChar | |
( | |
MultiByteToWideChar | |
CP_UTF8 ; from UTF-8 | |
0 ; no flags necessary | |
lpMultiByteStr | |
-1 ; convert until NULL is encountered | |
0 | |
0 | |
) | |
) | |
; allocate the buffer | |
(setq lpWideCharStr (dup " " (* cchWideChar SIZEOF_WCHAR))) | |
; convert | |
(setq ret | |
( | |
MultiByteToWideChar | |
CP_UTF8 ; from UTF-8 | |
0 ; no flags necessary | |
lpMultiByteStr | |
-1 ; convert until NULL is encountered | |
lpWideCharStr | |
cchWideChar | |
) | |
) | |
(if (> ret 0) lpWideCharStr nil) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment