szExeDesc是可执行文件名,
szIconSour是要添加或替换的图标文件名,
我感觉这函数没什么问题,也没有任合错误提示,可是被它修改的文件个个都死了---不能执行了。
哪里错了吗?function ChangePEIcon( szExeDesc, szIconSour: LPCSTR ):BOOL;
  function MAKELANGID(p, s:WORD):WORD;
  begin
    result := ( p shl 10 ) or s;
  end;
var
  hUpdate     : THandle; // update resource handle
  hFileIcon   : THandle;
  pMemIcon    : PBYTE;
  dwIconSize  : DWORD;
  byte_write  : DWORD;
begin
  result := FALSE;
  
  hFileIcon := CreateFile( szIconSour, GENERIC_READ, 0, nil,OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL, 0 );
  if hFileIcon = INVALID_HANDLE_VALUE then
  begin
    MessageBox( 0, 'Error Opening File for Reading', szIconSour, MB_ICONERROR );
    FreeMem( pMemIcon );
    exit;
  end;  dwIconSize := GetFileSize( hFileIcon, nil );
  pMemIcon := AllocMem( dwIconSize );  byte_write := 0;
  ReadFile( hFileIcon, pMemIcon^, dwIconSize, byte_write, nil );
  FileClose( hFileIcon );  hUpdate := BeginUpdateResource( szExeDesc, TRUE );
  if hUpdate = 0 then
  begin
    MessageBox( 0, 'Error begin update resource', szExeDesc, MB_ICONERROR );
    FreeMem( pMemIcon );
    exit;
  end;  if not UpdateResource( hUpdate,
                         RT_ICON,
                         'xx',
                         MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),
                         pMemIcon,
                         dwIconSize ) then
  begin
    MessageBox( 0, 'Error update resource', szExeDesc, MB_ICONERROR );
    FreeMem( pMemIcon );
    exit;
  end;  if not EndUpdateResource( hUpdate, FALSE ) then
  begin
    MessageBox( 0, 'Error end update resource', szExeDesc, MB_ICONERROR );
    FreeMem( pMemIcon );
    exit;
  end;  FreeMem( pMemIcon );
  result := TRUE;
end;

解决方案 »

  1.   

    请参考 Delphi7\Demos\ResXplor 这个目录中的文件,这是宝兰公司的例子程,可以读出可执行文件、动态链接文件中的所有资源,包括图标,看看人家是如何读出的,也许对于写入会有所启发。
      

  2.   

    看这里,应该可以搞定:http://expert.csdn.net/Expert/topic/2378/2378439.xml?temp=.2824213
      

  3.   

    To: longsky(长空) and crossbow(【带三尺剑立不世之功】)  :
    我并不想分析PE文件格式,找到要修改的图标,写入我的图标...这样的话,如果我的图标和原图标大小不一样,那个PE文件需要修改的偏移地址地方太多了...
    我认为读出很容易,写入太难了。所以我想用API...API UpdateResource就是修改资源的啊,
    那位和我讨论一下UpdateResource这个函数吧!
      

  4.   

    hUpdate := BeginUpdateResource( szExeDesc, TRUE );
    这会使该文件所有资源统统消失;你给函数的图标资源不对,必须给它一个资源而不是文件。给你两个结构:
    type
      MemIconEntry = packed record
        bWidth        : BYTE;      // Width of the image
        bHeight       : BYTE;      // Height of the image (times 2)
        bColorCount   : BYTE;      // 图像的颜色数量( 如果大于16色,此值为0 )
        bReserved     : BYTE;      // Reserved
        wPlanes       : WORD;      // Color Planes
        wBitCount     : WORD;      // Bits per pixel
        dwBytesInRes  : DWORD;     // how many bytes in this resource?
        nID           : WORD;      // the ID instead of the imageoffset.
      end;
      PMemIconEntry =^MemIconEntry;type
      MemIconDir = packed record
        idReserved    : WORD;      // Reserved
        idType        : WORD;      // resource type (1 for icons)
        idCount       : WORD;      // how many images?
        idEntries     : Array [0..ICON_COUNT_MAX-1] of MemIconEntry; 
      end;
      PMemIconDir =^MemIconDir;