Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I've found <a href="http://harryfin.spaces.live.com/blog/cns!31E57C5D97ECDF5!443.entry" rel="noreferrer">the answer</a> using Google. (<a href="http://translate.google.com/translate?js=y&amp;u=http%3A%2F%2Fharryfin.spaces.live.com%2Fblog%2Fcns!31E57C5D97ECDF5!443.entry&amp;sl=zh-CN&amp;tl=en" rel="noreferrer">English translation from Chinese</a>) Thanks anyways everyone!</p> <pre><code>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]); // Calculate the resource string area and the string index in that area iSection := AStringIdent div 16 + 1; iIndexInSection := AStringIdent mod 16; // If the resource already exists, then read it out of the original data hResInfo := FindResource(hModule, MakeIntResource(iSection), RT_STRING); if hResInfo &lt;&gt; 0 then begin iLen := SizeOfResource(hModule, hResInfo); SetLength(ResData, iLen); CopyMemory(ResData, LockResource(LoadResource(hModule, hResInfo)), iLen); end; // Should first close the file, and then update FreeLibrary(hModule); // Calculate the new data is written to location wsNewString := WideString(ANewString); iLen := Length(wsNewString); iPos := 0; for i := 0 to iIndexInSection do begin if iPos &gt; High(ResData) then SetLength(ResData, iPos + 2); if i &lt;&gt; iIndexInSection then begin iSkip := (ReadFromArray(ResData, iPos) + 1) * 2; Inc(iPos, iSkip); end; end; // Delete the original data and the data behind the temporary // storage of data to be added 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)); // Write new data WriteToArray(ResData, iLen, iPos); for i := 1 to iLen do WriteToArray(ResData, Ord(wsNewString[i]), iPos); // Write back to the original data for i := 0 to High(TempData) do ResData[iPos + i] := TempData[i]; // Write the data back to file 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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload