• Vui lòng đọc nội qui diễn đàn để tránh bị xóa bài viết
  • Tìm kiếm trước khi đặt câu hỏi

Module Thay đổi biểu tượng EXE

Các thủ thuật về hệ thống, thư mục, tập tin và mạng
User avatar
Sweet Love
Thành viên tích cực
Thành viên tích cực
Posts: 159
Joined: Sat 06/06/2009 1:37 pm
Location: Xuân Dương - Triệu Trung - Triệu Phong - Quảng Trị
Contact:

Module Thay đổi biểu tượng EXE

Postby Sweet Love » Mon 10/08/2009 10:57 am

Thủ thuật: Thay đổi biểu tượng File Exe
Tác giả: Sweet Love Sưu tầm
Mình sưu tầm nhưng chưa hiểu lắm. Mời pro vào Test thử

Code: Select all

  1. Option Explicit
  2. Type DIB_HEADER
  3.    Size        As Long
  4.    Width       As Long
  5.    Height      As Long
  6.    Planes      As Integer
  7.    Bitcount    As Integer
  8.    Reserved    As Long
  9.    ImageSize   As Long
  10. End Type
  11.  
  12. Type ICON_DIR_ENTRY
  13.    bWidth            As Byte
  14.    bHeight           As Byte
  15.    bColorCount       As Byte
  16.    bReserved         As Byte
  17.    wPlanes           As Integer
  18.    wBitCount         As Integer
  19.    dwBytesInRes      As Long
  20.    dwImageOffset     As Long
  21. End Type
  22.  
  23. Type ICON_DIR
  24.    Reserved          As Integer
  25.    Type              As Integer
  26.    Count             As Integer
  27. End Type
  28.  
  29. Type DIB_BITS
  30.    Bits()            As Byte
  31. End Type
  32.  
  33. Public Enum Errors
  34.    FILE_CREATE_FAILED = 1000
  35.    FILE_READ_FAILED
  36.    INVALID_PE_SIGNATURE
  37.    INVALID_ICO
  38.    NO_RESOURCE_TREE
  39.    NO_ICON_BRANCH
  40.    CANT_HACK_HEADERS
  41. End Enum
  42. Public Function ReplaceIcons(Source As String, Dest As String, Error As String) As Long
  43.    
  44.    Dim IcoDir As ICON_DIR
  45.    Dim IcoDirEntry As ICON_DIR_ENTRY
  46.    Dim tBits As DIB_BITS
  47.    Dim Icons() As IconDescriptor
  48.    Dim lngRet As Long
  49.    Dim BytesRead As Long
  50.    Dim hSource As Long
  51.    Dim hDest As Long
  52.    Dim ResTree As Long
  53.    
  54.    hSource = CreateFile(Source, ByVal &H80000000, 0, ByVal 0&, 3, 0, ByVal 0)
  55.    If hSource >= 0 Then
  56.       If Valid_ICO(hSource) Then
  57.          SetFilePointer hSource, 0, 0, 0
  58.          ReadFile hSource, IcoDir, 6, BytesRead, ByVal 0&
  59.          ReadFile hSource, IcoDirEntry, 16, BytesRead, ByVal 0&
  60.          SetFilePointer hSource, IcoDirEntry.dwImageOffset, 0, 0
  61.          ReDim tBits.Bits(IcoDirEntry.dwBytesInRes) As Byte
  62.          ReadFile hSource, tBits.Bits(0), IcoDirEntry.dwBytesInRes, BytesRead, ByVal 0&
  63.          CloseHandle hSource
  64.          hDest = CreateFile(Dest, ByVal (&H80000000 Or &H40000000), 0, ByVal 0&, 3, 0, ByVal 0)
  65.          If hDest >= 0 Then
  66.             If Valid_PE(hDest) Then
  67.                ResTree = GetResTreeOffset(hDest)
  68.                If ResTree > 308 Then   ' Sanity check
  69.                   lngRet = GetIconOffsets(hDest, ResTree, Icons)
  70.                   SetFilePointer hDest, Icons(1).Offset, 0, 0
  71.                   WriteFile hDest, tBits.Bits(0), UBound(tBits.Bits), BytesRead, ByVal 0&
  72.                   If Not HackDirectories(hDest, ResTree, Icons(1).Offset, IcoDirEntry) Then
  73.                      Err.Raise CANT_HACK_HEADERS, App.EXEName, "Unable to modify directories in target executable.  File may not contain any icon resources."
  74.                   End If
  75.                Else
  76.                   Err.Raise NO_RESOURCE_TREE, App.EXEName, Dest & " does not contain a valid resource tree.  File may be corrupt."
  77.                   CloseHandle hDest
  78.                End If
  79.             Else
  80.                Err.Raise INVALID_PE_SIGNATURE, App.EXEName, Dest & " is not a valid Win32 executable."
  81.                CloseHandle hDest
  82.             End If
  83.          CloseHandle hDest
  84.          Else
  85.             Err.Raise FILE_CREATE_FAILED, App.EXEName, "Failed to open " & Dest & ". Make sure file is not in use by another program."
  86.          End If
  87.       Else
  88.          Err.Raise INVALID_ICO, App.EXEName, Source & " is not a valid icon resource file."
  89.          CloseHandle hSource
  90.       End If
  91.    Else
  92.       Err.Raise FILE_CREATE_FAILED, App.EXEName, "Failed to open " & Source & ". Make sure file is not in use by another program."
  93.    End If
  94.    ReplaceIcons = 0
  95.    Exit Function
  96. ErrHandler:
  97.    ReplaceIcons = Err.Number
  98.    Error = Err.Description
  99. End Function
  100. Public Function Valid_ICO(hFile As Long) As Boolean
  101.    Dim tDir          As ICON_DIR
  102.    Dim BytesRead     As Long
  103.    If (hFile > 0) Then
  104.       ReadFile hFile, tDir, Len(tDir), BytesRead, ByVal 0&
  105.       If (tDir.Reserved = 0) And (tDir.Type = 1) And (tDir.Count > 0) Then
  106.          Valid_ICO = True
  107.       Else
  108.          Valid_ICO = False
  109.       End If
  110.    Else
  111.       Valid_ICO = False
  112.    End If
  113. End Function


