很久以前收集的一个替换exe图标的源码

DICOM:DCMTK工具包分析之dcmqrscp.exe、dcmqridx.exe、dcmqrti.exe 背景:新的一年开始,专栏会坚持下去,继续与大家相互交流学习。本篇博文再一次分析DCMTK自带的工具包dcmqrscp.exe(类似于一个单机版miniPACS),此次主要关注dcmqrscp.exe的数据库部分,通过使用dcmqridx.exe手动操作dcmqrscp.exe的数据库文件,直观了解数据库的记录内容;然后利用dcmqrti.exe查询dcmqrscp.exe的数据库文件,学习如何操作dcmqrscp.exe数据库。 阅读详情
 很久以前收集的一个替换exe图标的源码,来源已经无法考证。

标准模块1

Option Explicit
Type DIB_HEADER
    Size        As Long
    Width       As Long
    Height      As Long
    Planes      As Integer
    Bitcount    As Integer
    Reserved    As Long
    ImageSize   As Long
End Type

Type ICON_DIR_ENTRY
    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

Type ICON_DIR
    Reserved          As Integer
   Type              As Integer
    Count             As Integer
End Type

Type DIB_BITS
    Bits()            As Byte
End Type

Public Enum Errors
    FILE_CREATE_FAILED = 1000
    FILE_READ_FAILED
    INVALID_PE_SIGNATURE
    INVALID_ICO
    NO_RESOURCE_TREE
    NO_ICON_BRANCH
    CANT_HACK_HEADERS
End Enum
Public Function ReplaceIcons(Source As String, Dest As String, Error As String) As Long

    Dim IcoDir As ICON_DIR
    Dim IcoDirEntry As ICON_DIR_ENTRY
    Dim tBits As DIB_BITS
    Dim Icons() As IconDescriptor
    Dim lngRet As Long
    Dim BytesRead As Long
    Dim hSource As Long
    Dim hDest As Long
    Dim ResTree As Long

    hSource = CreateFile(Source, ByVal &H80000000, 0, ByVal 0&, 3, 0, ByVal 0)
    If hSource >= 0 Then
        If Valid_ICO(hSource) Then
            SetFilePointer hSource, 0, 0, 0
            ReadFile hSource, IcoDir, 6, BytesRead, ByVal 0&
            ReadFile hSource, IcoDirEntry, 16, BytesRead, ByVal 0&
            SetFilePointer hSource, IcoDirEntry.dwImageOffset, 0, 0
            ReDim tBits.Bits(IcoDirEntry.dwBytesInRes) As Byte
            ReadFile hSource, tBits.Bits(0), IcoDirEntry.dwBytesInRes, BytesRead, ByVal 0&
            CloseHandle hSource
            hDest = CreateFile(Dest, ByVal (&H80000000 Or &H40000000), 0, ByVal 0&, 3, 0, ByVal 0)
            If hDest >= 0 Then
                If Valid_PE(hDest) Then
                    ResTree = GetResTreeOffset(hDest)
                    If ResTree > 308 Then                  ' Sanity check
                        lngRet = GetIconOffsets(hDest, ResTree, Icons)
                        SetFilePointer hDest, Icons(1).Offset, 0, 0
                        WriteFile hDest, tBits.Bits(0), UBound(tBits.Bits), BytesRead, ByVal 0&
                        If Not HackDirectories(hDest, ResTree, Icons(1).Offset, IcoDirEntry) Then
                            eRR.Raise CANT_HACK_HEADERS, App.EXEName, "Unable to modify directories in target executable.  File may not contain any icon resources."
                        End If
                    Else
                        eRR.Raise NO_RESOURCE_TREE, App.EXEName, Dest & " does not contain a valid resource tree.  File may be corrupt."
                        CloseHandle hDest
                    End If
                Else
                    eRR.Raise INVALID_PE_SIGNATURE, App.EXEName, Dest & " is not a valid Win32 executable."
                    CloseHandle hDest
                End If
                CloseHandle hDest
            Else
                eRR.Raise FILE_CREATE_FAILED, App.EXEName, "Failed to open " & Dest & ". Make sure file is not in use by another program."
            End If
        Else
            eRR.Raise INVALID_ICO, App.EXEName, Source & " is not a valid icon resource file."
            CloseHandle hSource
        End If
    Else
        eRR.Raise FILE_CREATE_FAILED, App.EXEName, "Failed to open " & Source & ". Make sure file is not in use by another program."
    End If
    ReplaceIcons = 0
    Exit Function
