I use this function only to get unicode text from clipboard so it doesn't have a wrapper to save clipboard text which would be useful for conversion anywhere in your code. Example is coded using info from
eggheads.experts. Extremely fast and reliable. Tested with cyrillic (russian) names only.
Code:
clipAnsi()
{
StringLen,L,Clipboard
L:=(L+1)*4
transform,ca_Clip,unicode ; get clipboard text in UTF-8
varSetCapacity(ca_WideText,L,0) ; allocate buffer for 2-byte-char string
varSetCapacity(ca_AnsiText,L,0) ; alloc for resulting ansi string
; Convert UTF-8 to UTF-16. CP_UTF8=65001
if dllCall("MultiByteToWideChar",uint,65001, uint,0, str,ca_Clip
, unit,-1, str,ca_WideText, uint,L/2)
dllCall("WideCharToMultiByte",uint,0, uint,0, str,ca_WideText
, unit,-1, str,ca_AnsiText, uint,L/2, uint,0, uint,0)
; Convert UTF-16 to ANSI. CP_ACP=0
return ca_AnsiText
}
Another function is to set clipboard in unicode (through UTF-8 conversion) to an ansi string containing non-western characters from current system codepage.
Code:
clipSetUnicode(cu_AnsiText) ; copy ansi string to clipboard in unicode mode
{
StringLen,L,cu_AnsiText
L:=(L+1)*4
varSetCapacity(cu_WideText,L,0)
varSetCapacity(cu_UTFtext,L,0)
; ANSI to UTF-16. CP_ACP=0
if dllCall("MultiByteToWideChar",uint,0, uint,0, str,cu_AnsiText
, uint,-1, str,cu_WideText, uint,L/2)
dllCall("WideCharToMultiByte",uint,65001, uint,0, str,cu_WideText
, uint,-1, str,cu_UTFtext, uint,L/2, uint,0, uint,0)
; Convert UTF-16 to UTF-8. CP_UTF8=65001
transform,clipboard,unicode,%cu_UTFtext%
}