Nguyễn Xuân Khánh
Lớp 11B1 trường THPT Nguyễn Huệ thị xã Quảng Trị

User avatar
clarkkent
Mạnh Thường Quân
Mạnh Thường Quân
Posts: 1641
Joined: Wed 16/04/2008 11:25 am
Location: Chợ Lách - Bến Tre
Been thanked: 31 times
Contact:

Re: Module Thay đổi biểu tượng EXE

Postby clarkkent » Mon 10/08/2009 11:08 am

Thiếu module
• Hôm bây: www.tinsoftware.com ^ ^
Cố gắng lên...

User avatar
T7
Thành viên danh dự
Thành viên danh dự
Posts: 415
Joined: Thu 24/05/2007 8:19 pm
Location: Long Xuyên - An Giang
Been thanked: 12 times
Contact:

Re: Module Thay đổi biểu tượng EXE

Postby T7 » Mon 10/08/2009 4:33 pm

Dùng cái module này xem :D
Attachments
SwapIcon.rar
(6.24 KiB) Downloaded 299 times
While (i <= you) i++;

User avatar
clarkkent
Mạnh Thường Quân
Mạnh Thường Quân
Posts: 1641
Joined: Wed 16/04/2008 11:25 am
Location: Chợ Lách - Bến Tre
Been thanked: 31 times
Contact:

Re: Module Thay đổi biểu tượng EXE

Postby clarkkent » Mon 10/08/2009 4:52 pm

T7 wrote:Dùng cái module này xem :D

