编程实现自动替换另一程序中的资源exe(50分)

  • 主题发起人 主题发起人 wynney
  • 开始时间 开始时间
W

wynney

Unregistered / Unconfirmed
GUEST, unregistred user!
某个程序X有A.exe资源<br>我现有B.exe<br>我想把X中的资源替换成B.exe<br><br>虽然这个操作可以使用Reshacker之类的资源编辑软件手动来完成<br>我想讨教下大家,这个可以使用编程来自动实现嘛?
 
{*********************************<br>&quot;天龙八部&quot;<br>LoadLibraryEx<br>FindResource<br>LoadResource<br>LockResource<br>BeginUpdateResource<br>UpdateResource<br>EndUpdateResource<br>FreeResource<br>*********************************}<br>procedure TForm1.UpdateClick(Sender: TObject);<br>var <br>lpResLock :Pointer; &nbsp; &nbsp;// pointer to resource data<br>hExe,hRes,hResLoad,hUpdateRes:THandle; &nbsp; &nbsp; &nbsp; &nbsp;// handle to existing .EXE file<br>Getresult:Boolean;<br>begin<br>if &nbsp;(Source.Text = '') or (Target.Text = '') then Exit<br> else begin<br>// LoadLibrary把EXE文件当成一个dll加载 <br>hExe := LoadLibraryEx(Pchar(Target.Text),0,LOAD_LIBRARY_AS_DATAFILE);<br>if (hExe =0 ) then Showmessage('加载目标失败.');<br>// 查找目标中的资源文件<br>hRes := FindResource(hExe, 'SETUP','EXEFILE');<br>if (hRes = 0 ) then Showmessage('查找资源失败.');<br>// 载入目标中的资源文件<br>hResLoad := LoadResource(hExe, hRes);<br>if (hResLoad =0 ) then Showmessage('加载资源失败.');<br>// 锁定目标中的资源文件<br>lpResLock := LockResource(hRes);<br>if (lpResLock = Nil ) then Showmessage('锁定资源失败.');<br>// Open the file to which you want to add the dialog box resource.<br>hUpdateRes := BeginUpdateResource(Pchar(Source.Text), True);<br>if (hUpdateRes =0 ) then Showmessage('打开目标失败.');<br>// Add the dialog box resource to the update list.<br>Getresult := UpdateResource(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hUpdateRes, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp;用BeginUpdateResource获得的Handle<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'EXEFILE', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 资源类型名称(例如 RT_ICON, RT_ANIICON, 等)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'SETUP', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 需要修改的资源的名称<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2052, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 资源的语言类型, 可以使用MAKELANGID构造<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lpResLock, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 存放资源的二进制数据的地址<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SizeofResource(hExe, hRes)); // 用字节描述的数据的长度<br>if (Getresult = False) then Showmessage('无法更新资源.');<br>// Write changes to FOOT.EXE and then close it.<br>if ( Not EndUpdateResource(hUpdateRes, False)) then Showmessage('无法更改资源.');<br>// Clean up.<br>if ( Not FreeLibrary(hExe)) then Showmessage('无法释放资源.');<br> &nbsp;end;<br> end;<br><br>我是要把Project1.exe替换成Test.exe中的exefile<br>Target.Text:=D:/Code/更新资源/Test.exe<br>Source.Text :=D:/Code/更新资源/Project1.exe<br><br>为什么用上面的代码不成功啊
 
没兄弟会嘛?
 
这个是BC++的代码<br><br>那位大哥帮忙转化成Delphi的啊<br><br>bool UpdateResFromFile(char* filename, UINT resType, UINT resID, DWORD buffersize, char* destModule)<br>{<br> HANDLE hFile;<br> hFile = CreateFile(filename, <br> GENERIC_READ, // open for reading <br> FILE_SHARE_READ, // share for reading <br> NULL, // no security <br> OPEN_EXISTING, // existing file only <br> FILE_ATTRIBUTE_NORMAL, // normal file <br> NULL); // no attr. template <br><br> if (hFile == INVALID_HANDLE_VALUE) <br> { <br> cout&lt;&lt;&quot;Could not open file.&quot;; <br> }<br><br> char* Data = new char[buffersize];<br> DWORD dwBytes;<br> BOOL result = ReadFile(hFile, (void*)Data, buffersize, &dwBytes, NULL);<br> CloseHandle(hFile);<br><br> HANDLE hUpdateRes;<br> hUpdateRes = BeginUpdateResource(destModule, FALSE); <br> if (hUpdateRes == NULL) <br> { <br> cout&lt;&lt;&quot;Could not open file for writing.&quot;; <br> } <br><br> result = UpdateResource(hUpdateRes, // update resource handle <br> resType, <br> MAKEINTRESOURCE(resID), <br> MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), // neutral language<br> (void*)Data, // ptr to resource info <br> dwBytes); // size of resource info. <br> if (result == FALSE) <br> { <br> cout&lt;&lt;&quot;Could not add resource.&quot;; <br> } <br><br> // Write changes to FOOT.EXE and then close it. <br> if (!EndUpdateResource(hUpdateRes, FALSE)) <br> { <br> cout&lt;&lt;&quot;Could not write changes to file.&quot;; <br> } <br><br> delete Data;<br> return result;<br>}<br><br>// 用法, 如下是将外部的earth.bmp以200的ID号、RT_BITMAP的类型写入“foo.exe”<br>// 开的缓冲的大小为900000<br>ERRORCHECK(UpdateResFromFile(&quot;earth.bmp&quot;, RT_BITMAP, 200, 900000, &quot;foo.exe&quot;));
 
上一楼的代码应该就是我要的<br>那位兄弟帮翻译下<br>分数不够再加呀
 
问题已经解决:)<br>结帖
 
后退
顶部