ErrHandler:
    ReplaceIcons = eRR.Number
    Error = eRR.Description
End Function
Public Function Valid_ICO(hFile As Long) As Boolean
    Dim tDir          As ICON_DIR
    Dim BytesRead     As Long
    If (hFile > 0) Then
        ReadFile hFile, tDir, Len(tDir), BytesRead, ByVal 0&
        If (tDir.Reserved = 0) And (tDir.Type = 1) And (tDir.Count > 0) Then
            Valid_ICO = True
        Else
            Valid_ICO = False
        End If
    Else
        Valid_ICO = False
    End If
End Function

标准模块2

Option Explicit
Public Type IMAGE_DOS_HEADER
    Magic    As Integer
    cblp     As Integer
    cp       As Integer
    crlc     As Integer
    cparhdr  As Integer
    minalloc As Integer
    maxalloc As Integer
    ss       As Integer
    sp       As Integer
    csum     As Integer
    ip       As Integer
    cs       As Integer
    lfarlc   As Integer
    ovno     As Integer
    res(3)   As Integer
    oemid    As Integer
    oeminfo  As Integer
    res2(9)  As Integer
    lfanew      As Long
End Type

Public Type IMAGE_FILE_HEADER
    Machine              As Integer
    NumberOfSections     As Integer
    TimeDateStamp        As Long
    PointerToSymbolTable As Long
    NumberOfSymbols      As Long
    SizeOfOtionalHeader  As Integer
    Characteristics      As Integer
End Type

Public Type IMAGE_DATA_DIRECTORY
    DataRVA     As Long
    DataSize    As Long
End Type

Public Type IMAGE_OPTIONAL_HEADER
    Magic             As Integer
    MajorLinkVer      As Byte
    MinorLinkVer      As Byte
    CodeSize          As Long
    InitDataSize      As Long
    unInitDataSize    As Long
    EntryPoint        As Long
    CodeBase          As Long
    DataBase          As Long
    ImageBase         As Long
    SectionAlignment  As Long
    FileAlignment     As Long
    MajorOSVer        As Integer
    MinorOSVer        As Integer
    MajorImageVer     As Integer
    MinorImageVer     As Integer
    MajorSSVer        As Integer
    MinorSSVer        As Integer
    Win32Ver          As Long
    ImageSize         As Long
    HeaderSize        As Long
    Checksum          As Long
    Subsystem         As Integer
    DLLChars          As Integer
    StackRes          As Long
    StackCommit       As Long
    HeapReserve       As Long
    HeapCommit        As Long
    LoaderFlags       As Long
    RVAsAndSizes      As Long
    DataEntries(15)   As IMAGE_DATA_DIRECTORY
End Type

Public Type IMAGE_SECTION_HEADER
    SectionName(7)    As Byte
    Address           As Long
    VirtualAddress    As Long
    SizeOfData        As Long
    PData             As Long
    PReloc            As Long
    PLineNums         As Long
    RelocCount        As Integer
    LineCount         As Integer
    Characteristics   As Long
End Type

Type IMAGE_RESOURCE_DIR
    Characteristics   As Long
    TimeStamp         As Long
    MajorVersion      As Integer
    MinorVersion      As Integer
    NamedEntries      As Integer
    IDEntries         As Integer
End Type

Type RESOURCE_DIR_ENTRY
    Name              As Long
    Offset            As Long
End Type

Type RESOURCE_DATA_ENTRY
    Offset            As Long
    Size              As Long
    CodePage          As Long
    Reserved          As Long
End Type

Public Type IconDescriptor
    ID       As Long
    Offset   As Long
    Size     As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public 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
