function TFormMain.ReplaceMediaRes(SrcFileName:String;MediaFileName:String):Boolean;
var
fs:TFileStream;
buffer:PChar;
hUpdateRes:THandle;
begin
result:=false;
if not FileExists(SrcFileName) then begin
MessageBox(Handle,pchar(SrcFileName+' not exists.'),pchar(Application.Title),MB_ICONERROR);
exit;
end;
if not (FileExists(MediaFileName)) then begin
MessageBox(Handle,pchar(MediaFileName+' not exists.'),pchar(Application.Title),MB_ICONERROR);
exit;
end;
fs:=TFileStream.Create(MediaFileName,fmOpenReadWrite);
try
GetMem(buffer,fs.size);
try
fs.ReadBuffer(buffer^,fs.size);
//begin update resource
hUpdateRes:=BeginUpdateResource(pchar(SrcFileName),false);
if(hUpdateRes=0) then begin
MessageBox(Handle,pchar('Error to begin update resource.'),pchar(Application.Title),MB_ICONERROR);
exit;
end;
//!! the language id can get with GetUserDefaultLangID(), in english system it's 1033.
result:=UpdateResource(hUpdateRes,pchar('PLAYER'),pchar('MEDIA'),1033,buffer,fs.Size);
if not result then begin
MessageBox(Handle,pchar('Error ro update resource'),pchar(Application.Title),MB_ICONERROR);
exit;
end;
if not EndUpdateResource(hUpdateRes,false) then begin
MessageBox(Handle,pchar('Error to end update resource.'),pchar(Application.Title),MB_ICONERROR);
exit;
end;
finally
FreeMem(buffer);
end;
finally
fs.Free;
end;
result:=true;
end;