Custom Resource getting extra data?
Hi everyone,
I have a custom resource in a program that I need to recieve during runtime. During runtime, it retrieves the text, but there always seems to be one or two extra characters on the end.
This is the function:
char * get_code() {
HRSRC res_handle = FindResource(NULL, "MAIN_CODE_PHP", "PHP_DATA");
DWORD size = SizeofResource(NULL, res_handle);
char * code =(char*) malloc(size);
code = (char*) LockResource(LoadResource(NULL, res_handle));
printf(code);
return code;
}
The resource contains the following text
echo "SOME CODE! BLAH BLAH";
But when i use printf it prints
echo "SOME CODE! BLAH BLAH";(
I am confused? Why is the program grabbing the extra character?
[784 byte] By [
Phob0s] at [2007-11-20 11:20:05]

# 1 Re: Custom Resource getting extra data?
My guess would be that the resource doesn't have a NULL character at the end, so printf is printing everything until it finds a NULL character.
BTW, why do you malloc the buffer, then never use it? Perhaps you need something like:
char * get_code() {
HRSRC res_handle = FindResource(NULL, "MAIN_CODE_PHP", "PHP_DATA");
DWORD size = SizeofResource(NULL, res_handle);
char * code =(char*) malloc(size+1);
memset(code, 0, size+1);
const char *res= (const char*) LockResource(LoadResource(NULL, res_handle));
memcpy(code, res, size);
printf(code);
return code;
}
Viggy
# 3 Re: Custom Resource getting extra data?
Actually I take that back, when Try to compile the file i get
"sytax error missing ';' before 'const'"
and
" 'res' undeclared identifier"
When I comment out the memset line it compiles, but it again grabs the extra characters.
Phob0s at 2007-11-9 13:34:41 >

# 4 Re: Custom Resource getting extra data?
Actually I take that back, when Try to compile the file i get
"sytax error missing ';' before 'const'"
and
" 'res' undeclared identifier"
When I comment out the memset line it compiles, but it again grabs the extra characters.Did you forget a ';' at the end of that line (with memset)?
# 6 Re: Custom Resource getting extra data?
no thats what throws me off, the semi colon is thereWell, could you post your code then?
'const' is a keyword; if your compiler can't parse it right - most likely the previous line is screwed up.
# 7 Re: Custom Resource getting extra data?
fixed it, the problem was that since memset returns a value, the compiler was getting angry that I was not assigning that value to a variable.
It was easily fixed by this line:
char * dummy = memset(code, NULL, size+1);
Phob0s at 2007-11-9 13:38:46 >