Public Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Public Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private SectionAlignment   As Long
Private FileAlignment      As Long
Private ResSectionRVA      As Long
Private ResSectionOffset   As Long
Public Function Valid_PE(hFile As Long) As Boolean
    Dim Buffer(12)      As Byte
    Dim lngBytesRead    As Long
    Dim tDosHeader      As IMAGE_DOS_HEADER

    If (hFile > 0) Then
        ReadFile hFile, tDosHeader, ByVal Len(tDosHeader), lngBytesRead, ByVal 0&
        CopyMemory Buffer(0), tDosHeader.Magic, 2
        If (Chr(Buffer(0)) & Chr(Buffer(1)) = "MZ") Then
            SetFilePointer hFile, tDosHeader.lfanew, 0, 0
            ReadFile hFile, Buffer(0), 4, lngBytesRead, ByVal 0&
            If (Chr(Buffer(0)) = "P") And (Chr(Buffer(1)) = "E") And (Buffer(2) = 0) And (Buffer(3) = 0) Then
                Valid_PE = True
                Exit Function
            End If
        End If
    End If

    Valid_PE = False

End Function
Public Function GetResTreeOffset(hFile As Long) As Long
    On Error GoTo ErrHandler:

    Dim tDos          As IMAGE_DOS_HEADER
    Dim tFile         As IMAGE_FILE_HEADER
    Dim tOptional     As IMAGE_OPTIONAL_HEADER
    Dim tSections()   As IMAGE_SECTION_HEADER
    Dim BytesRead     As Long
    Dim intC          As Integer
    Dim TreeFound     As Boolean

    TreeFound = False
    If (hFile > 0) Then
        SetFilePointer hFile, 0, 0, 0
        ' Get the offset of the Image File Header
        ReadFile hFile, tDos, Len(tDos), BytesRead, ByVal 0&
        SetFilePointer hFile, ByVal tDos.lfanew + 4, 0, 0
        ' Get the Image File Header and the Image Optional Header
        ReadFile hFile, tFile, Len(tFile), BytesRead, ByVal 0&
        ReadFile hFile, tOptional, Len(tOptional), BytesRead, ByVal 0&
        ' Get section headers
        ReDim tSections(tFile.NumberOfSections - 1) As IMAGE_SECTION_HEADER
        ReadFile hFile, tSections(0), Len(tSections(0)) * tFile.NumberOfSections, BytesRead, ByVal 0&
        ' Make sure there is a resource tree in this file
        If (tOptional.DataEntries(2).DataSize) Then
            ' Save section alignment and file alignment of image
            SectionAlignment = tOptional.SectionAlignment
            FileAlignment = tOptional.FileAlignment
            ' Determine which section contains the resource tree
            For intC = 0 To UBound(tSections)
                If (tSections(intC).VirtualAddress <= tOptional.DataEntries(2).DataRVA) _
                        And ((tSections(intC).VirtualAddress + tSections(intC).SizeOfData) > tOptional.DataEntries(2).DataRVA) Then
                    TreeFound = True
                    ' Save RVA and offset of resource section for future calculations
                    ResSectionRVA = tSections(intC).VirtualAddress
                    ResSectionOffset = tSections(intC).PData
                    ' Calculate the physical file offset of the resouce tree
                    GetResTreeOffset = tSections(intC).PData + (tOptional.DataEntries(2).DataRVA - tSections(intC).VirtualAddress)
                    Exit For
                End If
            Next intC
            If Not TreeFound Then
                GetResTreeOffset = -1
            End If
        Else
            GetResTreeOffset = -1
        End If
    Else
        GetResTreeOffset = -1
    End If
    Exit Function

ErrHandler:
    MsgBox "An error occurred while locating the resource tree. " _
            & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
            App.EXEName & " - " & eRR.Description

