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,
    [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;
    }
    else {
      result = ch;// new byte[ch.Length];
      multibyte = false;
    }
    return (SqlBinary)result;
  }

}

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