exe-Dateiicon zur Laufzeit ändern

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von nikeee13.

    exe-Dateiicon zur Laufzeit ändern

    Hallo!
    ich suche eine Möglichkeit, das Icon meiner exe-Datei zur Laufzeit zu ändern. Es reicht mir nicht, dass nur das Icon oben links in den Fenstern geändert wird und eine Verknüpfung ist nicht ausreichend. Kennt jemand eine Möglichkeit dafür?

    Danke und schönen Abend,
    m477h35
    Das geht, allerdings darf dein Prog1.exe dabei nicht geöffnet sein.

    Das Icon würde so aussehen, wie du es definierst.
    Spass bei Seite:
    Das Icon musst du wahrscheinlich ändern, indem du die Ressourcen von Prog1 editierst. Das geht irgendwie über die WindowsAPI. Kann sein, dass es mal was hier gab dazu.
    Von meinem iPhone gesendet

    bla schrieb:

    credits: "miharbi: This is An Icon Changer Using The UpdateResource API.
    Credits Goes To a Chinese guy and julien for helping with pointers and for the nice comment"


    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Imports System.Security
    3. Public Class IconInjector
    4. ' Basically, you can change icons with the UpdateResource api call.
    5. ' When you make the call you say "I'm updating an icon", and you send the icon data.
    6. ' The main problem is that ICO files store the icons in one set of structures, and exe/dll files store them in
    7. ' another set of structures. So you have to translate between the two -- you can't just load the ICO file as
    8. ' bytes and send them with the UpdateResource api call.
    9. <SuppressUnmanagedCodeSecurity()> _
    10. Private Class NativeMethods
    11. <DllImport("kernel32")> _
    12. Public Shared Function BeginUpdateResource( _
    13. ByVal fileName As String, _
    14. <MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr
    15. End Function
    16. <DllImport("kernel32")> _
    17. Public Shared Function UpdateResource( _
    18. ByVal hUpdate As IntPtr, _
    19. ByVal type As IntPtr, _
    20. ByVal name As IntPtr, _
    21. ByVal language As Short, _
    22. <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)> _
    23. ByVal data() As Byte, _
    24. ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    25. End Function
    26. <DllImport("kernel32")> _
    27. Public Shared Function EndUpdateResource( _
    28. ByVal hUpdate As IntPtr, _
    29. <MarshalAs(UnmanagedType.Bool)> ByVal discard As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    30. End Function
    31. End Class
    32. ' The first structure in an ICO file lets us know how many images are in the file.
    33. <StructLayout(LayoutKind.Sequential)> _
    34. Private Structure ICONDIR
    35. Public Reserved As UShort ' Reserved, must be 0
    36. Public Type As UShort ' Resource type, 1 for icons.
    37. Public Count As UShort ' How many images.
    38. ' The native structure has an array of ICONDIRENTRYs as a final field.
    39. End Structure
    40. ' Each ICONDIRENTRY describes one icon stored in the ico file. The offset says where the icon image data
    41. ' starts in the file. The other fields give the information required to turn that image data into a valid
    42. ' bitmap.
    43. <StructLayout(LayoutKind.Sequential)> _
    44. Private Structure ICONDIRENTRY
    45. Public Width As Byte ' Width, in pixels, of the image
    46. Public Height As Byte ' Height, in pixels, of the image
    47. Public ColorCount As Byte ' Number of colors in image (0 if >=8bpp)
    48. Public Reserved As Byte ' Reserved ( must be 0)
    49. Public Planes As UShort ' Color Planes
    50. Public BitCount As UShort ' Bits per pixel
    51. Public BytesInRes As Integer ' Length in bytes of the pixel data
    52. Public ImageOffset As Integer ' Offset in the file where the pixel data starts.
    53. End Structure
    54. ' Each image is stored in the file as an ICONIMAGE structure:
    55. 'typdef struct
    56. '{
    57. ' BITMAPINFOHEADER icHeader; // DIB header
    58. ' RGBQUAD icColors[1]; // Color table
    59. ' BYTE icXOR[1]; // DIB bits for XOR mask
    60. ' BYTE icAND[1]; // DIB bits for AND mask
    61. '} ICONIMAGE, *LPICONIMAGE;
    62. <StructLayout(LayoutKind.Sequential)> _
    63. Private Structure BITMAPINFOHEADER
    64. Public Size As UInteger
    65. Public Width As Integer
    66. Public Height As Integer
    67. Public Planes As UShort
    68. Public BitCount As UShort
    69. Public Compression As UInteger
    70. Public SizeImage As UInteger
    71. Public XPelsPerMeter As Integer
    72. Public YPelsPerMeter As Integer
    73. Public ClrUsed As UInteger
    74. Public ClrImportant As UInteger
    75. End Structure
    76. ' The icon in an exe/dll file is stored in a very similar structure:
    77. <StructLayout(LayoutKind.Sequential, Pack:=2)> _
    78. Private Structure GRPICONDIRENTRY
    79. Public Width As Byte
    80. Public Height As Byte
    81. Public ColorCount As Byte
    82. Public Reserved As Byte
    83. Public Planes As UShort
    84. Public BitCount As UShort
    85. Public BytesInRes As Integer
    86. Public ID As UShort
    87. End Structure
    88. Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String)
    89. InjectIcon(exeFileName, iconFileName, 1, 1)
    90. End Sub
    91. Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String, ByVal iconGroupID As UInteger, ByVal iconBaseID As UInteger)
    92. Const RT_ICON = 3UI
    93. Const RT_GROUP_ICON = 14UI
    94. Dim iconFile As IconFile = iconFile.FromFile(iconFileName)
    95. Dim hUpdate = NativeMethods.BeginUpdateResource(exeFileName, False)
    96. Dim data = iconFile.CreateIconGroupData(iconBaseID)
    97. NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_GROUP_ICON), New IntPtr(iconGroupID), 0, data, data.Length)
    98. For i = 0 To iconFile.ImageCount - 1
    99. Dim image = iconFile.ImageData(i)
    100. NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_ICON), New IntPtr(iconBaseID + i), 0, image, image.Length)
    101. Next
    102. NativeMethods.EndUpdateResource(hUpdate, False)
    103. End Sub
    104. Private Class IconFile
    105. Private iconDir As New ICONDIR
    106. Private iconEntry() As ICONDIRENTRY
    107. Private iconImage()() As Byte
    108. Public ReadOnly Property ImageCount As Integer
    109. Get
    110. Return iconDir.Count
    111. End Get
    112. End Property
    113. Public ReadOnly Property ImageData(ByVal index As Integer) As Byte()
    114. Get
    115. Return iconImage(index)
    116. End Get
    117. End Property
    118. Private Sub New()
    119. End Sub
    120. Public Shared Function FromFile(ByVal filename As String) As IconFile
    121. Dim instance As New IconFile
    122. ' Read all the bytes from the file.
    123. Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename)
    124. ' First struct is an ICONDIR
    125. ' Pin the bytes from the file in memory so that we can read them.
    126. ' If we didn't pin them then they could move around (e.g. when the
    127. ' garbage collector compacts the heap)
    128. Dim pinnedBytes = GCHandle.Alloc(fileBytes, GCHandleType.Pinned)
    129. ' Read the ICONDIR
    130. instance.iconDir = DirectCast(Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject, GetType(ICONDIR)), ICONDIR)
    131. ' which tells us how many images are in the ico file. For each image, there's a ICONDIRENTRY, and associated pixel data.
    132. instance.iconEntry = New ICONDIRENTRY(instance.iconDir.Count - 1) {}
    133. instance.iconImage = New Byte(instance.iconDir.Count - 1)() {}
    134. ' The first ICONDIRENTRY will be immediately after the ICONDIR, so the offset to it is the size of ICONDIR
    135. Dim offset = Marshal.SizeOf(instance.iconDir)
    136. ' After reading an ICONDIRENTRY we step forward by the size of an ICONDIRENTRY
    137. Dim iconDirEntryType = GetType(ICONDIRENTRY)
    138. Dim size = Marshal.SizeOf(iconDirEntryType)
    139. For i = 0 To instance.iconDir.Count - 1
    140. ' Grab the structure.
    141. Dim entry = DirectCast(Marshal.PtrToStructure(New IntPtr(pinnedBytes.AddrOfPinnedObject.ToInt64 + offset), iconDirEntryType), ICONDIRENTRY)
    142. instance.iconEntry(i) = entry
    143. ' Grab the associated pixel data.
    144. instance.iconImage(i) = New Byte(entry.BytesInRes - 1) {}
    145. Buffer.BlockCopy(fileBytes, entry.ImageOffset, instance.iconImage(i), 0, entry.BytesInRes)
    146. offset += size
    147. Next
    148. pinnedBytes.Free()
    149. Return instance
    150. End Function
    151. Public Function CreateIconGroupData(ByVal iconBaseID As UInteger) As Byte()
    152. ' This will store the memory version of the icon.
    153. Dim sizeOfIconGroupData As Integer = Marshal.SizeOf(GetType(ICONDIR)) + Marshal.SizeOf(GetType(GRPICONDIRENTRY)) * ImageCount
    154. Dim data(sizeOfIconGroupData - 1) As Byte
    155. Dim pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned)
    156. Marshal.StructureToPtr(iconDir, pinnedData.AddrOfPinnedObject, False)
    157. Dim offset = Marshal.SizeOf(iconDir)
    158. For i = 0 To ImageCount - 1
    159. Dim grpEntry As New GRPICONDIRENTRY
    160. Dim bitmapheader As New BITMAPINFOHEADER
    161. Dim pinnedBitmapInfoHeader = GCHandle.Alloc(bitmapheader, GCHandleType.Pinned)
    162. Marshal.Copy(ImageData(i), 0, pinnedBitmapInfoHeader.AddrOfPinnedObject, Marshal.SizeOf(GetType(BITMAPINFOHEADER)))
    163. pinnedBitmapInfoHeader.Free()
    164. grpEntry.Width = iconEntry(i).Width
    165. grpEntry.Height = iconEntry(i).Height
    166. grpEntry.ColorCount = iconEntry(i).ColorCount
    167. grpEntry.Reserved = iconEntry(i).Reserved
    168. grpEntry.Planes = bitmapheader.Planes
    169. grpEntry.BitCount = bitmapheader.BitCount
    170. grpEntry.BytesInRes = iconEntry(i).BytesInRes
    171. grpEntry.ID = CType(iconBaseID + i, UShort)
    172. Marshal.StructureToPtr(grpEntry, New IntPtr(pinnedData.AddrOfPinnedObject.ToInt64 + offset), False)
    173. offset += Marshal.SizeOf(GetType(GRPICONDIRENTRY))
    174. Next
    175. pinnedData.Free()
    176. Return data
    177. End Function
    178. End Class
    179. End Class




    VB.NET-Quellcode

    1. IconInjector.InjectIcon(ExePath, IconPath)
    Bitteschön.