修改exe资源中的字符串 [英] Modifying a string in resource of an exe

查看:76
本文介绍了修改exe资源中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改以下代码中的buffer,然后保存对可执行文件资源的更改?我正在寻找类似 SetString(handle,index,buffer,size) 的东西.

How do I modify buffer in following code then save the changes in resource of the executable? I'm looking for something like SetString(handle,index,buffer,size).

var
  hExe : Cardinal;
  buffer : array [0..4097] of ansichar;
begin
  hExe:=LoadLibraryEx(PAnsiChar(Edit2.Text),0,LOAD_LIBRARY_AS_DATAFILE);
  LoadString(hExe,65300,buffer,SizeOf(buffer));
  ShowMessage(buffer);
  //need to modify buffer here then I'll unload the resources..
end;

更新:这是我对 UpdateResource 的尝试

Update: Here's my attempt on UpdateResource

var
  hEXE: DWORD;
  pData: PAnsiChar;
begin
  pData := PAnsiChar(Edit1.Text);
  hEXE := BeginUpdateResource(pchar(edit2.text), FALSE);
  if hEXE <> 0 then
  begin
    UpdateResource(hEXE, RT_string, MAKEINTRESOURCE(4082), LANG_NEUTRAL,
      @pData, Length(pData)); //if i change 4082 to 65300 it creates another key like 4082
    EndUpdateResource(hEXE, FALSE);
  end;

这段代码搞乱了整个 4082 内容.问题是 RT_STRING 中名为 4082 的项目是一组字符串.当我在资源编辑器中打开exe时,点击字符串表然后4082结果是:

This code messes up the whole 4082 content. The problem is item named 4082 in RT_STRING is group of strings. When I open the exe in a resource editor, click string table then 4082 the result is:

STRINGTABLE
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
{
65296,  "Unicode"
65297,  "Big Endian Unicode"
65298,  "UTF-8"
65299,  "UTF-7"
65300,  "ABCDE12345"
}

所以我要么需要解析字符串组,要么需要一个 API 来设置组中索引为 65300 的修改字符串.有什么想法吗?

So I either need to parse the string group or I need an API to set modify string with the index 65300 in the group. Any ideas?

推荐答案

我找到了 答案 使用 Google.(中文翻译) 谢谢大家!

I've found the answer using Google. (English translation from Chinese) Thanks anyways everyone!

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 <> 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 > High(ResData) then
      SetLength(ResData, iPos + 2);
    if i <> 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;

这篇关于修改exe资源中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