xài InsertIcons hả T7, không ổn, nó chạy rồi không mở được file đó nữa

bài đầu thiếu module này thôi

Code: Select all

  1. Option Explicit
  2. Public Type IMAGE_DOS_HEADER
  3.    Magic    As Integer
  4.    cblp     As Integer
  5.    cp       As Integer
  6.    crlc     As Integer
  7.    cparhdr  As Integer
  8.    minalloc As Integer
  9.    maxalloc As Integer
  10.    ss       As Integer
  11.    sp       As Integer
  12.    csum     As Integer
  13.    ip       As Integer
  14.    cs       As Integer
  15.    lfarlc   As Integer
  16.    ovno     As Integer
  17.    res(3)   As Integer
  18.    oemid    As Integer
  19.    oeminfo  As Integer
  20.    res2(9)  As Integer
  21.    lfanew      As Long
  22. End Type
  23.  
  24. Public Type IMAGE_FILE_HEADER
  25.    Machine              As Integer
  26.    NumberOfSections     As Integer
  27.    TimeDateStamp        As Long
  28.    PointerToSymbolTable As Long
  29.    NumberOfSymbols      As Long
  30.    SizeOfOtionalHeader  As Integer
  31.    Characteristics      As Integer
  32. End Type
  33.  
  34. Public Type IMAGE_DATA_DIRECTORY
  35.    DataRVA     As Long
  36.    DataSize    As Long
  37. End Type
  38.  
  39. Public Type IMAGE_OPTIONAL_HEADER
  40.    Magic             As Integer
  41.    MajorLinkVer      As Byte
  42.    MinorLinkVer      As Byte
  43.    CodeSize          As Long
  44.    InitDataSize      As Long
  45.    unInitDataSize    As Long
  46.    EntryPoint        As Long
  47.    CodeBase          As Long
  48.    DataBase          As Long
  49.    ImageBase         As Long
  50.    SectionAlignment  As Long
  51.    FileAlignment     As Long
  52.    MajorOSVer        As Integer
  53.    MinorOSVer        As Integer
  54.    MajorImageVer     As Integer
  55.    MinorImageVer     As Integer
  56.    MajorSSVer        As Integer
  57.    MinorSSVer        As Integer
  58.    Win32Ver          As Long
  59.    ImageSize         As Long
  60.    HeaderSize        As Long
  61.    Checksum          As Long
  62.    Subsystem         As Integer
  63.    DLLChars          As Integer
  64.    StackRes          As Long
  65.    StackCommit       As Long
  66.    HeapReserve       As Long
  67.    HeapCommit        As Long
  68.    LoaderFlags       As Long
  69.    RVAsAndSizes      As Long
  70.    DataEntries(15)   As IMAGE_DATA_DIRECTORY
  71. End Type
  72.  
  73. Public Type IMAGE_SECTION_HEADER
  74.    SectionName(7)    As Byte
  75.    Address           As Long
  76.    VirtualAddress    As Long
  77.    SizeOfData        As Long
  78.    PData             As Long
  79.    PReloc            As Long
  80.    PLineNums         As Long
  81.    RelocCount        As Integer
  82.    LineCount         As Integer
  83.    Characteristics   As Long
  84. End Type
  85.  
  86. Type IMAGE_RESOURCE_DIR
  87.    Characteristics   As Long
  88.    TimeStamp         As Long
  89.    MajorVersion      As Integer
  90.    MinorVersion      As Integer
  91.    NamedEntries      As Integer
  92.    IDEntries         As Integer
  93. End Type
  94.  
  95. Type RESOURCE_DIR_ENTRY
  96.    Name              As Long
  97.    Offset            As Long
  98. End Type
  99.  
  100. Type RESOURCE_DATA_ENTRY
  101.    Offset            As Long
  102.    Size              As Long
  103.    CodePage          As Long
  104.    Reserved          As Long
  105. End Type
  106.  
  107. Public Type IconDescriptor
  108.    ID       As Long
  109.    Offset   As Long
  110.    Size     As Long
  111. End Type
  112.  
  113. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  114. 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
  115. 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
  116. 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
  117. Public Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
  118. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  119.  
  120. Private SectionAlignment   As Long
  121. Private FileAlignment      As Long
  122. Private ResSectionRVA      As Long
  123. Private ResSectionOffset   As Long
  124. Public Function Valid_PE(hFile As Long) As Boolean
  125.    
  126.    Dim Buffer(12)      As Byte
  127.    Dim lngBytesRead    As Long
  128.    Dim tDosHeader      As IMAGE_DOS_HEADER
  129.    
  130.    If (hFile > 0) Then
  131.       ReadFile hFile, tDosHeader, ByVal Len(tDosHeader), lngBytesRead, ByVal 0&
  132.       CopyMemory Buffer(0), tDosHeader.Magic, 2
  133.       If (Chr(Buffer(0)) & Chr(Buffer(1)) = "MZ") Then
  134.          SetFilePointer hFile, tDosHeader.lfanew, 0, 0
  135.          ReadFile hFile, Buffer(0), 4, lngBytesRead, ByVal 0&
  136.          If (Chr(Buffer(0)) = "P") And (Chr(Buffer(1)) = "E") And (Buffer(2) = 0) And (Buffer(3) = 0) Then
  137.             Valid_PE = True
  138.             Exit Function
  139.          End If
  140.       End If
  141.    End If
  142.    
  143.    Valid_PE = False
  144.    
  145. End Function
  146. Public Function GetResTreeOffset(hFile As Long) As Long
  147. On Error GoTo ErrHandler:
  148.    
  149.    Dim tDos          As IMAGE_DOS_HEADER
  150.    Dim tFile         As IMAGE_FILE_HEADER
  151.    Dim tOptional     As IMAGE_OPTIONAL_HEADER
  152.    Dim tSections()   As IMAGE_SECTION_HEADER
  153.    Dim BytesRead     As Long
  154.    Dim intC          As Integer
  155.    Dim TreeFound     As Boolean
  156.    
  157.    TreeFound = False
  158.    If (hFile > 0) Then
  159.       SetFilePointer hFile, 0, 0, 0
  160.       ' Get the offset of the Image File Header
  161.       ReadFile hFile, tDos, Len(tDos), BytesRead, ByVal 0&
  162.       SetFilePointer hFile, ByVal tDos.lfanew + 4, 0, 0
  163.       ' Get the Image File Header and the Image Optional Header
  164.       ReadFile hFile, tFile, Len(tFile), BytesRead, ByVal 0&
  165.       ReadFile hFile, tOptional, Len(tOptional), BytesRead, ByVal 0&
  166.       ' Get section headers
  167.       ReDim tSections(tFile.NumberOfSections - 1) As IMAGE_SECTION_HEADER
  168.       ReadFile hFile, tSections(0), Len(tSections(0)) * tFile.NumberOfSections, BytesRead, ByVal 0&
  169.       ' Make sure there is a resource tree in this file
  170.       If (tOptional.DataEntries(2).DataSize) Then
  171.          ' Save section alignment and file alignment of image
  172.          SectionAlignment = tOptional.SectionAlignment
  173.          FileAlignment = tOptional.FileAlignment
  174.          ' Determine which section contains the resource tree
  175.          For intC = 0 To UBound(tSections)
  176.             If (tSections(intC).VirtualAddress <= tOptional.DataEntries(2).DataRVA) _
  177.              And ((tSections(intC).VirtualAddress + tSections(intC).SizeOfData) > tOptional.DataEntries(2).DataRVA) Then
  178.                TreeFound = True
  179.                ' Save RVA and offset of resource section for future calculations
  180.                ResSectionRVA = tSections(intC).VirtualAddress
  181.                ResSectionOffset = tSections(intC).PData
  182.                ' Calculate the physical file offset of the resouce tree
  183.                GetResTreeOffset = tSections(intC).PData + (tOptional.DataEntries(2).DataRVA - tSections(intC).VirtualAddress)
  184.                Exit For
  185.             End If
  186.          Next intC
  187.          If Not TreeFound Then
  188.             GetResTreeOffset = -1
  189.          End If
  190.       Else
  191.          GetResTreeOffset = -1
  192.       End If
  193.    Else
  194.       GetResTreeOffset = -1
  195.    End If
  196.    Exit Function
  197.  
  198. ErrHandler:
  199.    MsgBox "An error occurred while locating the resource tree. " _
  200.     & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
  201.     App.EXEName & " - " & eRR.Description
  202.    
  203. End Function
  204. Public Function GetIconOffsets(hFile As Long, TreeOffset As Long, Icons() As IconDescriptor) As Long
  205. On Error GoTo ErrHandler:
  206.  
  207.    Dim Root          As IMAGE_RESOURCE_DIR      ' Root node of resource tree
  208.    Dim L1Entries()   As RESOURCE_DIR_ENTRY      ' 1st level of directory entries
  209.    Dim L2Root()      As IMAGE_RESOURCE_DIR      ' Level 2 resource directories
  210.    Dim L2Entries()   As RESOURCE_DIR_ENTRY      ' 2nd level of directory entries
  211.    Dim L3Root()      As IMAGE_RESOURCE_DIR      ' Level 3 resource directories
  212.    Dim L3Entries()   As RESOURCE_DIR_ENTRY      ' 3rd level of directory entries
  213.    Dim DataEntries() As RESOURCE_DATA_ENTRY     ' Resource data entries
  214.    Dim DIB           As DIB_HEADER              ' Descriptor for icon images
  215.    Dim iLvl1         As Integer                 ' Loop Counter (first level)
  216.    Dim iLvl2         As Integer                 ' Loop Counter (second level)
  217.    Dim iLvl3         As Integer                 ' Loop Counter (third level)
  218.    Dim Cursor        As Long                    ' Temp val for setting file pointer
  219.    Dim BytesRead     As Long                    ' For ReadFile()
  220.    Dim Count         As Integer                 ' Number of icons found
  221.    
  222.    If (hFile > 0) Then
  223.       Count = 0
  224.       SetFilePointer hFile, ByVal TreeOffset, 0, 0
  225.       ' Get the root node and begin navigating the resource tree
  226.       ReadFile hFile, Root, Len(Root), BytesRead, ByVal 0
  227.       ReDim L2Root(Root.NamedEntries + Root.IDEntries) As IMAGE_RESOURCE_DIR
  228.       ReDim L1Entries(Root.NamedEntries + Root.IDEntries) As RESOURCE_DIR_ENTRY
  229.       ' Get first level child nodes
  230.       For iLvl1 = 1 To (Root.NamedEntries + Root.IDEntries)
  231.          SetFilePointer hFile, TreeOffset + 8 + (iLvl1 * 8), 0, 0
  232.          ReadFile hFile, L1Entries(iLvl1), 8, BytesRead, ByVal 0&
  233.          If L1Entries(iLvl1).Name = 3 Then
  234.             ' Jump to level 2 and get directory
  235.             '     Strip high-order byte from offset
  236.             CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  237.             Cursor = Cursor + TreeOffset
  238.             SetFilePointer hFile, ByVal Cursor, 0, 0
  239.             ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
  240.             ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
  241.             ReDim L2Entries(L2Root(iLvl1).IDEntries + L2Root(iLvl1).NamedEntries) As RESOURCE_DIR_ENTRY
  242.             For iLvl2 = 1 To (L2Root(iLvl1).IDEntries + L2Root(iLvl1).NamedEntries)
  243.                ' Read second level child nodes
  244.                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  245.                Cursor = Cursor + TreeOffset
  246.                SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
  247.                ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
  248.                ' Jump to level 3 and get directory
  249.                CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  250.                Cursor = Cursor + TreeOffset
  251.                SetFilePointer hFile, ByVal Cursor, 0, 0
  252.                ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
  253.                ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
  254.                ReDim DataEntries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DATA_ENTRY
  255.                For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
  256.                   ' Read third level child nodes
  257.                   CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  258.                   Cursor = Cursor + TreeOffset
  259.                   SetFilePointer hFile, (Cursor + 8 + (iLvl3 * 8)), 0, 0
  260.                   ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
  261.                   ' Jump to IMAGE_DATA_ENTRY and get RVA of IconDir structure
  262.                   SetFilePointer hFile, TreeOffset + (L3Entries(iLvl3).Offset), 0, 0
  263.                   ReadFile hFile, DataEntries(iLvl3), 16, BytesRead, ByVal 0&
  264.                   ' Convert RVA of IconDir structure to file offset and store
  265.                   Count = Count + 1
  266.                   ReDim Preserve Icons(Count) As IconDescriptor
  267.                   Icons(Count).Offset = RVA_to_Offset(DataEntries(iLvl3).Offset)
  268.                   ' Store ID of icon resource
  269.                   Icons(Count).ID = L2Entries(iLvl2).Name
  270.                   ' Store Size of icon resource
  271.                   SetFilePointer hFile, Icons(Count).Offset, 0, 0
  272.                   ReadFile hFile, DIB, ByVal Len(DIB), BytesRead, ByVal 0&
  273.                   Icons(Count).Size = DIB.ImageSize + 40
  274.                Next iLvl3
  275.             Next iLvl2
  276.          End If
  277.       Next iLvl1
  278.    Else
  279.       Count = 0
  280.    End If
  281.    
  282.    ' Return the number of icons found
  283.    GetIconOffsets = Count
  284.    Exit Function
  285.    
  286. ErrHandler:
  287.    MsgBox "An error occurred while locating the icon resources. " _
  288.     & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
  289.     App.EXEName & " - " & eRR.Description
  290.    
  291. End Function
  292. Public Function HackDirectories(hFile As Long, ResTree As Long, DIBOffset As Long, _
  293.                                 DIBAttrib As ICON_DIR_ENTRY) As Boolean
  294. On Error GoTo ErrHandler:
  295.  
  296.    Dim Cursor        As Long                 ' File pointer position
  297.    Dim Root          As IMAGE_RESOURCE_DIR   ' Root node of res tree
  298.    Dim L1Entries()   As RESOURCE_DIR_ENTRY   ' First-level child nodes
  299.    Dim L2Root()      As IMAGE_RESOURCE_DIR   ' Second-level root nodes
  300.    Dim L2Entries()   As RESOURCE_DIR_ENTRY   ' Second-level child nodes
  301.    Dim L3Root()      As IMAGE_RESOURCE_DIR   ' Third-level root nodes
  302.    Dim L3Entries()   As RESOURCE_DIR_ENTRY   ' Third-level child nodes
  303.    Dim DataEntries() As RESOURCE_DATA_ENTRY  ' IMAGE_RESOURCE_DATA_ENTRY structs
  304.    Dim IcoDir        As ICON_DIR             ' IconDirectory in EXE
  305.    Dim iLvl1         As Integer              ' Loop Counter (first level)
  306.    Dim iLvl2         As Integer              ' Loop Counter (second level)
  307.    Dim iLvl3         As Integer              ' Loop Counter (third level)
  308.    Dim intC          As Integer              ' Loop Counter (general)
  309.    Dim BytesRead     As Long                 ' Returned by Read/WriteFile API's
  310.    
  311.    If (hFile >= 0) Then
  312.       ' Convert DIBOffset to an RVA (needed for RESOURCE_DATA_ENTRY structures)
  313.       DIBOffset = Offset_to_RVA(DIBOffset)
  314.       SetFilePointer hFile, ByVal ResTree, 0, 0
  315.       ReadFile hFile, Root, Len(Root), BytesRead, ByVal 0&
  316.       ReDim L1Entries(Root.NamedEntries + Root.IDEntries) As RESOURCE_DIR_ENTRY
  317.       ReDim L2Root(Root.NamedEntries + Root.IDEntries) As IMAGE_RESOURCE_DIR
  318.       ' Loop through first-level child nodes and find RT_GROUP_ICON branch
  319.       For iLvl1 = 1 To (Root.NamedEntries + Root.IDEntries)
  320.          SetFilePointer hFile, ResTree + 8 + (iLvl1 * 8), 0, 0
  321.          ReadFile hFile, L1Entries(iLvl1), 8, BytesRead, ByVal 0&
  322.          If L1Entries(iLvl1).Name = &HE Then
  323.             ' RT_GROUP_ICON branch found
  324.             CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  325.             Cursor = Cursor + ResTree
  326.             SetFilePointer hFile, Cursor, 0, 0
  327.             ' Read second-level directory
  328.             ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
  329.             ReDim L2Entries(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As RESOURCE_DIR_ENTRY
  330.             ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
  331.             For iLvl2 = 1 To (L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries)
  332.                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  333.                Cursor = Cursor + ResTree
  334.                SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
  335.                ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
  336.                CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  337.                Cursor = Cursor + ResTree
  338.                SetFilePointer hFile, Cursor, 0, 0
  339.                ' Read thrid-level directory
  340.                ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
  341.                ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
  342.                For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
  343.                   ' Read third-level child nodes
  344.                   CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  345.                   Cursor = Cursor + ResTree + 8 + (iLvl3 * 8)
  346.                   SetFilePointer hFile, Cursor, 0, 0
  347.                   ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
  348.                   ' Jump to RESOURCE_DATA_ENTRY
  349.                   CopyMemory Cursor, L3Entries(iLvl3).Offset, 3
  350.                   Cursor = Cursor + ResTree
  351.                   SetFilePointer hFile, Cursor, 0, 0
  352.                   ReDim Preserve DataEntries(iLvl3) As RESOURCE_DATA_ENTRY
  353.                   ReadFile hFile, DataEntries(iLvl3), 16, BytesRead, ByVal 0&
  354.                   ' Jump to and read ICON_DIR structure
  355.                   Cursor = RVA_to_Offset(DataEntries(iLvl3).Offset)
  356.                   SetFilePointer hFile, Cursor, 0, 0
  357.                   ReadFile hFile, IcoDir, 6, BytesRead, ByVal 0&
  358.                   For intC = 1 To IcoDir.Count
  359.                      WriteFile hFile, DIBAttrib, Len(DIBAttrib) - 4, BytesRead, ByVal 0&
  360.                      SetFilePointer hFile, 2, 0, 1
  361.                   Next intC
  362.                Next iLvl3
  363.             Next iLvl2
  364.          ElseIf L1Entries(iLvl1).Name = 3 Then
  365.             CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  366.             Cursor = Cursor + ResTree
  367.             SetFilePointer hFile, ByVal Cursor, 0, 0
  368.             ' Read second-level directory
  369.             ReadFile hFile, L2Root(iLvl1), 16, BytesRead, ByVal 0&
  370.             ReDim L2Entries(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As RESOURCE_DIR_ENTRY
  371.             ReDim L3Root(L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries) As IMAGE_RESOURCE_DIR
  372.             For iLvl2 = 1 To (L2Root(iLvl1).NamedEntries + L2Root(iLvl1).IDEntries)
  373.                CopyMemory Cursor, L1Entries(iLvl1).Offset, 3
  374.                Cursor = Cursor + ResTree
  375.                SetFilePointer hFile, Cursor + 8 + (iLvl2 * 8), 0, 0
  376.                ReadFile hFile, L2Entries(iLvl2), 8, BytesRead, ByVal 0&
  377.                CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  378.                Cursor = Cursor + ResTree
  379.                SetFilePointer hFile, Cursor, 0, 0
  380.                ' Read thrid-level directory
  381.                ReadFile hFile, L3Root(iLvl2), 16, BytesRead, ByVal 0&
  382.                ReDim L3Entries(L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries) As RESOURCE_DIR_ENTRY
  383.                For iLvl3 = 1 To (L3Root(iLvl2).NamedEntries + L3Root(iLvl2).IDEntries)
  384.                   ' Read third-level child nodes
  385.                   CopyMemory Cursor, L2Entries(iLvl2).Offset, 3
  386.                   Cursor = Cursor + ResTree + 8 + (iLvl3 * 8)
  387.                   SetFilePointer hFile, Cursor, 0, 0
  388.                   ReadFile hFile, L3Entries(iLvl3), 8, BytesRead, ByVal 0&
  389.                   ' Jump to and hack the RESOURCE_DATA_ENTRY
  390.                   Cursor = L3Entries(iLvl3).Offset + ResTree
  391.                   SetFilePointer hFile, Cursor, 0, 0
  392.                   WriteFile hFile, DIBOffset, 4, BytesRead, ByVal 0&
  393.                   WriteFile hFile, CLng(DIBAttrib.dwBytesInRes + 40), 4, BytesRead, ByVal 0&
  394.                Next iLvl3
  395.             Next iLvl2
  396.          End If
  397.       Next iLvl1
  398.    Else
  399.       HackDirectories = False
  400.       Exit Function
  401.    End If
  402.    
  403.    HackDirectories = True
  404.    Exit Function
  405.    
  406. ErrHandler:
  407.    MsgBox "An error occurred while modifying the resource directories. " _
  408.     & " Please make sure neither of the specified files are in use.", vbOKOnly + vbExclamation, _
  409.     App.EXEName & " - " & eRR.Description
  410.    
  411. End Function
  412. Private Function RVA_to_Offset(RVA As Long) As Long
  413. On Error GoTo ErrHandler:
  414.    Dim TempOffset    As Long           ' Difference of RVA and start of section
  415.    TempOffset = RVA - ResSectionRVA
  416.    If (TempOffset >= 0) Then
  417.       ' Calculate the file offset of the RVA
  418.       RVA_to_Offset = ResSectionOffset + TempOffset
  419.    Else
  420.       RVA_to_Offset = -1
  421.    End If
  422.    Exit Function
  423.    
  424. ErrHandler:
  425.    MsgBox "Error in RVA_to_Offset function: " & eRR.Number & ": " & eRR.Description, _
  426.     vbOKOnly + vbExclamation, App.EXEName & " - Error"
  427. End Function
  428.  
  429. Private Function Offset_to_RVA(Offset As Long) As Long
  430. On Error GoTo ErrHandler:
  431.  
  432.    Dim TempOffset    As Long          ' Difference of Offset and start of section
  433.    
  434.    ' Get distance between offset and start of resource section
  435.    TempOffset = Offset - ResSectionOffset
  436.    If TempOffset >= 0 Then
  437.       ' Calculate RVA of the file offset
  438.       Offset_to_RVA = ResSectionRVA + TempOffset
  439.    Else
  440.       Offset_to_RVA = -1
  441.    End If
  442.    Exit Function
  443.    
  444. ErrHandler:
  445.   MsgBox "Error in Offset_to_RVA function: " & eRR.Number & ": " & eRR.Description, _
  446.     vbOKOnly + vbExclamation, App.EXEName & " - Error"
  447.    
  448. End Function
  449.  
• Hôm bây: www.tinsoftware.com ^ ^
Cố gắng lên...


Return to “[VB] Hệ thống - Tập tin - Thư mục và Mạng”

Who is online

Users browsing this forum: No registered users and 0 guests