分别用VB、Delphi、VC++、ASM四种语言写的一个用UpdateResource修改EXE文件图标的多源码示例

前端之家收集整理的这篇文章主要介绍了分别用VB、Delphi、VC++、ASM四种语言写的一个用UpdateResource修改EXE文件图标的多源码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
===============================================
Delphi Code:

===============================================

//请自行添加到 Type 处
PICONDIRENTRY = ^ICONDIRENTRY;
ICONDIRENTRY = packed record
    bWidth: Byte;
    bHeight: Byte;
    bColorCount: Byte;
    bReserved: Byte;
    wPlanes: Word;
    wBitCount: Word;
    dwBytesInRes: DWORD;
    dwImageOffset: DWORD;
end;

PICONDIR = ^ICONDIR;
ICONDIR = packed record
    idReserved: Word;
    idType: Word;
    idCount: Word;
    idEntries: ICONDIRENTRY;
end;

PGRPICONDIRENTRY = ^GRPICONDIRENTRY;
GRPICONDIRENTRY = packed record
    bWidth: Byte;
    bHeight: Byte;
    bColorCount: Byte;
    bReserved: Byte;
    wPlanes: Word;
    wBitCount: Word;
    dwBytesInRes: DWORD;
    nID: Word;
end;
PGRPICONDIR = ^GRPICONDIR;
GRPICONDIR = packed record
    idReserved: Word;
    idType: Word;
    idCount: Word;
    idEntries: GRPICONDIRENTRY; 
end;
//////////////////////////////////////////////
//函数说明:修改EXE图标
//
//参    数:IconFile 图标文件 
//              ExeFile 被修改的EXE文件
//
//返回值: 成功为True,否则False
/////////////////////////////////////////////
function ChangeExeIcon(IcoFile,ExeFile: string): Boolean;
var
stID: ICONDIR;
stGID: GRPICONDIR;

pGrpIcon: PBYTE;
pIcon: PBYTE;
hUpdate: DWORD;
nSize,nGSize: DWORD; 
hFile: THandle;
dwReserved: DWORD; 
ret: Boolean;
begin
Result := False;

hFile := CreateFile(PChar(IcoFile),GENERIC_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if hFile = INVALID_HANDLE_VALUE then
    Exit;
try                      
    ReadFile(hFile,stID,Sizeof(ICONDIR),dwReserved,nil);
    nSize := stID.idEntries.dwBytesInRes;
    GetMem(pIcon,nSize);
    SetFilePointer(hFile,stID.idEntries.dwImageOffset,FILE_BEGIN);
    ReadFile(hFile,pIcon^,nSize,nil);
    stGID.idType := 1;
    stGID.idCount := stID.idCount;
    stGID.idReserved := 0;
    CopyMemory(@stGID.idEntries.bWidth,@stID.idEntries.bWidth,12);
    stGID.idEntries.nID := 0;
    nGSize := Sizeof(GRPICONDIR);
    GetMem(pGrpIcon,nGSize);
    CopyMemory(pGrpIcon,@stGID,nGSize);
    hUpdate := BeginUpdateResource(PChar(ExeFile),False);
    try
      ret := UpdateResource(hUpdate,RT_GROUP_ICON,MAKEINTRESOURCE(1),pGrpIcon,nGSize);
      ret := UpdateResource(hUpdate,RT_ICON,pIcon,nSize);
    finally
      EndUpdateResource(hUpdate,False);
    end;
finally
    CloseHandle(hFile);
end;
Result := ret;
end;

===============================================
VB Code:
===============================================
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String,ByVal dwDesiredAccess As Long,ByVal dwShareMode As Long,lpSecurityAttributes As Any,ByVal dwCreationDisposition As Long,ByVal dwFlagsAndAttributes As Long,ByVal hTemplateFile As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long,lpBuffer As Any,ByVal nNumberOfBytesToRead As Long,lpNumberOfBytesRead As Long,lpOverlapped As Any) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long,ByVal lDistanceToMove As Long,lpDistanceToMoveHigh As Long,ByVal dwMoveMethod As Long) As Long
Private Declare Function BeginUpdateResource Lib "kernel32" Alias "BeginUpdateResourceA" (ByVal pFileName As String,ByVal bDeleteExistingResources As Long) As Long
Private Declare Function UpdateResource Lib "kernel32" Alias "UpdateResourceA" (ByVal hUpdate As Long,ByVal lpType As Long,ByVal lpName As Long,ByVal wLanguage As Long,lpData As Any,ByVal cbData As Long) As Long
Private Declare Function EndUpdateResource Lib "kernel32" Alias "EndUpdateResourceA" (ByVal hUpdate As Long,ByVal fDiscard As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any,Source As Any,ByVal Length As Long)
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Const INVALID_HANDLE_VALUE = -1
Private Const GENERIC_READ = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_BEGIN = 0
Private Const OPEN_EXISTING = 3
Private Const RT_ICON = 3&
Private Const DIFFERENCE As Long = 11
Private Const RT_GROUP_ICON As Long = (RT_ICON + DIFFERENCE)

