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
MrViggy at 2007-11-9 13:32:31 >
# 2 Re: Custom Resource getting extra data?
Thanks alot man, that seems to have fixed the problem!
Phob0s at 2007-11-9 13:33:41 >
# 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)?
VladimirF at 2007-11-9 13:35:45 >
# 5 Re: Custom Resource getting extra data?
no thats what throws me off, the semi colon is there
Phob0s at 2007-11-9 13:36:43 >
# 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.
VladimirF at 2007-11-9 13:37:42 >
# 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 >