在delphi中,如何将MemoryStream写入数据资源?

procedure StringtoRes (filename:string; Inputstream: TMemoryStream);
var
 hUpdate: THandle;
begin
 hUpdate := BeginUpdateResource(PChar(filename), True);
 UpdateResource(hUpdate, RT_RCDATA, 'ID', LANG_NEUTRAL,InputStream,InputStream.Size);
 EndUpdateResource(hUpdate,False);
end;


这段代码给我带来了访问冲突,并且给人强烈的不适感,因为我什至不知道从哪里开始修复它。有没有人?

最佳答案

lpDataUpdateResource()参数中,您需要传递TMemoryStream.Memory属性的值而不是TMemoryStream对象指针,例如:

procedure StringtoRes (const FileName: string; Inputstream: TMemoryStream);
var
  hUpdate: THandle;
begin
  hUpdate := BeginUpdateResource(PChar(FileName), True);
  try
    UpdateResource(hUpdate, RT_RCDATA, 'ID', LANG_NEUTRAL, InputStream.Memory, InputStream.Size);
  finally
    EndUpdateResource(hUpdate, False);
  end;
end;

08-27 12:28