Private Type ICONDIRENTRY
    bWidth As Byte
    bHeight As Byte
    bColorCount As Byte
    bReserved As Byte
    wPlanes As Integer
    wBitCount As Integer
    dwBytesInRes As Long
    dwImageOffset As Long
End Type
Private Type ICONDIR
    idReserved As Integer
    idType As Integer
    idCount As Integer
    'idEntries As ICONDIRENTRY
End Type
Private Type GRPICONDIRENTRY
    bWidth As Byte
    bHeight As Byte
    bColorCount As Byte
    bReserved As Byte
    wPlanes As Integer
    wBitCount As Integer
    dwBytesInRes As Long
    nID As Integer
End Type
Private Type GRPICONDIR
    idReserved As Integer
    idType As Integer
    idCount As Integer
    idEntries As GRPICONDIRENTRY
End Type
'//////////////////////////////////////////////
'//函数说明:修改EXE图标
'//
'//参    数:IconFile 图标文件
'//              ExeFile 被修改的EXE文件
'//
'//返回值: 成功为True,否则False
'/////////////////////////////////////////////////////
Private Function ChangeExeIcon(ByVal IconFile As String,ByVal ExeFile As String) As Boolean
    On Error GoTo cw
    
    Dim stID As ICONDIR
    Dim stIDE As ICONDIRENTRY
    Dim stGID As GRPICONDIR
    
    Dim hFile As Long
    Dim pIcon() As Byte,pGrpIcon() As Byte
    Dim nSize As Long,nGSize As Long
    Dim dwReserved As Long
    Dim hUpdate As Long
    Dim ret As Long
    
    hFile = CreateFile(IconFile,ByVal 0&,0)
    If hFile = INVALID_HANDLE_VALUE Then Exit Function
    ret = ReadFile(hFile,Len(stID),ByVal 0&)
    If ret = 0 Then GoTo cw
    
    ret = ReadFile(hFile,stIDE,Len(stIDE),ByVal 0&)
    nSize = stIDE.dwBytesInRes
    ReDim pIcon(nSize - 1)
    SetFilePointer hFile,stIDE.dwImageOffset,FILE_BEGIN
    ret = ReadFile(hFile,pIcon(0),ByVal 0&)
    If ret = 0 Then GoTo cw
    
    With stGID
        .idType = 1
        .idCount = stID.idCount
        .idReserved = 0
        CopyMemory stGID.idEntries,12
        .idEntries.nID = 0
    End With
    
    nGSize = Len(stGID)
    ReDim pGrpIcon(nGSize - 1)
    CopyMemory pGrpIcon(0),stGID,nGSize
    
        
    hUpdate = BeginUpdateResource(ExeFile,False)
    ret = UpdateResource(hUpdate,1,pGrpIcon(0),nGSize)
    ret = UpdateResource(hUpdate,nSize)
    EndUpdateResource hUpdate,False
    If ret = 0 Then GoTo cw
    ChangeExeIcon = True
    
cw:
    CloseHandle hFile
    
End Function
===============================================
VC++ Code:
===============================================
#include <stdio.h>
#include <windows.h>
#include <tchar.h>

struct ICONDIRENTRY
{
    BYTE bWidth;
    BYTE bHeight;
    BYTE bColorCount;
    BYTE bReserved;
    WORD wPlanes;
    WORD wBitCount;
    DWORD dwBytesInRes;
    DWORD dwImageOffset;
};

struct ICONDIR
{
    WORD idReserved;
    WORD idType;
    WORD idCount;
    //ICONDIRENTRY idEntries;
};

struct GRPICONDIRENTRY
{
    BYTE bWidth;
    BYTE bHeight;
    BYTE bColorCount;
    BYTE bReserved;
    WORD wPlanes;
    WORD wBitCount;
    DWORD dwBytesInRes;
    WORD nID;
};
struct GRPICONDIR
{
    WORD idReserved;
    WORD idType;
    WORD idCount;
    GRPICONDIRENTRY idEntries;
};

//////////////////////////////////////////////
//函数说明:修改EXE图标
//
//参    数:IconFile 图标文件 
//              ExeFile 被修改的EXE文件
//
//返回值: 成功为True,否则False
/////////////////////////////////////////////
bool ChangeExeIcon(LPWSTR IconFile,LPWSTR ExeFile)
{
    ICONDIR stID;
    ICONDIRENTRY stIDE;
    GRPICONDIR stGID;
    HANDLE hFile;
    DWORD nSize,nGSize,dwReserved;
    HANDLE hUpdate;
    PBYTE pIcon,pGrpIcon;
    BOOL ret;
    hFile = CreateFile(IconFile,NULL,NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
       return false;
    }
    ZeroMemory(&stID,sizeof(ICONDIR));
    ret = ReadFile(hFile,&stID,sizeof(ICONDIR),&dwReserved,NULL);
    ZeroMemory(&stIDE,sizeof(ICONDIRENTRY));
    ret = ReadFile(hFile,&stIDE,sizeof(ICONDIRENTRY),NULL);
    nSize = stIDE.dwBytesInRes;
    pIcon = (PBYTE)malloc(nSize);
    SetFilePointer(hFile,FILE_BEGIN);
    ret = ReadFile(hFile,(LPVOID)pIcon,NULL);
    if (!ret)
    {
       CloseHandle(hFile);
       return false;
    }
    ZeroMemory(&stGID,sizeof(GRPICONDIR));
    stGID.idCount = stID.idCount;
    stGID.idReserved = 0;
    stGID.idType = 1;
    CopyMemory(&stGID.idEntries,12);
    stGID.idEntries.nID = 0;
    nGSize = sizeof(GRPICONDIR);
    pGrpIcon = (PBYTE)malloc(nGSize);
    CopyMemory(pGrpIcon,&stGID,nGSize);

    hUpdate = BeginUpdateResource(ExeFile,false);
    ret = UpdateResource(hUpdate,(LPVOID)pGrpIcon,nGSize);
    ret = UpdateResource(hUpdate,nSize);
EndUpdateResource(hUpdate,false);
    if (!ret)
    {
       CloseHandle(hFile);
       return false;
    }
    CloseHandle(hFile);
    return true;
}

