Get the size of LPTSTR or CHAR * containing hexadecimal values
I am trying to get the size of the LPTSTR variable and the CONST CHAR variable using the code below, but I cannot get the size I want.
I should be 20, but instead I get 0 for const char * variable and 4 for LPTSTR variable.
const char *var1 = "\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x02\x00\x00\x00"
"\x5B\xE0\x5B\xE0"
"\x00\x00\x00\x00";
LPTSTR var2 = "\x00\x00\x00\x00"
"\x00\x00\x00\x00"
"\x02\x00\x00\x00"
"\x5B\xE0\x5B\xE0"
"\x00\x00\x00\x00";
printf("%d", sizeof(var1)); // this outputs 0
printf("%d", sizeof(var2)); // this outputs 4
I need to get the size of a value in order to insert it into the windows registry as binary data ( REG_BINARY
) using the following function:
lRes = RegSetValueEx(hMykey, "Scancode Map", 0, REG_BINARY, (LPBYTE) var2, sizeof(var2));
source to share
The type var1
is equal const char*
and the size var2
is LPTSTR
(in your case it is an alias char*
). sizeof var1
is equivalent sizeof (const char*)
, not the size of the character array it points to. Your platform sizeof (char*)
is 4 bytes.
Instead, you could do:
const char var1[] = { 0x00, 0x00, ..., 0x00 };
And then it sizeof var1
will be equivalent sizeof (const char[20])
to what you need.
source to share
Alternative:
#define STR "\x00\x00\x00\x00" \
"\x00\x00\x00\x00" \
"\x02\x00\x00\x00" \
"\x5B\xE0\x5B\xE0" \
"\x00\x00\x00\x00"
const char * var1 = STR;
printf("%d\n", sizeof(STR)-1);
Not sure what is the purpose of these variables, but you can also do
lRes = RegSetValueEx (hMykey, "Scancode Map", 0, REG_BINARY, (LPBYTE) STR, sizeof (STR) -1);
directly.
source to share