One code works one bugs, yet functionality equivilent?


The following works without error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
size = get_key_value_int("font", "size", 10);
  weight =get_key_value_int("font", "weight", f_weight);
  italic = get_key_value_int("font", "italic", 0);
  underline = get_key_value_int("font", "underline", 0);
  strikeout = get_key_value_int("font", "strikeout", 0);
  get_key_value_string("font", "facename", facename, "Arial");
  t->hFont = CreateFontA(
    size, 0, 0, 0,
    weight,
    italic,
    underline,
    strikeout,
    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
    DEFAULT_QUALITY, FF_DONTCARE|DEFAULT_PITCH,
    facename);


The following code dies on the second line (I know this because each call to get_key_value_int prints debugging output on the console):
1
2
3
4
5
6
7
8
9
  t->hFont = CreateFontA(
    get_key_value_int("font", "size", 10), 0, 0, 0,
    get_key_value_int("font", "weight", f_weight),
    get_key_value_int("font", "italic", 0),
    get_key_value_int("font", "underline", 0),
    get_key_value_int("font", "strikeout", 0),
    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
    DEFAULT_QUALITY, FF_DONTCARE|DEFAULT_PITCH,
    get_key_value_string("font", "facename", facename, "Arial"));


In fact the first block of code is just a workaround for the second. For some reason all the function calls being inside the function's arguments causes an error.

The debugger output says: "Unhandled exception at 0x765bd15b in w32.exe: 0xC0000005: Access violation reading location 0x72756f43." If I click on break it has halted at line 371 of crtexe.c. Call stack reveals that the first address mentioned is in gdi32.dll.

the query_key functions are:

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
void *get_key_value(int type, void *buf, char *subkey, char *value) {
  unsigned long sz;
  char temp [1024];
  HKEY hkey = NULL;
  PHKEY phkey = &hkey;
  long err;
  assert(regkey);
  strcat(strcat(strcpy(temp, regkey), "\\"), subkey);

  err = RegCreateKeyExA(HKEY_CURRENT_USER, temp,
    0, NULL, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_QUERY_VALUE | KEY_SET_VALUE,
		NULL, phkey, NULL);
  if(err != ERROR_SUCCESS)
    ErrorExitCode(err);

  RegQueryValueExA(hkey, value, NULL, NULL, NULL, &sz);
  printf("querying key %s/%s, size %d\n", subkey, value, sz);
  err = RegQueryValueExA(hkey, value, NULL, NULL, buf, &sz);
  if(err != ERROR_SUCCESS)
    ErrorExitCode(err);

  RegCloseKey(hkey);
  return buf;
}

int get_key_value_int(const char *subkey, const char *value, const int def) {
  static int v;
  return ((*((int *)get_key_value(REG_DWORD, &v, subkey, value))) != 0) ? v : def;
}

char *get_key_value_string(const char *subkey, const char *value, char *buf, const char *def) {
  return ((*((char *)get_key_value(REG_DWORD, &buf, subkey, value))) != NULL) ? buf : strcpy(buf, def);
}


Help?
Topic archived. No new replies allowed.