老问题:关于EXE的资源替换,只用API解决。求例子,完整,适用给分(300分)

  • 主题发起人 主题发起人 晶晶
  • 开始时间 开始时间

晶晶

Unregistered / Unconfirmed
GUEST, unregistred user!
我刚刚遇到一个问题:<br>在为一个程序改资源时,用beginupdateresource,updateresource,endupdateresource<br>几个函数时问题百出。虽然API说明中说的明白,但还是很多问题出来,实在没有辙,上来<br>求救了!千万别跟我说分析PE结构,那个读取资源还可以(几层循环也不简单),导入资<br>源嘛(更难了),我还没有那个算法功力[:D],最好有全部用API函数更换资源的例子,更<br>换的资源内容要包括:ICON,BITMAP,STRING,总之,越详细越好!!!分数先300,成功<br>后加200,一个问题500分,决不食言!
 
我的E-mail:tufeiping@vip.sina.com<br>mailto:tufeiping@vip.sina.com
 
把exe里面的资源通通取出来<br>&nbsp;<br><br>作者:king_koo<br><br>下载例子源代码<br><br> <br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>一、前言 <br>  不知大家用过exescope没有,那是日本鬼子写的一个很有用的东西,它能把exe等pe<br>格式(portable executable)文件的资源(图标、位图、对话框、声音等等)分析出来,并<br>能改写回去。当然vc的ide也有类似功能。大家是不是觉得很神秘?其实只要弄清了pe文件<br>的结构,你也可以写一个类似的工具出来。下面是我近 来对pe文件的分析经验,给大家<br>作参考。同时希望看到有中国人能写出比日本鬼子更牛的分析工具来。 <br>&nbsp;<br>&nbsp;<br>二、重要的数据结构 <br>  PE格式简要说明:(更详细的资料见http://vcangle.8u8.com文件格式专页)<br>PE文件总结构如下表: <br>&nbsp;<br>&nbsp;<br> <br>DOS MZ header &nbsp;;dos头 <br>DOS stub &nbsp; &nbsp; &nbsp; ;dos附加段<br>PE header &nbsp; &nbsp; &nbsp;;NT头<br>Section table &nbsp;;节表<br>Section 1 &nbsp; &nbsp; &nbsp;;第一节<br>Section 2 &nbsp; &nbsp; &nbsp;;<br>Section ... <br>Section n &nbsp; &nbsp; &nbsp;;第n节<br>&nbsp; 其中NT头:<br>&nbsp; typedef struct _IMAGE_NT_HEADERS { <br>&nbsp; DWORD Signature;//PE文件头标志 :"PE/0/0"。<br>&nbsp; IMAGE_FILE_HEADER FileHeader; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//PE文件物理分布的信息 <br>&nbsp; IMAGE_OPTIONAL_HEADER32 OptionalHeader;//PE文件逻辑分布的信息 <br>&nbsp; } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32; <br>&nbsp; 节表数据结构:(可参考winnt.h)<br>&nbsp; typedef struct _IMAGE_SECTION_HEADER { <br>&nbsp; BYTE &nbsp; &nbsp;Name[IMAGE_SIZEOF_SHORT_NAME];//节表名称,如“.text” <br>&nbsp; union { &nbsp;DWORD &nbsp; PhysicalAddress; &nbsp; &nbsp; //物理地址 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp;DWORD &nbsp; VirtualSize; &nbsp; &nbsp; &nbsp; &nbsp; //真实长度 <br>} Misc; <br>DWORD &nbsp; VirtualAddress; &nbsp; &nbsp; &nbsp; &nbsp; //RVA <br>DWORD &nbsp; SizeOfRawData; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//物理长度 <br>DWORD &nbsp; PointerToRawData; &nbsp; &nbsp; &nbsp; //节基于文件的偏移量 <br>DWORD &nbsp; PointerToRelocations; &nbsp; //重定位的偏移 <br>DWORD &nbsp; PointerToLinenumbers; &nbsp; //行号表的偏移 <br>WORD &nbsp; &nbsp;NumberOfRelocations; &nbsp; &nbsp;//重定位项数目 <br>WORD &nbsp; &nbsp;NumberOfLinenumbers; &nbsp; &nbsp;//行号表的数目 <br>DWORD &nbsp; Characteristics; &nbsp; &nbsp; &nbsp; &nbsp;//节属性 <br>&nbsp; } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER; <br><br>&nbsp; 2.2位图文件格式。由文件头,位图信息和数据段组成。<br>&nbsp; typedef struct tagBITMAPFILEHEADER { // bmfh文件头 <br>&nbsp; &nbsp; &nbsp; WORD &nbsp; &nbsp;bfType; //"BM"<br>&nbsp; &nbsp; &nbsp; DWORD &nbsp; bfSize; <br>&nbsp; &nbsp; &nbsp; WORD &nbsp; &nbsp;bfReserved1; <br>&nbsp; &nbsp; &nbsp; WORD &nbsp; &nbsp;bfReserved2; <br>&nbsp; &nbsp; &nbsp; DWORD &nbsp; bfOffBits; <br>&nbsp; } BITMAPFILEHEADER;<br>&nbsp; typedef struct tagBITMAPINFO { // bmi----位图信息 <br>&nbsp; &nbsp; &nbsp; BITMAPINFOHEADER bmiHeader; // &nbsp; ----位图信息头<br>&nbsp; &nbsp; &nbsp; RGBQUAD &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bmiColors[1];// ----调色板* <br>&nbsp; } BITMAPINFO; <br>&nbsp; typedef struct tagBITMAPINFOHEADER{ // bmih 位图信息头<br>&nbsp; &nbsp; &nbsp; DWORD &nbsp;biSize; //该结构大小<br>&nbsp; &nbsp; &nbsp; LONG &nbsp; biWidth; <br>&nbsp; &nbsp; &nbsp; LONG &nbsp; biHeight; <br>&nbsp; &nbsp; &nbsp; WORD &nbsp; biPlanes; <br>&nbsp; &nbsp; &nbsp; WORD &nbsp; biBitCount; <br>&nbsp; &nbsp; &nbsp; DWORD &nbsp;biCompression; <br>&nbsp; &nbsp; &nbsp; DWORD &nbsp;biSizeImage; //位图数据大小 <br>&nbsp; &nbsp; &nbsp; LONG &nbsp; biXPelsPerMeter; <br>&nbsp; &nbsp; &nbsp; LONG &nbsp; biYPelsPerMeter; <br>&nbsp; &nbsp; &nbsp; DWORD &nbsp;biClrUsed; <br>&nbsp; &nbsp; &nbsp; DWORD &nbsp;biClrImportant; <br>&nbsp; } BITMAPINFOHEADER; //该结构后紧接着就是DATA了。 &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; <br>&nbsp;<br>三、取资源主要思路<br>  先要判断是否PE文件。文件头两byte应为0x4d5a即("MZ"),然后到地址0x3c中读出PE<br>文件头(_IMAGE_NT_HEADERS)。判断文件头结构的Signature是否为17744(即"PE/0/0")。<br><br>  读出节表(_IMAGE_SECTION_HEADER),判断有没有资源节。我采用了一个简单的方法<br>:节表名为".rsrc/0/0"即是资源节。(注:据说该法不定可靠)<br>  如有资源节,则读取资源目录(_IMAGE_RESOURCE_DIRECTORY)和资源入口(_IMAGE_RES<br>OURCE_DIRECTORY_ENTRY)。资源在pe文件中处一树型结构中,可有三层。在资源目录根部<br>,如果资源入口的资源名是一个id,那么它代表一种资源类型。否则,它指向下一层资源<br>入口。(祥见例程)<br>  读出具体资源的数据来。具体资源的入口(_IMAGE_RESOURCE_DATA_ENTRY)在就是资源<br>树的叶子。它的OffsetToData成员指明了具体资源的物理位置。不过特别要注意的是,Of<br>fsetToData并不是具体资源在文件中的实际偏移,具体见我的例程。具体资源的长度则由<br>Size成员指定。<br>  导出bmp资源。pe文件里的bmp资源由BITMAPINFO部分和BITMAPDATA部分组成。我们只<br>要为它构造一个文件头(BITMAPFILEHEADER),就可以写成bmp文件了。这里特别要注意的<br>是BITMAPINFO的bmiColors成员是未定长的。你要为它<br>指明长度,以申请内存。<br>好了,我不说了,有什么不明白就看例程吧。 <br>&nbsp;<br>&nbsp;<br>四、例子程序简介<br>  我的例子程序基本实现了读取pe文件的资源信息,并能把位图显示、导出。其它资源<br>的操作还在研究中。由于作者水平有限,在分析有些文件时可能会不成功。欢迎指正!主<br>页: http://vcangle.8u8.com,邮箱:king_koo@163.net. <br>五、参考资料<br>&nbsp;<br>如何提取并保存图标资源<br><br>作者:Future Studio.徐景周<br><br>下载例子源代码<br><br> <br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp; &nbsp; 当你想要使用别的应用程序中的漂亮图标时,该怎么办呢?也许就会用到一些图标提<br>取工具吧!那么,你知道它们是怎么实现的,想不想自己动手也做一个适合自己的图标提<br>取工具呢?下面,就让我用我以前做过的一个工具<轻轻松松抓图标>来告诉你吧!它可<br>以提取各种文件中的图标资源,并可将其保存为图标(ICO)、位图(BMP)两种格式。程序运<br>行后界面如下: <br>&nbsp;<br>&nbsp;<br>&nbsp;<br>图一 <br>&nbsp; &nbsp; 在上面的例程中,我整合出一个图标类CIcons和与之相配的一个位图类CDib,其中包<br>括Icons.h、Icons.cpp、Dib.h和Dib.cpp四个文件。在你的工程中直接加入这四个文件后<br>,调用其类涵数,既可做出你自己的图标工具来。<br>下面让我们来看看如何来具体用它们来实现:<br>1、 提取并显示出图标到左侧列表框中。<br>代码实现如下:<br>//读取各种资源内部图标并显示在左侧列表框中 <br>&nbsp;<br>&nbsp;<br>void CIconSnapDlg::OnOK() <br>{ <br> CFileDialog fileDialog(<br>TRUE,"*.ICO",NULL,NULL,"资源文件(*.ICO,*.BMP,*.EXE,*.DLL,*.ICL)|*.ICO;*.BMP;*.<br>EXE;*.DLL;*.ICL||");<br> if (fileDialog.DoModal() == IDOK) <br> { <br> szOpenFileName=fileDialog.GetPathName(); <br>&nbsp; &nbsp; &nbsp; &nbsp; szOpenFileExtName= fileDialog.GetFileExt ();<br> szOpenFileExtName.MakeLower ();<br><br> m_List.ResetContent (); //选清空左侧图标列表框<br><br> //读取并显示ICON文件<br>&nbsp; &nbsp; &nbsp; &nbsp; if(szOpenFileExtName =="ico") <br> {<br> lpIR=pIcons-&gt;ReadIconFromICOFile (szOpenFileName); <br> HICON hIcon;<br> hIcon=ExtractIcon(AfxGetInstanceHandle(),szOpenFileName,0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(hIcon!=NULL)<br> m_List.AddString (szOpenFileName);<br> CStatic* pStatic = (CStatic*) GetDlgItem(IDC_ICONS);<br> &nbsp; &nbsp; &nbsp; &nbsp;pStatic-&gt;SetIcon (hIcon);<br> }<br> else if(szOpenFileExtName == "bmp") //读取并显示BMP文件<br> {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pIcons-&gt;IconImageFromBMPFile<br>(szOpenFileName,&amp;lpIR-&gt;IconImages[0],TRUE);<br> HICON hIcon;<br> hIcon=pIcons-&gt;MakeIconFromResource (&amp;lpIR-&gt;IconImages [0]);<br> if(hIcon!=NULL) <br> m_List.AddString (szOpenFileName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CStatic* pStatic = (CStatic*) GetDlgItem(IDC_ICONS);<br> &nbsp; &nbsp; &nbsp; &nbsp;pStatic-&gt;SetIcon (hIcon);<br> }<br>&nbsp; &nbsp; &nbsp; &nbsp; else //读取并显示EXE、DLL等资源文件<br> {<br> &nbsp; HINSTANCE &nbsp;hLibrary;<br><br> &nbsp; // Load the DLL/EXE - NOTE: must be a 32bit EXE/DLL for this to work<br> &nbsp; if( (hLibrary = LoadLibraryEx( szOpenFileName, NULL,<br>LOAD_LIBRARY_AS_DATAFILE )) == NULL )<br> &nbsp; {<br> // Failed to load - abort<br> MessageBox( szOpenFileName+ "文件载入错误,必须是WIN32的文件!", "错误",<br>MB_OK );<br> return;<br> &nbsp; }<br> &nbsp; &nbsp; // Store the info<br> &nbsp; EDII.szFileName =szOpenFileName;<br> &nbsp; EDII.hInstance = hLibrary;<br> &nbsp; &nbsp;<br> &nbsp; // Fill in the listbox with the icons available<br> &nbsp; if( ! EnumResourceNames( EDII.hInstance, RT_GROUP_ICON, (ENUMRESNAMEPROC<br>)MyEnumProcedure, (LPARAM)GetSafeHwnd()) )<br> &nbsp; {<br> MessageBox( "列举图标资源名时出错!", "错误", MB_OK );<br> return;<br> &nbsp; }<br> }<br><br> m_List.SetCurSel (0);<br> if( m_List.GetCount() &nbsp;== 0 )<br> {<br> MessageBox( "此文件中没有图标资源!", "错误", MB_OK );<br> //无图标资源,置保存和复制按钮为无效状态<br> m_Copy.EnableWindow (false);<br> m_SaveAs.EnableWindow (false);<br> return;<br> }<br>&nbsp; &nbsp; &nbsp; &nbsp; //有图标资源,置保存和复制按钮为有效状态????<br> m_Copy.EnableWindow (true);<br> m_SaveAs.EnableWindow (true);<br><br> //刷新调用OnPaint来显示图标<br> InvalidateRect(NULL,TRUE);<br> } <br>} &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <br>&nbsp;<br>在OnPaint()涵数中加入下面代码用来具体显示提取出的图标或位图资源。<br>//根据左侧图标列表,利用OnPaint()来更新右侧相应图标 <br>&nbsp;<br>&nbsp;<br> <br>LPTSTR lpIconID;<br>HICON hIcon;<br>if((lpIconID=(LPTSTR)m_List.GetItemData(m_List.GetCurSel()))!=(LPTSTR)LB_ERR<br>)<br>{<br><br> if(szOpenFileExtName=="exe"||szOpenFileExtName=="dll"||szOpenFileExtName=="ic<br>l")<br> {<br>&nbsp; &nbsp; &nbsp; &nbsp; hIcon=pIcons-&gt;GetIconFromInstance(EDII.hInstance,lpIconID);<br> &nbsp; &nbsp; CStatic* pStatic = (CStatic*) GetDlgItem(IDC_ICONS);<br> &nbsp; &nbsp; pStatic-&gt;SetIcon (hIcon);<br> }<br>} &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; <br>&nbsp;<br>2、 如何将提取出的图标资源保存为Ico或Bmp格式。<br>//保存图标资源为ICO或BMP格式文件 <br>&nbsp;<br>&nbsp;<br>void CIconSnapDlg::OnButtonSaveas() <br>{<br> LPTSTR lpIconID;<br><br>&nbsp; CFileDialog fileDialog( FALSE,"*.ICO",NULL,NULL,"图标文件(*.ICO)|*.ICO|位图<br>文件(*.BMP)|*.BMP||");<br> if (fileDialog.DoModal() == IDOK) <br> {<br> szSaveFileName=fileDialog.GetPathName(); <br> &nbsp; &nbsp; &nbsp; &nbsp;szSaveFileExtName= fileDialog.GetFileExt ();<br> szSaveFileExtName.MakeLower ();<br><br> if(szOpenFileExtName=="exe"||szOpenFileExtName=="dll"||szOpenFileExtName=="i<br>cl")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((lpIconID=(LPTSTR)m_List.GetItemData (m_List.GetCurSel()))!=<br>(LPTSTR)LB_ERR)<br> &nbsp; &nbsp; lpIR=pIcons-&gt;ReadIconFromEXEFile (szOpenFileName,lpIconID);<br> &nbsp; &nbsp; &nbsp; &nbsp; if(szSaveFileExtName=="bmp")<br> {<br> &nbsp;if(lpIR!=NULL &amp;&amp; m_List.GetCount ()&gt;0)<br> &nbsp;{<br> BeginWaitCursor();<br> pIcons-&gt;IconImageToBMPFile (szSaveFileName,&amp;lpIR-&gt;IconImages [0]);<br> EndWaitCursor();<br> &nbsp;}<br> &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox( "没有可保存的图标资源!", "错误", MB_OK );<br> }<br> else if(szSaveFileExtName=="ico")<br> {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(lpIR!=NULL &amp;&amp; m_List.GetCount ()&gt;0)<br> &nbsp;{<br> BeginWaitCursor();<br> pIcons-&gt;WriteIconToICOFile (lpIR,szSaveFileName);<br> EndWaitCursor();<br> &nbsp;}<br> &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; MessageBox( "没有可保存的图标资源!", "错误", MB_OK );<br> }<br> }<br>}<br>&nbsp; &nbsp; &nbsp; <br>&nbsp;<br>以上代码的详细实现,请在下载后源码后,仔细参看既可。 <br>&nbsp;<br>
 
看看这个 PE Resource Editor<br>http://www.wilsonc.demon.co.uk/d6peresourceeditor.htm<br>源代码得下载地址:<br>http://www.wilsonc.demon.co.uk/files/d6/peresourceexplorer/peresourceexplorer.zip<br>那网站有很多冬冬。慢慢享受吧。http://www.wilsonc.demon.co.uk/<br>CIONO1<br>
 
还有一点忘了写。<br>在google groups 找到了Colin写的这个帖子。<br>http://groups.google.com/groups?hl=en&amp;lr=&amp;ie=UTF-8&amp;oe=UTF-8&amp;selm=3d0879e9_1%40dnews<br>我刚刚试过。可以改ICON。<br>//--------------------------------------------------------------------------<br>uses ..., unitPEFile, unitResourceGraphics;<br><br>procedure ReplaceIcon (const moduleName, newIconName : string);<br>var<br>&nbsp; module : TPEResourceModule;<br>&nbsp; i, lang : Integer;<br>&nbsp; nm : string;<br>&nbsp; newIcon : TIconGroupResourceDetails;<br>begin<br>&nbsp; module := TPEResourceModule.Create;<br>&nbsp; try<br>&nbsp; &nbsp; module.LoadFromFile(moduleName);<br><br>&nbsp; &nbsp; i := 0;<br>&nbsp; &nbsp; while i &lt; module.ResourceCount do &nbsp;// Find the old icon<br>&nbsp; &nbsp; &nbsp; if module.ResourceDetails .ResourceType = IntToStr (Integer <br>(RT_GROUP_ICON)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; break<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc (i);<br><br>&nbsp; &nbsp; if i &lt; module.ResourceCount then<br>&nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Delete the old icon<br>&nbsp; &nbsp; &nbsp; lang := module.ResourceDetails .ResourceLanguage;<br>&nbsp; &nbsp; &nbsp; nm := module.ResourceDetails .ResourceName;<br>&nbsp; &nbsp; &nbsp; module.DeleteResource(i);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; lang := $809; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // There wasn't an old icon<br>&nbsp; &nbsp; &nbsp; nm := '1'<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the new icon<br>&nbsp; &nbsp; newIcon := TIconGroupResourceDetails.CreateNew(module, lang, nm);<br>&nbsp; &nbsp; newIcon.LoadImage(newIconName); &nbsp; &nbsp; // Load it.<br><br>&nbsp; &nbsp; module.SaveToFile(moduleName); &nbsp; &nbsp; &nbsp;// Save the new .EXE<br>&nbsp; finally<br>&nbsp; &nbsp; module.Free<br>&nbsp; end<br>end;<br>//-------------------------------------<br>CIONO1<br>
 
kcahcn:<br>谢谢!对于其他的资源改变有例子吗?
 
各位老大,对于你们的帮助非常感谢。有没有直接用(上述3个)API做出来的例子???<br>各位的辛苦都会有分!
 
对于以上三个函数就没有人做过应用的吗?帮帮忙。
 
delphi 的例子里有
 
就三个函数,自己慢慢做,能成功的。
 
各位老大!导出资源我做出来了,我现在是想将我的图片导入EXE中,怎么做,怎么全是告诉我怎么<br>导出资源的,没有谁说说导入资源的!??
 
晶晶 :<br>仔细看那个源代码。<br>我按照前面ReplaceIcon的例子写了这个过程。你试试看。(不费时间,1分钟就改好了)。<br>你多费时间看Colin写的源代码。应该能改其它的资源。<br>如果下面的过程有问题告诉我。(我只是改了ReplaceIcon的某些地方,没仔细研究代码)<br>不要忘了考虑BITMAP类型的资源不止一个。。。<br>procedure ReplaceBitmap (const moduleName:string; picture : TPicture);<br>var<br>&nbsp; module : TPEResourceModule;<br>&nbsp; i, lang : Integer;<br>&nbsp; nm : string;<br>&nbsp; newBitmap:TBitmapResourceDetails;<br>&nbsp; ResourceDetails:TResourceDetails;<br>begin<br>&nbsp; module := TPEResourceModule.Create;<br>&nbsp; try<br>&nbsp; &nbsp; module.LoadFromFile(moduleName);<br><br>&nbsp; &nbsp; i := 0;<br>&nbsp; &nbsp; while i &lt; module.ResourceCount do &nbsp;// Find BITMAP<br>&nbsp; &nbsp; &nbsp; if module.ResourceDetails .ResourceType = IntToStr (Integer<br>(RT_BITMAP)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; break<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc (i);<br><br>&nbsp; &nbsp; if i &lt; module.ResourceCount then<br>&nbsp; &nbsp; begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Delete the old BITMAP<br>&nbsp; &nbsp; &nbsp; lang := module.ResourceDetails .ResourceLanguage;<br>&nbsp; &nbsp; &nbsp; nm := module.ResourceDetails .ResourceName;<br>&nbsp; &nbsp; &nbsp; module.DeleteResource(i);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; lang := $809; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // No BITMAP found<br>&nbsp; &nbsp; &nbsp; nm := '1'<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create the new BITMAP<br>&nbsp; &nbsp; newBitmap := TBitmapResourceDetails.CreateNew(module, lang, nm);<br>&nbsp; &nbsp; newBitmap.SetImage(Picture); &nbsp; &nbsp; // Set image.<br>&nbsp; &nbsp; module.SaveToFile(moduleName); &nbsp; &nbsp; &nbsp;// Save the new .EXE<br>&nbsp; finally<br>&nbsp; &nbsp; module.Free<br>&nbsp; end<br>end;<br>//-------------<br>CIONO1
 
{<br>QuickInfo<br>&nbsp; Windows NT: Requires version 3.1 or later.<br>&nbsp; Windows: Unsupported. &nbsp;不支持 98<br>&nbsp; Windows CE: Unsupported.<br>&nbsp; Header: Declared in winbase.h.<br>&nbsp; Import Library: Use kernel32.lib.<br>&nbsp; Unicode: Implemented as Unicode and ANSI versions on Windows NT.<br>}<br><br>//这几个函数要在 NT 3.1 或更高版本上运行.<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; hExe,hUpdateRes:THandle;<br>&nbsp; hRes,hResLoad:HRSRC;<br>&nbsp; lpResLock:pchar;<br>&nbsp; b:Boolean;<br>begin<br>&nbsp; hExe := LoadLibrary('c:/Program Files/borland/delphi5/projects/project2.exe');<br>&nbsp; if hExe=0 then begin<br>&nbsp; &nbsp; showmessage('Can''t load notepad!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; hRes := FindResource(hExe, 'MAINICON',RT_GROUP_ICON);<br>&nbsp; if hRes=0 then begin<br>&nbsp; &nbsp; showmessage('Can''t load Icon!');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; hResLoad := LoadResource(hExe, hRes);<br>&nbsp; if hResLoad=0 then begin<br>&nbsp; &nbsp; showmessage('Can''t load !');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; lpResLock := LockResource(hRes);<br>&nbsp; if lpResLock=nil then begin<br>&nbsp; &nbsp; showmessage('Can''t lock !');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br><br>&nbsp; hUpdateRes := BeginUpdateResource('c:/Program Files/borland/delphi5/projects/project3.exe', FALSE);<br>&nbsp; if hUpdateRes=0 then begin<br>&nbsp; &nbsp; // 98下运行出现的错误:此功能仅在 Win32 模式下有效。<br>&nbsp; &nbsp; showmessage('Could not open file for writing.');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; b:= UpdateResource(hUpdateRes, &nbsp; &nbsp; &nbsp; // update resource handle<br>&nbsp; &nbsp; &nbsp;RT_GROUP_ICON, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // change dialog box resource<br>&nbsp; &nbsp; &nbsp;'MAINICON', &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// dialog box name<br>&nbsp; &nbsp; &nbsp;0{MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL)}, &nbsp;// neutral language<br>&nbsp; &nbsp; &nbsp;lpResLock, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ptr to resource info<br>&nbsp; &nbsp; &nbsp;SizeofResource(hExe, hRes)); // size of resource info.<br><br>&nbsp; if not(b) then begin<br>&nbsp; &nbsp; showmessage('Could not add resource.');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>// Write changes to FOOT.EXE and then close it.<br>&nbsp; if (not(EndUpdateResource(hUpdateRes, FALSE)))<br>&nbsp; then begin<br>&nbsp; &nbsp; showmessage('Could not write changes to file.');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>// Clean up.<br>&nbsp; if (not(FreeLibrary(hExe)))<br>&nbsp; then &nbsp; showmessage('Could not free executable.');<br><br>end;<br>
 
大家用过ResHacker软件没有?<br>它可以将外面的一副BMP图片替换一个EXE中选定的位图资源,我就是要这种例子和代码。<br>以上说的很多都是看资源,或是修改一点的,有没有全面的????<br>有的话帮帮我,我再加分,总共600分怎么样?
 
晶晶:<br>呜呜呜呜。<br>仔细看看那源代码。你可以做跟ResHacker一样的软件。<br>别人给你作全面的就没意思了吧。<br>我有空就帮你研究代码吧。(你自己也要仔细研究)<br><br>CIONO1
 
&nbsp; &nbsp;你说的三个函数在98上是不支持的,<br>所以在98上可能还是要通过PE来获取资源。<br><br>虽然PE结构复杂一点,但掌握了也是一劳永逸!!
 
kcahcn:非常感谢你的热情帮助!<br>说实话,因为是用到他自己写的库文件,所以我就没有仔细看,难道他可以达到相同的功能。<br>我去看看去!呵呵,只要有收获这些分就是你的了。
 
晶晶:<br>别误会我了。我不要分(我讨厌积分这个概念),我只是喜欢与别人讨论技术问题。<br>CIONO1
 
kcahcn:别误会。分这个东西我也不喜欢,不然也不会扔这么多分[:)]<br>但出了分总得给对自己有帮助的人,希望你留下一点联系方式,那么我们好联系,做好这个<br>软件。还可以讨论其他一点问题(向你请教的东西很多),我的qq:30362915
 
后退
顶部