KbdLayerDescriptor pVkToWcharTable returns NULL on Win64

1.6k views Asked by At

I am running out of ideas here. I have a piece of code adapted from http://thetechnofreak.com/technofreak/keylogger-visual-c/ to convert keycodes to unicode chars. It works fine in all situations except when you try to run the 32-bit version from 64-bit Windows. For some reason pKbd->pVkToWcharTable keeps returning NULL. I have tried __ptr64 as well as explicitly specifying SysWOW64 and System32 for the kbd dll path. I have found several items across the internet referring to this exact or very similar problem but I cannot seem to get any of the solutions to work (See: KbdLayerDescriptor returns NULL at 64bit architecture) The following is my test code that was compiled with mingw-32 on Windows XP (gcc -std=c99 Wow64Test.c) and then executed on Windows 7 64-bit. On Windows XP I am getting a valid pointer, however on Windows 7 I am getting NULL.

***Update: So it looks like the problems I am having are due to mingw not implementing __ptr64 correctly as the sizeof operation gives 4 bytes instead of the 8 bytes given by visual studio. So the real solution would be figuring out a way to make the size of KBD_LONG_POINTER dynamic or at least 64-bits but I am not sure if thats possible. Any ideas?

#include <windows.h>
#include <stdio.h>

#define KBD_LONG_POINTER __ptr64
//#define KBD_LONG_POINTER

typedef struct {
    BYTE Vk;
    BYTE ModBits;
} VK_TO_BIT, *KBD_LONG_POINTER PVK_TO_BIT;

typedef struct {
    PVK_TO_BIT pVkToBit;
    WORD wMaxModBits;
    BYTE ModNumber[];
} MODIFIERS, *KBD_LONG_POINTER PMODIFIERS;

typedef struct _VK_TO_WCHARS1 {
    BYTE VirtualKey;
    BYTE Attributes;
    WCHAR wch[1];
} VK_TO_WCHARS1, *KBD_LONG_POINTER PVK_TO_WCHARS1;

typedef struct _VK_TO_WCHAR_TABLE {
    PVK_TO_WCHARS1 pVkToWchars;
    BYTE nModifications;
    BYTE cbSize;
} VK_TO_WCHAR_TABLE, *KBD_LONG_POINTER PVK_TO_WCHAR_TABLE;

typedef struct {
    DWORD dwBoth;
    WCHAR wchComposed;
    USHORT uFlags;
} DEADKEY, *KBD_LONG_POINTER PDEADKEY;

typedef struct {
    BYTE vsc;
    WCHAR *KBD_LONG_POINTER pwsz;
} VSC_LPWSTR, *KBD_LONG_POINTER PVSC_LPWSTR;

typedef struct _VSC_VK {
    BYTE Vsc;
    USHORT Vk;
} VSC_VK, *KBD_LONG_POINTER PVSC_VK;

typedef struct _LIGATURE1 {
    BYTE VirtualKey;
    WORD ModificationNumber;
    WCHAR wch[1];
} LIGATURE1, *KBD_LONG_POINTER PLIGATURE1;

typedef struct tagKbdLayer {
    PMODIFIERS pCharModifiers;
    PVK_TO_WCHAR_TABLE pVkToWcharTable;
    PDEADKEY pDeadKey;
    PVSC_LPWSTR pKeyNames;
    PVSC_LPWSTR pKeyNamesExt;
    WCHAR *KBD_LONG_POINTER *KBD_LONG_POINTER pKeyNamesDead;
    USHORT *KBD_LONG_POINTER pusVSCtoVK;
    BYTE bMaxVSCtoVK;
    PVSC_VK pVSCtoVK_E0;
    PVSC_VK pVSCtoVK_E1;
    DWORD fLocaleFlags;
    BYTE nLgMax;
    BYTE cbLgEntry;
    PLIGATURE1 pLigature;
    DWORD dwType;
    DWORD dwSubType;
} KBDTABLES, *KBD_LONG_POINTER PKBDTABLES;


typedef PKBDTABLES(CALLBACK *KbdLayerDescriptor) (VOID);

int main() {
    PKBDTABLES pKbd;
    HINSTANCE kbdLibrary = NULL;

    //"C:\\WINDOWS\\SysWOW64\\KBDUS.DLL"
    //"C:\\WINDOWS\\System32\\KBDUS.DLL"
    kbdLibrary = LoadLibrary("C:\\WINDOWS\\SysWOW64\\KBDUS.DLL");

    KbdLayerDescriptor pKbdLayerDescriptor = (KbdLayerDescriptor) GetProcAddress(kbdLibrary, "KbdLayerDescriptor");

    if(pKbdLayerDescriptor != NULL) {
        pKbd = pKbdLayerDescriptor();

        printf("Is Null? %d 0x%X\n", sizeof(pKbd->pVkToWcharTable), pKbd->pVkToWcharTable);
    }

    FreeLibrary(kbdLibrary);
    kbdLibrary = NULL;
}
1

There are 1 answers

1
Zoltán Sólyom On BEST ANSWER

It might be late for you, but here is a solution for anyone having the same problem. This demo and incomplete explanation helps, but only works in Visual Studio: http://www.codeproject.com/Articles/439275/Loading-keyboard-layout-KbdLayerDescriptor-in-32-6

The pointers in the structures in kbd.h all have the KBD_LONG_POINTER macro, which is defined as *__ptr64* on 64 bit operating systems. In Visual Studio, this makes the pointers take up 8 bytes instead of the usual 4 of 32 bit programs. Unfortunately in MinGW, *__ptr64* is defined to not do anything.

As written in the linked explanation, the KbdLayerDescriptor function returns pointers differently on 32 bit and 64 bit Windows. The size of pointers seem to depend on the operating system and not on the running program. Actually, the pointers are still 4 bytes on a 64 bit operating system for a 32 bit program, but in VS, the __ptr64 keyword lies that they are not.

For example some structures look like this in kbd.h:

typedef struct {
    BYTE Vk;
    BYTE ModBits;
} VK_TO_BIT, *KBD_LONG_POINTER PVK_TO_BIT;

typedef struct {
    PVK_TO_BIT pVkToBit;
    WORD wMaxModBits;
    BYTE ModNumber[];
} MODIFIERS, *KBD_LONG_POINTER PMODIFIERS;

This can't work neither in MinGW nor in VS for 32 bit programs on 64 bit Windows. Because the pVkToBit member in MODIFIERS is only 4 bytes without __ptr64. The solution is to forget about KBD_LONG_POINTER (you could even remove them all) and define structures similar to the above. i.e. :

struct VK_TO_BIT64
{
    BYTE Vk;
    BYTE ModBits;
};

struct MODIFIERS64
{
    VK_TO_BIT64 *pVkToBit;
    int _align1;
    WORD wMaxModBits;
    BYTE ModNumber[];
};

(You could use VK_TO_BIT and not define your own VK_TO_BIT64, as they are the same, but having separate definitions help understanding what's going on.)

The member pVkToBit still takes up 4 bytes, but KbdLayerDescriptor pads pointers to 8 bytes on a 64 bit OS, so we have to insert some padding (int _align1).

You have to do the same thing for the other structures in kbd.h. For example this will replace KBDTABLES:

struct WCHARARRAY64
{
    WCHAR *str;
    int _align1;
};

struct KBDTABLES64
{
    MODIFIERS64 *pCharModifiers;
    int _align1;
    VK_TO_WCHAR_TABLE64 *pVkToWcharTable;
    int _align2;
    DEADKEY64 *pDeadKey;
    int _align3;
    VSC_LPWSTR64 *pKeyNames;
    int _align4;
    VSC_LPWSTR64 *pKeyNamesExt;
    int _align5;
    WCHARARRAY64 *pKeyNamesDead;
    int _align6;
    USHORT *pusVSCtoVK;
    int _align7;
    BYTE bMaxVSCtoVK;
    int _align8;
    VSC_VK64 *pVSCtoVK_E0;
    int _align9;
    VSC_VK64 *pVSCtoVK_E1;
    int _align10;
    DWORD fLocaleFlags;
    byte nLgMax;
    byte cbLgEntry;
    LIGATURE64_1 *pLigature;
    int _align11;
    DWORD dwType;
    DWORD dwSubType;
};

(Notice that the _align8 member does not come after a pointer.)

To use this all, you have to check whether you are running on 64 bit windows with this: http://msdn.microsoft.com/en-us/library/ms684139%28v=vs.85%29.aspx

If not, use the original structures from kbd.h, because the pointers behave correctly. They take up 4 bytes. In case the program is running on a 64 bit OS, use the structures you created. You can achieve it with this:

typedef __int64 (CALLBACK *LayerDescriptor64)(); // Result should be cast to KBDTABLES64.
typedef PKBDTABLES (CALLBACK *LayerDescriptor)(); // This is used on 32 bit OS.

static PKBDTABLES kbdtables = NULL;
static KBDTABLES64 *kbdtables64 = NULL;

And in some initialization function:

if (WindowsIs64Bit()) // Your function that checks the OS version.
{
    LayerDescriptor64 KbdLayerDescriptor = (LayerDescriptor64)GetProcAddress(kbdLibrary, "KbdLayerDescriptor");
    if (KbdLayerDescriptor != NULL)
        kbdtables64 = (KBDTABLES64*)KbdLayerDescriptor();
    else
        kbdtables64 = NULL;
}
else
{
    LayerDescriptor KbdLayerDescriptor = (LayerDescriptor)GetProcAddress(kbdLibrary, "KbdLayerDescriptor");
    if (KbdLayerDescriptor != NULL)
        kbdtables = KbdLayerDescriptor();
    else
        kbdtables = NULL;
}

This solution does not use __ptr64 at all, and works both in VS and MinGW. The things you have to watch out for are:

  1. The structures should be aligned on 8 byte boundaries. (This is the default in current VS or MinGW, at least for C++.)
  2. Don't define KBD_LONG_POINTER to __ptr64, or remove it from everywhere. Although you are better off not changing kbd.h.
  3. Understand how alignment for structure members work. (I have compiled this as C++ and not C. I'm not sure whether alignment rules would be any different for C.)
  4. Use the correct variable (either kbdtables or kbdtables64) depending on the OS.
  5. This solution is obviously not needed when compiling a 64 bit program.