Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a stream to a RCDATA resource

In delphi, how do you write a MemoryStream to a data resource?

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;

This code gives me an access violation and an intense feeling of inadequancy because I don't even know where to start fixing it. Does anyone?

like image 914
N. McA. Avatar asked Jun 07 '12 20:06

N. McA.


1 Answers

In the lpData parameter of UpdateResource(), you need to pass the value of the TMemoryStream.Memory property instead of the TMemoryStream object pointer, eg:

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; 
like image 106
Remy Lebeau Avatar answered Sep 23 '22 14:09

Remy Lebeau