views:

314

answers:

2

I have been using FindResource, LoadResource and LockResource to access resources in a res file. I have a wave file which I want extract and play through my Delphi application.

I have done it without extraction, but it's not the thing I want to do. I want to first extract the wave file. Can anyone point me to the right solution?

+3  A: 

You can use TResourceStream class to load the WAVE resource, and save it to disk using SaveToFile method.

vcldeveloper
+4  A: 

If you're already calling LoadResource and LockResource, then you're already halfway there. LockResource gives you a pointer to the first byte of the resource data. Call SizeofResource to find out how many bytes are there, and you can do whatever you want with that block of memory, such as copy it to another block of memory or write it to a file.

resinfo := FindResource(module, MakeIntResource(resid), type);
hres := LoadResource(module, resinfo);
pres := LockResource(module, hres);
// The following is the only new line in your code. You should
// already have code like the above.
size := SizeofResource(module, resinfo);

Copy to another memory block:

var
  buffer: TBytes;

SetLength(buffer, size);
Move(pres^, buffer[0], size);

Write to a file:

var
  fs: TStream;

fs := TFileStream.Create('foo.wav', fmCreate);
try
  fs.Write(pres^, size);
finally
  fs.Free;
end;

This gives us several ways to play that wave data:

PlaySound(MakeIntResource(resid), module, snd_Resource);
PlaySound(PChar(pres), 0, snd_Memory);
PlaySound(PChar(@buffer[0]), 0, snd_Memory);
PlaySound('foo.wav', 0, snd_FileName);
Rob Kennedy