Wystarczyło pogoogle'ować
type
TByteDynArray = array of Byte;
procedure UpdateResString(AFileName, ANewString: string; AStringIdent: Integer);
procedure WriteToArray(AArray: TByteDynArray; AData: Word; var APos: Integer);
begin
AArray[APos] := Lo(AData);
AArray[APos + 1] := Hi(AData);
Inc(APos, 2);
end;
function ReadFromArray(AArray: TByteDynArray; APos: Integer): Word;
begin
Result := AArray[APos] + AArray[APos + 1] * 16;
end;
var
hModule, hResInfo, hUpdate: THandle;
ResData, TempData: TByteDynArray;
wsNewString: WideString;
iSection, iIndexInSection: Integer;
i, iLen, iSkip, iPos: Integer;
begin
hModule := LoadLibrary(PChar(AFileName));
if hModule = 0 then
raise Exception.CreateFmt('File %s failed to load.', [AFileName]);
iSection := AStringIdent div 16 + 1;
iIndexInSection := AStringIdent mod 16;
hResInfo := FindResource(hModule, MakeIntResource(iSection), RT_STRING);
if hResInfo <> 0 then
begin
iLen := SizeOfResource(hModule, hResInfo);
SetLength(ResData, iLen);
CopyMemory(ResData, LockResource(LoadResource(hModule, hResInfo)), iLen);
end;
FreeLibrary(hModule);
wsNewString := WideString(ANewString);
iLen := Length(wsNewString);
iPos := 0;
for i := 0 to iIndexInSection do
begin
if iPos > High(ResData) then
SetLength(ResData, iPos + 2);
if i <> iIndexInSection then
begin
iSkip := (ReadFromArray(ResData, iPos) + 1) * 2;
Inc(iPos, iSkip);
end;
end;
iSkip := (ReadFromArray(ResData, iPos) + 1) * 2;
TempData := Copy(ResData, iPos + iSkip, Length(ResData) - iSkip);
SetLength(ResData, iPos);
SetLength(ResData, iPos + (iLen + 1) * 2 + Length(TempData));
WriteToArray(ResData, iLen, iPos);
for i := 1 to iLen do
WriteToArray(ResData, Ord(wsNewString[i]), iPos);
for i := 0 to High(TempData) do
ResData[iPos + i] := TempData[i];
hUpdate := BeginUpdateResource(PChar(AFileName), False);
if hUpdate = 0 then
raise Exception.CreateFmt(
'Cannot write file %s. Please check whether it is open or set read-only.',
[AFileName]);
UpdateResource(hUpdate, RT_STRING, MakeIntResource(iSection), LANG_NEUTRAL,
ResData, Length(ResData));
EndUpdateResource(hUpdate, False);
end;
@Edit: BTW program zmieniający stringa w Windows Vista/7 musi być uruchomiony jako administrator (przynajmniej ja to zauważyłem)