Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

MultiByteToWideChar (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern int MultiByteToWideChar(uint CodePage, uint dwFlags, string
   lpMultiByteStr, int cbMultiByte, [Out, MarshalAs(UnmanagedType.LPWStr)]
   StringBuilder lpWideCharStr, int cchWideChar);

Alternative version

    [DllImport("kernel32.dll")]
    private static extern int MultiByteToWideChar(
      uint CodePage,
      uint dwFlags,
      [MarshalAs(UnmanagedType.LPArray)] Byte[] lpMultiByteStr,
      int cbMultiByte,
      [Out, MarshalAs(UnmanagedType.LPArray)]
      Byte[] lpWideCharStr,
      int cchWideChar);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

  public class Multibyte {
    [DllImport("kernel32.dll")]
    private static extern int MultiByteToWideChar(
      uint CodePage,
      uint dwFlags,
      //string lpMultiByteStr, //
      [MarshalAs(UnmanagedType.LPArray)] Byte[] lpMultiByteStr,
      int cbMultiByte,
      [Out, MarshalAs(UnmanagedType.LPArray)]
      Byte[] lpWideCharStr,
      int cchWideChar);

    public static string ToUnicode(uint codepage, Byte[] lpMultiByteStr) {
      Byte[] lpWideCharStr = new Byte[2*lpMultiByteStr.Length];
      MultiByteToWideChar(codepage, 0, lpMultiByteStr, lpMultiByteStr.Length, lpWideCharStr, 2*lpMultiByteStr.Length);
      return System.Text.Encoding.Unicode.GetString(lpWideCharStr);
    }

public class Multibyte {

  [DllImport("kernel32.dll")]
  private static extern int MultiByteToWideChar(
    uint CodePage,
    uint dwFlags,
    [MarshalAs(UnmanagedType.LPArray)] Byte[] lpMultiByteStr,
    int cbMultiByte,
    [Out, MarshalAs(UnmanagedType.LPArray)]
      Byte[] lpWideCharStr,
    int cchWideChar);

  [SqlFunction]
  public static SqlString ConvToUnicode(SqlInt32 codepage, SqlString multibyteString) {
    bool un;
    byte[] b = (byte[])iConvToMultibyteArray(multibyteString, out un);
    if (un) return (SqlString)ToUnicode((uint)(int)codepage, b);
    else return multibyteString;
  }

  private static SqlBinary iConvToMultibyteArray(SqlString multibyteString, out bool multibyte) {
    byte[] result;
    byte[] ch = multibyteString.GetUnicodeBytes();
    if ((ch.Length >= 2) && (ch[1] == 0x00)) {
      result = new byte[ch.Length / 2];
      for (int i = 0; i < ch.Length / 2; i++) {
    result[i] = ch[i * 2];
      }
      multibyte = true;
    [SqlFunction]
    public static SqlString ConvToUnicode(SqlInt32 codepage, SqlString multibyteString) {
      byte[] b = (byte[])ConvToMultibyteArray(multibyteString);
      return (SqlString)ToUnicode((uint)(int)codepage, b);
    }
    else {
      result = ch;// new byte[ch.Length];
      multibyte = false;
    }
    return (SqlBinary)result;
  }

}

    [SqlFunction]
    public static SqlBinary ConvToMultibyteArray(SqlString multibyteString) {
      byte[] result;
      Char[] ch = ((string)multibyteString).ToCharArray();
      if ((ch.Length > 4) && (ch[1] == (char)0x00) && (ch[3] == (char)0x00)) {
    result = new byte[ch.Length / 2];
    for (int i = 0; i < ch.Length / 2; i++) {
      result[i] = (byte)ch[i * 2];
    }
      }
      else {
    result = new byte[ch.Length];
    for (int i = 0; i < ch.Length; i++) {
      result[i] = (byte)ch[i];
    }
      }
      return (SqlBinary)result;
    }
  }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions