RegQueryValueEx programcrash on 64-Bit

Hello,

I'm trying to get a value from the registry on Windows 7 64-Bit, but if I'm using the Winapi function "RegQueryValueEx" the programm always crash.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int getKeyboardLayoutFile(char* layoutFile, DWORD bufferSize) {
  HKEY hKey;
  DWORD varType = REG_SZ;

  char kbdName[KL_NAMELENGTH];
  GetKeyboardLayoutName(kbdName);

  char kbdKeyPath[51 + KL_NAMELENGTH];
  snprintf(kbdKeyPath, 51 + KL_NAMELENGTH,
           "SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", kbdName);

  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR) kbdKeyPath, 0,
                   KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS) return -1;

  if (RegQueryValueEx(hKey, "Layout File", NULL, &varType, (LPBYTE) layoutFile,
                      &bufferSize) != ERROR_SUCCESS) return -1;

  RegCloseKey(hKey);

  return 1;
}


I hope anyone can help me. :)


Regards cookieexploit
You've not initialised hKey.
@kbw
You don't need that.



I've figure out that the problem is at another, and not the "RegQueryValueEx" function.
Note that the whole code works fine on an 32-bit windows. I'm at programming with a 64-bit os, so I hope you can help me and tell me what I'm doing wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// headerfile

#define TYPEDEF_VK_TO_WCHARS(n) typedef struct _VK_TO_WCHARS##n { \
  BYTE  VirtualKey; \
  BYTE  Attributes; \
  WCHAR wch[n]; \
} VK_TO_WCHARS##n, *KBD_LONG_POINTER PVK_TO_WCHARS##n;
 
TYPEDEF_VK_TO_WCHARS(1)
TYPEDEF_VK_TO_WCHARS(2)
TYPEDEF_VK_TO_WCHARS(3)
TYPEDEF_VK_TO_WCHARS(4)
TYPEDEF_VK_TO_WCHARS(5)
TYPEDEF_VK_TO_WCHARS(6)
TYPEDEF_VK_TO_WCHARS(7)
TYPEDEF_VK_TO_WCHARS(8)
TYPEDEF_VK_TO_WCHARS(9)
TYPEDEF_VK_TO_WCHARS(10)
 
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;
 
#define INIT_PVK_TO_WCHARS(i, n) \
if((pKbd->pVkToWcharTable[i].cbSize - 2) / 2 == n) \
  pVkToWchars##n = (PVK_TO_WCHARS##n)pKbd->pVkToWcharTable[i].pVkToWchars; \
 
  
// sourcefile
HINSTANCE loadKeyboardLayout() {
  PKBDTABLES pKbd;
  HINSTANCE kbdLibrary;
  KbdLayerDescriptor pKbdLayerDescriptor = NULL;
 
  char layoutFile[MAX_PATH];
 
  if (getKeyboardLayoutFile(layoutFile, sizeof(layoutFile)) == -1) return NULL;
 
  char systemDirectory[MAX_PATH];
  GetSystemDirectory(systemDirectory, MAX_PATH);
 
  char kbdLayoutFilePath[MAX_PATH];
  snprintf(kbdLayoutFilePath, MAX_PATH, "%s\\%s", systemDirectory, layoutFile);
 
  kbdLibrary = LoadLibrary(kbdLayoutFilePath);
 
  pKbdLayerDescriptor
      = (KbdLayerDescriptor) GetProcAddress(kbdLibrary, "KbdLayerDescriptor");
 
  if (pKbdLayerDescriptor != NULL) pKbd = pKbdLayerDescriptor();
  else return NULL;
 
  int i = 0;
  do {
    INIT_PVK_TO_WCHARS(i, 1) // crash at this line on an 64-Bit windows
    INIT_PVK_TO_WCHARS(i, 2)
    INIT_PVK_TO_WCHARS(i, 3)
    INIT_PVK_TO_WCHARS(i, 4)
    INIT_PVK_TO_WCHARS(i, 5)
    INIT_PVK_TO_WCHARS(i, 6)
    INIT_PVK_TO_WCHARS(i, 7)
    INIT_PVK_TO_WCHARS(i, 8)
    INIT_PVK_TO_WCHARS(i, 9)
    INIT_PVK_TO_WCHARS(i, 10)
    i++;
  } while (pKbd->pVkToWcharTable[i].cbSize != 0);
  return kbdLibrary;
}
What is this ????????

1
2
3
#define INIT_PVK_TO_WCHARS(i, n) \
if((pKbd->pVkToWcharTable[i].cbSize - 2) / 2 == n) \
  pVkToWchars##n = (PVK_TO_WCHARS##n)pKbd->pVkToWcharTable[i].pVkToWchars; 



Where the constants came from here ??


Also, do you use a debugger to see what the problem is exactly ??
@ OP: Based on your response to kbw I can confidently say that this task is beyond you, this doesn't mean give up. I'm always glad to help people push their boundries. You DO have to initialize "hKey" but lucky for you that is done as the last parameter to "RegOpenKeyEx(...)", this is why you HAVE to pass it as a pointer.

This here:
1
2
3
4
5
#define TYPEDEF_VK_TO_WCHARS(n) typedef struct _VK_TO_WCHARS##n { \
  BYTE  VirtualKey; \
  BYTE  Attributes; \
  WCHAR wch[n]; \
} VK_TO_WCHARS##n, *KBD_LONG_POINTER PVK_TO_WCHARS##n; 
Is NOT how you declare a struct!!!! Where did you even get this?

You program crashes on Line 57 for 64-bit because your "INIT_PVK_TO_WCHARS" psuedo-structure doesn't have any bounds checking on its memory allocation. Always remember that C++ is powerful because it is a high-level language that allows DMA.
To start with, I don't think you have pasted the correct code:
1
2
3
4
5
6
#define TYPEDEF_VK_TO_WCHARS(n) typedef struct _VK_TO_WCHARS##n { \
  BYTE  VirtualKey; \
  BYTE  Attributes; \
  WCHAR wch[n]; \
} VK_TO_WCHARS##n, *KBD_LONG_POINTER PVK_TO_WCHARS##n; //syntax error on this line
 


And a syntax error here as well:
1
2
3
4
5
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; //syntax error on this line 
@ guestgulkan: You mean because of the missing comma?
certainly the comma (and I was also wondering if the third item was also supposed to be a pointer)

EDIT:
It looks supiciously as if it was taken/modified from this file here:
http://doxygen.reactos.org/d7/df4/kbd_8h_source.html
Last edited on
@OP: Do you know what you are trying to do? At first glance it looks like you're trying to remap the keyboard but this seems like an obnoxious way to go about doing it. Is this hosted somewhere? Could you post a link to where you got this?
Topic archived. No new replies allowed.