End Function
Public Function GetIconOffsets(hFile As Long, TreeOffset As Long, Icons() As IconDescriptor) As Long
    On Error GoTo ErrHandler:

    Dim Root          As IMAGE_RESOURCE_DIR                ' Root node of resource tree
    Dim L1Entries()   As RESOURCE_DIR_ENTRY                ' 1st level of directory entries
    Dim L2Root()      As IMAGE_RESOURCE_DIR                ' Level 2 resource directories
    Dim L2Entries()   As RESOURCE_DIR_ENTRY                ' 2nd level of directory entries
    Dim L3Root()      As IMAGE_RESOURCE_DIR                ' Level 3 resource directories
    Dim L3Entries()   As RESOURCE_DIR_ENTRY                ' 3rd level of directory entries
    Dim DataEntries() As RESOURCE_DATA_ENTRY               ' Resource data entries
    Dim DIB           As DIB_HEADER                        ' Descriptor for icon images
    Dim iLvl1         As Integer                           ' Loop Counter (first level)
    Dim iLvl2         As Integer                           ' Loop Counter (second level)
    Dim iLvl3         As Integer                           ' Loop Counter (third level)
    Dim Cursor        As Long                              ' Temp val for setting file pointer
    Dim BytesRead     As Long                              ' For ReadFile()
    Dim Count         As Integer                           ' Number of icons found

    If (hFile > 0) Then
        Count = 0
        SetFilePointer hFile, ByVal TreeOffset, 0, 0
        ' Get the root node and begin navigating the resource tree
        ReadFile hFile, Root, Len(Root), BytesRead, ByVal 0
        ReDim L2Root(Root.NamedEntries + Root.IDEntries) As IMAGE_RESOURCE_DIR
        ReDim L1Entries(Root.NamedEntries + Root.IDEntries) As RESOURCE_DIR_ENTRY
        ' Get first level child nodes
        For iLvl1 = 1 To (Root.NamedEntries + Root.IDEntries)
            SetFilePointer hFile, TreeOffset + 8 + (iLvl1 * 8), 0, 0
            ReadFile hFile, L1Entries(iLvl1), 8, BytesRead, ByVal 0&
            If L1Entries(iLvl1).Name = 3 Then
                ' Jump to level 2 and get directory
                '     Strip high-order byte from offset
                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                Cursor = Cursor + TreeOffset
                SetFilePointer hFile, ByVal Cursor, 0, 0
                ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
                ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
                ReDim L2Entries(L2Root(iLvl1).IDEntries + L2Root(iLvl1).NamedEntries) As RESOURCE_DIR_ENTRY
                For iLvl2 = 1 To (L2Root(iLvl1).IDEntries + L2Root(iLvl1).NamedEntries)
                    ' Read second level child nodes
                    CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                    Cursor = Cursor + TreeOffset
                    SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
                    ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
                    ' Jump to level 3 and get directory
                    CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                    Cursor = Cursor + TreeOffset
                    SetFilePointer hFile, ByVal Cursor, 0, 0
                    ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
                    ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
                    ReDim DataEntries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DATA_ENTRY
                    For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
                        ' Read third level child nodes
                        CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                        Cursor = Cursor + TreeOffset
                        SetFilePointer hFile, (Cursor + 8 + (iLvl3 * 8)), 0, 0
                        ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
                        ' Jump to IMAGE_DATA_ENTRY and get RVA of IconDir structure
                        SetFilePointer hFile, TreeOffset + (L3Entries(iLvl3).Offset), 0, 0
                        ReadFile hFile, DataEntries(iLvl3), 16, BytesRead, ByVal 0&
                        ' Convert RVA of IconDir structure to file offset and store
                        Count = Count + 1
                        ReDim Preserve Icons(Count) As IconDescriptor
                        Icons(Count).Offset = RVA_to_Offset(DataEntries(iLvl3).Offset)
                        ' Store ID of icon resource
                        Icons(Count).ID = L2Entries(iLvl2).Name
                        ' Store Size of icon resource
                        SetFilePointer hFile, Icons(Count).Offset, 0, 0
                        ReadFile hFile, DIB, ByVal Len(DIB), BytesRead, ByVal 0&
                        Icons(Count).Size = DIB.ImageSize + 40
                    Next iLvl3
                Next iLvl2
            End If
        Next iLvl1
    Else
        Count = 0
    End If

    ' Return the number of icons found
    GetIconOffsets = Count
    Exit Function

ErrHandler:
    MsgBox "An error occurred while locating the icon resources. " _
            & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
            App.EXEName & " - " & eRR.Description

