Declare Function WideCharToMultiByte Lib "kernel32" (ByVal codepage As Long, ByVal dwFlags As Long, lpWideCharStr As Any, ByVal cchWideChar As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
WideCharToMultiByte преобразует заданную строку Unicode в ANSI на основании заданной кодовой страницы
В успешном случае возвращается количество символов в lpMultiByteStr или необходимая длина буфера, если параметр cchMultiByte равен 0. В случае ошибки возвращается 0 (используйте GetLastError для получения кода ошибки)
CP_ACP = 0 | Кодовая страница ANSI |
CP_MACCP | Кодовая страница Macintosh |
CP_OEMCP | Кодовая страница OEM |
CP_SYMBOL | Windows 2000 и выше: Кодовая страница SYMBOL |
CP_THREAD_ACP | Windows 2000 и выше: Кодовая страница ANSI для текущей нити |
CP_UTF7 | Windows 98/Me/NT 4.0/2000 и выше: Translate using UTF-7. When this is set, lpDefaultChar and lpUsedDefaultChar must be NULL |
CP_UTF8 | Windows 98/Me/NT 4.0/2000 и выше: Translate using UTF-8. When this is set, dwFlags must be zero and both lpDefaultChar and lpUsedDefaultChar must be NULL |
WC_NO_BEST_FIT_CHARS | Windows 2000 or later: Any Unicode characters that do not translate directly to multibyte equivalents will be translated to the default character (see lpDefaultChar parameter). In other words, if translating from Unicode to multibyte and back to Unicode again does not yield the exact same Unicode character, the default character is used. This flag may be used by itself or in combination with the other dwFlag options |
WC_COMPOSITECHECK | Convert composite characters to precomposed characters |
WC_DISCARDNS | Discard nonspacing characters during conversion |
WC_SEPCHARS | Generate separate characters during conversion. This is the default conversion behavior |
WC_DEFAULTCHAR | Replace exceptions with the default character during conversion |
' Возвращает ANSI-строку из указателя на Unicode-строку Public Function GetStrFromPtrW(lpszW As Long) As String Dim sRtn As String sRtn = String$(lstrlenW(ByVal lpszW) * 2, 0) ' 2 bytes/char ' WideCharToMultiByte также возвращает длину Unicode-строки ' sRtn = String$(WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, 0, 0, 0, 0), 0) Call WideCharToMultiByte(CP_ACP, 0, ByVal lpszW, -1, ByVal sRtn, Len(sRtn), 0, 0) GetStrFromPtrW = GetStrFromBufferA(sRtn) End Function Public Function GetStrFromBufferA(sz As String) As String If InStr(sz, vbNullChar) Then GetStrFromBufferA = Left$(sz, InStr(sz, vbNullChar) - 1) Else ' If sz had no null char, the Left$ function ' above would return a zero length string (""). GetStrFromBufferA = sz End If End Function