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.dbf');
// 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(Buffer)^, 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;
clyde99Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
DrDelphiConnect With a Mentor Commented:
a simple example that I have used and works for me...

var Stream:TresourceStream;
begin
     stream:=Tresourcestream.Create(Hinstance,'EXE','RT_RCDATA');
     stream.SaveToFile('Zoomer.exe');
     Winexec('Zoomer.exe',sw_ShowMaximized);
end;


This takes an EXE (my Zoomer utility, found on my webpage) and pulls it from a resource file into a TResourcestream,. From there save to file and viola! It is done! You'll probably want to add some error checking etc, but the essentials are there.





Good luck..
0
 
mhervaisCommented:
hep Dr Delphi.

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
0
 
DrDelphiCommented:
<g> what can I say? I'm just an ugly American...<g>

0
 
clyde99Author Commented:
Well, I'll be buggerd! Works for me too :-)
0
 
DrDelphiCommented:
Glad to hear it... <g>
0
All Courses

From novice to tech pro — start learning today.