clyde99
asked on
extract binary file from resource.
Hi,
I am using delphi 3.02
If have created a resource file that contains a binary file (in this case a dbf file) when I try to extract the file, it appears to be truncated after the first byte that contains hex 00 (ie null). Can you only extract text files from resource files? If not how do you extract a binary file from a resource.
Below is my code for extracting the file from the resource: (note this works fine for a plain ascii text file)
var
stream : tmemeroystream;
stream := tMemoryStream.create;
getres('TEST.DBF',stream);
stream.savetofile('test.db f');
// test.dbf in truncated after the first null byte (ie Hex 00)
procedure GetRes(ResID : String; var Mems :Tmemorystream);
var
FndRes : HRSRC;
LdRes : THandle;
Buffer : PChar;
begin
//Locates the resource and returns a handle to it.
// If the function fails it returns 0.
FndRes := FindResource(HInstance,
MakeIntResource(ResID),
RT_RCDATA);
if FndRes <> 0 then
begin
//Loads the resource into global memory and returns a handle to //the global memory block containing the data associated with the //resource. If this function fails it also returns 0.
LdRes := LoadResource(Hinstance,
FndRes);
if LdRes <> 0 then
begin
//If the loaded resource is locked, the return value is a
// pointer to the first byte of the resource otherwise, it is
// NULL.
Buffer := LockResource(LdRes);
// To make this a valid PChar, you should add a null at the
// end of the buffer, but this demo works without error as far
// as I know.
try
mems.WriteBuffer(Pointer(B uffer)^, Length(Buffer));
//Important, must set the stream position to the first byte //of data. Will not work without this.
mems.Position := 0;
finally
FreeResource(LdRes);
end;
end;
end;
end;
I am using delphi 3.02
If have created a resource file that contains a binary file (in this case a dbf file) when I try to extract the file, it appears to be truncated after the first byte that contains hex 00 (ie null). Can you only extract text files from resource files? If not how do you extract a binary file from a resource.
Below is my code for extracting the file from the resource: (note this works fine for a plain ascii text file)
var
stream : tmemeroystream;
stream := tMemoryStream.create;
getres('TEST.DBF',stream);
stream.savetofile('test.db
// test.dbf in truncated after the first null byte (ie Hex 00)
procedure GetRes(ResID : String; var Mems :Tmemorystream);
var
FndRes : HRSRC;
LdRes : THandle;
Buffer : PChar;
begin
//Locates the resource and returns a handle to it.
// If the function fails it returns 0.
FndRes := FindResource(HInstance,
MakeIntResource(ResID),
RT_RCDATA);
if FndRes <> 0 then
begin
//Loads the resource into global memory and returns a handle to //the global memory block containing the data associated with the //resource. If this function fails it also returns 0.
LdRes := LoadResource(Hinstance,
FndRes);
if LdRes <> 0 then
begin
//If the loaded resource is locked, the return value is a
// pointer to the first byte of the resource otherwise, it is
// NULL.
Buffer := LockResource(LdRes);
// To make this a valid PChar, you should add a null at the
// end of the buffer, but this demo works without error as far
// as I know.
try
mems.WriteBuffer(Pointer(B
//Important, must set the stream position to the first byte //of data. Will not work without this.
mems.Position := 0;
finally
FreeResource(LdRes);
end;
end;
end;
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<g> what can I say? I'm just an ugly American...<g>
ASKER
Well, I'll be buggerd! Works for me too :-)
Glad to hear it... <g>
in french you don't write viola but voila !!!
(il) viola means (he) raped.
I think you do not want to get sued for sexual aggression ? :-))
regards
marc