MultiByteToWideChar (Kernel32)



Usage

    Private Const CP_ACP As Integer = 0          ' Default ANSI code page.
    Private Const CP_OEMCP As Integer = 1
    Private Const CP_UTF8 As Integer = 65001      ' UTF8.
    Private Const CP_UTF16_LE As Integer = 1200       ' UTF16 - little endian.
    Private Const CP_UTF16_BE As Integer = 1201       ' UTF16 - big endian.
    Private Const CP_UTF32_LE As Integer = 12000      ' UTF32 - little endian.
    Private Const CP_UTF32_BE As Integer = 12001      ' UTF32 - big endian.

    Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Integer, ByVal dwFlags As Integer, ByVal lpMultiByteStr As IntPtr, ByVal cchMultiByte As Integer, ByVal lpWideCharStr As IntPtr, ByVal cchWideChar As Integer) As Integer

    Private Function StrPtr(MyString) As IntPtr
        ' GC handle instance

        Dim gh As GCHandle = GCHandle.Alloc(MyString, GCHandleType.Pinned)
        ' get address of variable

        Dim AddrOfMyString As IntPtr = gh.AddrOfPinnedObject()
        StrPtr = AddrOfMyString
        ' free the handle and unpin variable

        gh.Free()

    End Function

    Dim ret As Integer
    Dim strMByte As String = "12345"
    Dim strWide As String = Space(10)

    ret = MultiByteToWideChar(CP_UTF8, 0, StrPtr(strMByte), Len(strMByte) * 2, StrPtr(strWide), Len(strWide))