===============================================
ASM Code:
===============================================
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
ICONDIRENTRY STRUCT
    bWidth BYTE ?
    bHeight BYTE ?
    bColorCount BYTE ?
    bReserved BYTE ?
    wPlanes WORD ?
    wBitCount WORD ?
    dwBytesInRes DWORD ?
    dwImageOffset DWORD ?
ICONDIRENTRY ENDS
ICONDIR STRUCT
    idReserved WORD ?
    idType WORD ?
    idCount WORD ?
    ;idEntries ICONDIRENTRY <>
ICONDIR ENDS
GRPICONDIRENTRY STRUCT
    bWidth BYTE ?
    bHeight BYTE ?
    bColorCount BYTE ?
    bReserved BYTE ?
    wPlanes WORD ?
    wBitCount WORD ?
    dwBytesInRes DWORD ?
    nID   WORD ?
GRPICONDIRENTRY ENDS
GRPICONDIR STRUCT
    idReserved WORD ?
    idType WORD ?
    idCount WORD ?
    idEntries GRPICONDIRENTRY <>
GRPICONDIR ENDS
.data
szIcon   db 'a.ico',0
szFile   db 'a.exe',0
.code
//////////////////////////////////////////////
//函数说明:修改EXE图标
//
//参    数:IconFile 图标文件 
//              ExeFile 被修改的EXE文件
//
//返回值: 成功为True,否则False
/////////////////////////////////////////////
_ChangeExeIcon proc IconFile,ExeFile

     local @stID:   ICONDIR
     local @stIDE:   ICONDIRENTRY
     local @stGID:   GRPICONDIR
     local @hFile:   DWORD
     local @dwReserved: DWORD
     local @nSize:   DWORD
     local @nGSize:   DWORD
     local @pIcon:   DWORD
     local @pGrpIcon: DWORD
     local @hUpdate:   DWORD
      local @ret:   DWORD

     invoke CreateFile,IconFile,NULL
     mov @hFile,eax
     .if eax == INVALID_HANDLE_VALUE
          xor eax,eax
          ret
     .endif

     invoke RtlZeroMemory,addr @stID,sizeof @stID
     invoke ReadFile,@hFile,sizeof @stID,addr @dwReserved,NULL
     invoke RtlZeroMemory,addr @stIDE,sizeof @stIDE
     invoke ReadFile,sizeof @stIDE,NULL

     push @stIDE.dwBytesInRes
     pop @nSize
     invoke GlobalAlloc,GPTR,@nSize 
     mov @pIcon,eax
     invoke SetFilePointer,@stIDE.dwImageOffset,FILE_BEGIN
     invoke ReadFile,@pIcon,@nSize,NULL
     cmp eax,0
     je err
     invoke RtlZeroMemory,addr @stGID,sizeof @stGID
     push @stID.idCount
     pop @stGID.idCount
     mov @stGID.idReserved,0
     mov @stGID.idType,1
     invoke RtlMoveMemory,addr @stGID.idEntries,12
     mov @stGID.idEntries.nID,0
     mov @nGSize,sizeof @stGID
     invoke GlobalAlloc,@nGSize
     mov @pGrpIcon,eax
     invoke RtlMoveMemory,@pGrpIcon,@nGSize

     ;开始修改
     invoke BeginUpdateResource,ExeFile,FALSE
     mov @hUpdate,eax
     invoke UpdateResource,@hUpdate,@nGSize
     invoke UpdateResource,@nSize
     mov @ret,eax
     invoke EndUpdateResource,FALSE
     .if @ret == FALSE
          jmp err
     .endif
     ;成功后到此一日游
     invoke GlobalFree,@pIcon
     invoke CloseHandle,@hFile
     mov eax,1
ret
err:
     ;失败后到此一日游
     invoke GlobalFree,@hFile
     xor eax,eax
     ret
_ChangeExeIcon endp
;==========================程序入口=============================
start:

     invoke _ChangeExeIcon,offset szIcon,offset szFile
     invoke ExitProcess,NULL
end start
原文:http://download.csdn.net/download/domaryj/5775353

猜你在找的VB相关文章