Failed to extract files from Delphi resource file
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 w开发者_如何学JAVAant to do. I want to first extract the wave file. Can anyone point me to the right solution?
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);
You can use TResourceStream class to load the WAVE resource, and save it to disk using SaveToFile method.
精彩评论