End Function
Public Function HackDirectories(hFile As Long, ResTree As Long, DIBOffset As Long, _
                                DIBAttrib As ICON_DIR_ENTRY) As Boolean
    On Error GoTo ErrHandler:

    Dim Cursor        As Long                              ' File pointer position
    Dim Root          As IMAGE_RESOURCE_DIR                ' Root node of res tree
    Dim L1Entries()   As RESOURCE_DIR_ENTRY                ' First-level child nodes
    Dim L2Root()      As IMAGE_RESOURCE_DIR                ' Second-level root nodes
    Dim L2Entries()   As RESOURCE_DIR_ENTRY                ' Second-level child nodes
    Dim L3Root()      As IMAGE_RESOURCE_DIR                ' Third-level root nodes
    Dim L3Entries()   As RESOURCE_DIR_ENTRY                ' Third-level child nodes
    Dim DataEntries() As RESOURCE_DATA_ENTRY               ' IMAGE_RESOURCE_DATA_ENTRY structs
    Dim IcoDir        As ICON_DIR                          ' IconDirectory in EXE
    Dim iLvl1         As Integer                           ' Loop Counter (first level)
    Dim iLvl2         As Integer                           ' Loop Counter (second level)
    Dim iLvl3         As Integer                           ' Loop Counter (third level)
    Dim intC          As Integer                           ' Loop Counter (general)
    Dim BytesRead     As Long                              ' Returned by Read/WriteFile API's

    If (hFile >= 0) Then
        ' Convert DIBOffset to an RVA (needed for RESOURCE_DATA_ENTRY structures)
        DIBOffset = Offset_to_RVA(DIBOffset)
        SetFilePointer hFile, ByVal ResTree, 0, 0
        ReadFile hFile, Root, Len(Root), BytesRead, ByVal 0&
        ReDim L1Entries(Root.NamedEntries + Root.IDEntries) As RESOURCE_DIR_ENTRY
        ReDim L2Root(Root.NamedEntries + Root.IDEntries) As IMAGE_RESOURCE_DIR
        ' Loop through first-level child nodes and find RT_GROUP_ICON branch
        For iLvl1 = 1 To (Root.NamedEntries + Root.IDEntries)
            SetFilePointer hFile, ResTree + 8 + (iLvl1 * 8), 0, 0
            ReadFile hFile, L1Entries(iLvl1), 8, BytesRead, ByVal 0&
            If L1Entries(iLvl1).Name = &HE Then
                ' RT_GROUP_ICON branch found
                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                Cursor = Cursor + ResTree
                SetFilePointer hFile, Cursor, 0, 0
                ' Read second-level directory
                ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
                ReDim L2Entries(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As RESOURCE_DIR_ENTRY
                ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
                For iLvl2 = 1 To (L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries)
                    CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                    Cursor = Cursor + ResTree
                    SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
                    ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
                    CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                    Cursor = Cursor + ResTree
                    SetFilePointer hFile, Cursor, 0, 0
                    ' Read thrid-level directory
                    ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
                    ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
                    For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
                        ' Read third-level child nodes
                        CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                        Cursor = Cursor + ResTree + 8 + (iLvl3 * 8)
                        SetFilePointer hFile, Cursor, 0, 0
                        ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
                        ' Jump to RESOURCE_DATA_ENTRY
                        CopyMemory Cursor, L3Entries(iLvl3).Offset, 3
                        Cursor = Cursor + ResTree
                        SetFilePointer hFile, Cursor, 0, 0
                        ReDim Preserve DataEntries(iLvl3) As RESOURCE_DATA_ENTRY
                        ReadFile hFile, DataEntries(iLvl3), 16, BytesRead, ByVal 0&
                        ' Jump to and read ICON_DIR structure
                        Cursor = RVA_to_Offset(DataEntries(iLvl3).Offset)
                        SetFilePointer hFile, Cursor, 0, 0
                        ReadFile hFile, IcoDir, 6, BytesRead, ByVal 0&
                        For intC = 1 To IcoDir.Count
                            WriteFile hFile, DIBAttrib, Len(DIBAttrib) - 4, BytesRead, ByVal 0&
                            SetFilePointer hFile, 2, 0, 1
                        Next intC
                    Next iLvl3
                Next iLvl2
            ElseIf L1Entries(iLvl1).Name = 3 Then
                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                Cursor = Cursor + ResTree
                SetFilePointer hFile, ByVal Cursor, 0, 0
                ' Read second-level directory
                ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
                ReDim L2Entries(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As RESOURCE_DIR_ENTRY
                ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
                For iLvl2 = 1 To (L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries)
                    CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
                    Cursor = Cursor + ResTree
                    SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
                    ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
                    CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                    Cursor = Cursor + ResTree
                    SetFilePointer hFile, Cursor, 0, 0
                    ' Read thrid-level directory
                    ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
                    ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
                    For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
                        ' Read third-level child nodes
                        CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
                        Cursor = Cursor + ResTree + 8 + (iLvl3 * 8)
                        SetFilePointer hFile, Cursor, 0, 0
                        ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
                        ' Jump to and hack the RESOURCE_DATA_ENTRY
                        Cursor = L3Entries(iLvl3).Offset + ResTree
                        SetFilePointer hFile, Cursor, 0, 0
                        WriteFile hFile, DIBOffset, 4, BytesRead, ByVal 0&
                        WriteFile hFile, CLng(DIBAttrib.dwBytesInRes + 40), 4, BytesRead, ByVal 0&
                    Next iLvl3
                Next iLvl2
            End If
        Next iLvl1
    Else
        HackDirectories = False
        Exit Function
    End If

    HackDirectories = True
    Exit Function

ErrHandler:
    MsgBox "An error occurred while modifying the resource directories. " _
            & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
            App.EXEName & " - " & eRR.Description

End Function
Private Function RVA_to_Offset(RVA As Long) As Long
    On Error GoTo ErrHandler:
    Dim TempOffset    As Long                              ' Difference of RVA and start of section
    TempOffset = RVA - ResSectionRVA
    If (TempOffset >= 0) Then
        ' Calculate the file offset of the RVA
        RVA_to_Offset = ResSectionOffset + TempOffset
    Else
        RVA_to_Offset = -1
    End If
    Exit Function

ErrHandler:
    MsgBox "Error in RVA_to_Offset function: " & eRR.Number & ": " & eRR.Description, _
            vbOKOnly + vbExclamation, App.EXEName & " - Error"
End Function

Private Function Offset_to_RVA(Offset As Long) As Long
    On Error GoTo ErrHandler:

    Dim TempOffset    As Long                              ' Difference of Offset and start of section

    ' Get distance between offset and start of resource section
    TempOffset = Offset - ResSectionOffset
    If TempOffset >= 0 Then
        ' Calculate RVA of the file offset
        Offset_to_RVA = ResSectionRVA + TempOffset
    Else
        Offset_to_RVA = -1
    End If
    Exit Function

ErrHandler:
    MsgBox "Error in Offset_to_RVA function: " & eRR.Number & ": " & eRR.Description, _
            vbOKOnly + vbExclamation, App.EXEName & " - Error"

End Function


使用示例:

Private Sub Command1_Click()
    Dim eRR As String
    ReplaceIcons "C:/a.exe", "C:/a.ico", eRR
    MsgBox "Icon Changed", , "Icon Changer"
End Sub

修改exe中的Icon 示例:SheelTool( 用于源码保护,为exe加壳)备注: 设置下项目允许不安全代码。 阅读详情

相关推荐

DevExpress 重编译 替换强命名 修改源码

本文以DevExpress 11.1.8举例 必须满足几个条件 1、 必须有DXperience相应版本的全部源代码SourceCode。把全部源代码复制到X:\Program Files\DevExpress XXX\Components\Sources目录。目标目录的默认位置是在C:\Program Files\DevExpress 20XX\Components\Sources(其中X.X

a237428367的专栏 5456

更改VB程序的图标

更改VB程序的图标   替换效果,见下图。 而程序图标的是用户运行程序前,最先显现在屏幕上的事物。所以,我认为程序有需要利用一个有本人特点的图标。 这里,需要用到的是ResHacker。 本文介绍的是Visual Basic 6.0 图标更改方式,VB.NET,未必适用。     如果文章,是介绍“点击某个按钮,选择图标”之类的内容,就失去了文章的意义了。

1203

Python打包exe文件,并修改图标

大家早好、午好、晚好吖~今天来给大家整一个Python打包exe可执行文件的究极教程!当然,顺便再给它换个图标。首先把你的代码准备好,尽量不要中文路径,容易报错。这个是要用的模块它不是Python默认有的模块,所以需要我们安装一下,直接实在不会安装模块的小伙伴…来本博主的Q君羊:261823976点击蓝字叭~模块安装好后我们开始打包,注意,所有的文件都尽量用英文,免得报错了。首先我们在CMD里面把目录切换到你的代码存放的那个盘,比如我的是放在C盘,输入C: ,

weixin_62853513的博客 1182

VB6.0实现修改EXE程序的图标

当你给一家公司做技术支持的时候,需求各种各样的,其中今天遇到就是要修改某个程序的图标,代码实现如下。'//注 意:这个方法貌似只对有些EXE文件有效,这不是万能的方法,此方法只能做为参考所用。'//参 数:IconFile 图标文件 ExeFile 被修改的EXE文件。'// q1016058890 群 214016721。'//返 回 值: 成功为True,否则False。MsgBox "成功"MsgBox "失败"'//函数说明:修改EXE图标。

追逐光芒,追随内心 1724

解决winload.exe丢失或损坏的问题

当出现winload.exe丢失或损坏的问题时,可以尝试通过重启计算机、检查硬盘连接、执行系统恢复和运行自动修复工具等步骤来解决问题。在Windows操作系统中,winload.exe是一个重要的系统文件,负责启动操作系统。在本文中,我们将探讨如何解决winload.exe丢失或损坏的问题,并提供相关的源代码示例。获取有效的winload.exe文件:从可靠的来源获取有效的winload.exe文件。如果无法通过系统恢复或自动修复工具修复winload.exe文件,可以尝试手动替换该文件。

2301_79331387的博客 1万+

VB修改EXE文件图标实现

内容索引:VB源码,图形处理,图标  利用VB编写的一个小工具,用于实现修改EXE文件的图标,测试抓图如上所示,选择你要更改的EXE文件的址,再选择一个你准备好的图标文件,点击“修改图标”,原EXE文件的图标就被修改成了你选择的图标,其实不知道有什么用,我们一般不会去修改图标的,只是让理解一下内部机理,有助于我们学习编程。

替换exe图标的源码

标准模块1 Option ExplicitType DIB_HEADER Size As Long Width As Long Height As Long Planes As Integer Bitcount As Integer Reserved As Long

飞翔的专栏 1798

VB生成EXE文件图标ICO替换(不限于VB)

VB自带的图标都很丑,本工具对生成的EXE文件图标能够快捷的替换,可以换成各种自己希望的ico格式的图标,非常方便

Python打包脚本为EXE可执行文件

在 main.py 中导入 add.py 的加法函数和 sub.py 的减法函数,并通过读取 config.yaml 中的参数来决定调用加法或减法函数。若需要将一个Python小项目打包成exe,则一般会选取入口程序(或称为主程序)作为打包的入口,并通过打包工具Pyinstaller进行打包操作。Python脚本打包为EXE的过程,主要是通过将Python代码、依赖库和Python解释器一起打包成一个可执行文件,从而实现不依赖于Python环境的独立执行。这样,生成的EXE文件将使用你指定的图标。

weixin_43715601的博客 3485

易语言源码应用程序图标修改器源码.rar

易语言源码应用程序图标修改器源码.rar

exe文本替换源码.rar

exe文本替换源码.rar

从源码到EXE:Ahk2Exe内部工作原理与Compiler.ahk核心逻辑剖析

Ahk2Exe是AutoHotkey官方脚本编译器,它本身也是用AutoHotkey编写的工具,能够将AutoHotkey脚本(.ahk)转换为独立的可执行文件(.exe),让用户无需安装AutoHotkey环境即可运行脚本。本文将深入解析Ahk2Exe的内部工作原理,重点剖析Compiler.ahk的核心逻辑,帮助新手和普通用户理解脚本到可执行文件的转换过程。 ## 一、Ahk2Exe工作流程

gitblog_00154的博客 815

易语言替换图标模块源码-易语言

易语言替换图标模块源码

无源码修改exe文件图标

将exe导入到Restorator里面解析,如果解析不了就用FixRes修复一下 然后导入自己的ico替换即可

Cosmopolitan的博客 4772

修改exe程序图标 EXE程序图标随意换

修改exe程序图标 EXE程序图标随意换

Exe图标替换工具ico

Exe图标替换

java使用poi在word模板中替换柱状图、折线图、饼图、表格、文本、图片

java使用poi在word模板中替换柱状图、折线图、饼图、表格、文本、图片

遛羊的懒懒 3666

【中兴交换机MC-LAG配置】

​ 中兴mc-lag配置 主设备配置如下: Keepalive链路 interface smartgroup1023 //用作MC-LAG的Keepalive链路接口 switch attribute disable yes description For_Keepalived exit interface xgei-0/1/1/47 switch attribute disable yes description For_Keepalived exit interface

qq_31440733的博客 1万+
上一篇: vb6内嵌汇编实现图像快速对比
下一篇: 浅谈.Net中数据保存的完整性
迈克揉索芙特
博客等级 码龄24年 865粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值