无法从Delphi资源文件中提取文件

时间:2010-03-03 19:30:20

标签: delphi delphi-2010

我一直在使用FindResource,LoadResource和LockResource来访问res文件中的资源。我有一个wave文件,我想通过我的Delphi应用程序提取和播放。

我没有提取就完成了,但这不是我想做的事情。我想先提取波形文件。有人能指出我正确的解决方案吗?

2 个答案:

答案 0 :(得分:6)

如果您已经拨打LoadResourceLockResource,那么您已经在那里了一半。 LockResource为您提供指向资源数据的第一个字节的指针。调用SizeofResource以找出有多少字节,并且您可以使用该内存块执行任何操作,例如将其复制到另一个内存块或将其写入文件。

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);

复制到另一个内存块:

var
  buffer: TBytes;

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

写入文件:

var
  fs: TStream;

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

这为我们提供了几种播放波形数据的方法:

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);

答案 1 :(得分:3)

您可以使用TResourceStream类加载WAVE资源,并使用SaveToFile方法将其保存到磁盘。