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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrDelphiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.