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

ToUnicode (user32)
 
.
Summary
Summary

C# Signature:

[DllImport("user32.dll")]
static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte []
   lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff,
   int cchBuff, uint wFlags, IntPtr dwhkl);

VB.Net Signature:

    Public Declare Function ToUnicodeEx Lib "user32" (
        wVirtKey As UInteger,
        wScanCode As UInteger,
        lpKeyState As Byte(),
        <Out()>
        <MarshalAs(UnmanagedType.LPWStr, SizeConst:=64)>
        ByVal lpChar As System.Text.StringBuilder,
        cchBuff As Integer,
        wFlags As UInteger,
        dwhkl As IntPtr) As Integer

static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte [] lpKeyState,
   [Out, MarshalAs(UnmanagedType.LPWStr,SizeConst=64)] StringBuilder pwszBuff, int cchBuff,
   uint wFlags);

User-Defined Types:

None.

Notes:

Convert a ASCI Character into Unicode Character with the Keyboard Layout

Depending on the actual Size of the String which needs to be translated, one must be careful to adjust the SizeConst of the pwszBuff. I had weird memory problems until I nailed the size which seems plausible because the size is passed with the parameter cchBuff anyway.

Tips & Tricks:

I've had managed heap corruption when calling this, even when the appropriate StringBuilder.Capacity is provided as the cchBuff parameter, unless the SizeConst is also specified on the MarshalAs attribute. If someone more knowledgeable can provide additional information, please edit this.

Alternatively:

[DllImport("USER32.DLL", CharSet=CharSet.Unicode)]
public static extern int ToUnicode(uint virtualKey, uint scanCode, byte[] keyStates, [MarshalAs(UnmanagedType.LPArray)] [Out] char[] chars, int charMaxCount, uint flags);

Sample Code:

and pass it char[] instead of StringBuilder.

    [DllImport("user32.dll")]
    static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte [] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

Tips & Tricks:

The trick is to make sure that "chars" is marshaled as UNICODE string. This can be done either by setting UnmanagedType.LPWStr or by CharSet=CharSet.Unicode.

    [DllImport("user32.dll")]
    static extern bool GetKeyboardState(byte [] lpKeyState);

Sample Code:

    readonly byte[] keyStates = new byte[0x100];
    readonly char[] chars = new char[1];

    [DllImport("user32.dll")]
    static extern uint MapVirtualKey(uint uCode, uint uMapType);
    char OnKeyEvent(uint wVirtKey, uint wScanCode) {
        if (GetKeyboardState(m_keyStates)) {
        int r = ToUnicode(wVirtKey, wScanCode, keyStates, chars, chars.Length, 0);
        if (r == -1) {
            // ("ToUnicode: dead key");
        } else if (r == 0) {
            // ("ToUnicode: key has no Unicode value");
        } else if (r == 1) {
            return m_chars[0];
        } else {
            // ("ToUnicode: unexpected return value: " + r);
        }
        }
        return '\0';
    }

    [DllImport("user32.dll")]
    static extern IntPtr GetKeyboardLayout(uint idThread);
    [DllImport("USER32.DLL")]
    public static extern bool GetKeyboardState([MarshalAs(UnmanagedType.LPArray)] [Out] byte[] values);

    public static string VKCodeToUnicode(uint VKCode)
    {
    System.Text.StringBuilder sbString = new System.Text.StringBuilder();
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern int ToUnicode(uint virtualKey, uint scanCode, byte[] keyStates, [MarshalAs(UnmanagedType.LPArray)] [Out] char[] chars, int charMaxCount, uint flags);

    byte[] bKeyState  = new byte[255];
    bool bKeyStateStatus = GetKeyboardState(bKeyState);
    if (!bKeyStateStatus)
        return "";
    uint lScanCode = MapVirtualKey(VKCode,0);
    IntPtr HKL = GetKeyboardLayout(0);

    ToUnicodeEx(VKCode, lScanCode, bKeyState, sbString, (int)5, (uint)0, HKL);
    return sbString.ToString();
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
ToUnicodeEx on MSDN
Documentation
ToUnicode on MSDN

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