-
Notifications
You must be signed in to change notification settings - Fork 1
/
Class2.vb
164 lines (148 loc) · 6.95 KB
/
Class2.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Imports System.Runtime.InteropServices
Imports System.Security
Public Class IconInjector
<SuppressUnmanagedCodeSecurity()>
Private Class NativeMethods
<DllImport("kernel32")>
Public Shared Function BeginUpdateResource(
ByVal fileName As String,
<MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr
End Function
<DllImport("kernel32")>
Public Shared Function UpdateResource(
ByVal hUpdate As IntPtr,
ByVal type As IntPtr,
ByVal name As IntPtr,
ByVal language As Short,
<MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)>
ByVal data() As Byte,
ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("kernel32")>
Public Shared Function EndUpdateResource(
ByVal hUpdate As IntPtr,
<MarshalAs(UnmanagedType.Bool)> ByVal discard As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
End Class
<StructLayout(LayoutKind.Sequential)>
Private Structure ICONDIR
Public Reserved As UShort
Public Type As UShort
Public Count As UShort
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure ICONDIRENTRY
Public Width As Byte
Public Height As Byte
Public ColorCount As Byte
Public Reserved As Byte
Public Planes As UShort
Public BitCount As UShort
Public BytesInRes As Integer
Public ImageOffset As Integer
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure BITMAPINFOHEADER
Public Size As UInteger
Public Width As Integer
Public Height As Integer
Public Planes As UShort
Public BitCount As UShort
Public Compression As UInteger
Public SizeImage As UInteger
Public XPelsPerMeter As Integer
Public YPelsPerMeter As Integer
Public ClrUsed As UInteger
Public ClrImportant As UInteger
End Structure
<StructLayout(LayoutKind.Sequential, Pack:=2)>
Private Structure GRPICONDIRENTRY
Public Width As Byte
Public Height As Byte
Public ColorCount As Byte
Public Reserved As Byte
Public Planes As UShort
Public BitCount As UShort
Public BytesInRes As Integer
Public ID As UShort
End Structure
Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String)
InjectIcon(exeFileName, iconFileName, 1, 1)
End Sub
Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String, ByVal iconGroupID As UInteger, ByVal iconBaseID As UInteger)
Const RT_ICON = 3UI
Const RT_GROUP_ICON = 14UI
Dim iconFile As IconFile = IconFile.FromFile(iconFileName)
Dim hUpdate = NativeMethods.BeginUpdateResource(exeFileName, False)
Dim data = iconFile.CreateIconGroupData(iconBaseID)
NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_GROUP_ICON), New IntPtr(iconGroupID), 0, data, data.Length)
For i = 0 To iconFile.ImageCount - 1
Dim image = iconFile.ImageData(i)
NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_ICON), New IntPtr(iconBaseID + i), 0, image, image.Length)
Next
NativeMethods.EndUpdateResource(hUpdate, False)
End Sub
Private Class IconFile
Private iconDir As New ICONDIR
Private iconEntry() As ICONDIRENTRY
Private iconImage()() As Byte
Public ReadOnly Property ImageCount() As Integer
Get
Return iconDir.Count
End Get
End Property
Public ReadOnly Property ImageData(ByVal index As Integer) As Byte()
Get
Return iconImage(index)
End Get
End Property
Private Sub New()
End Sub
Public Shared Function FromFile(ByVal filename As String) As IconFile
Dim instance As New IconFile
Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename)
Dim pinnedBytes = GCHandle.Alloc(fileBytes, GCHandleType.Pinned)
instance.iconDir = DirectCast(Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject, GetType(ICONDIR)), ICONDIR)
instance.iconEntry = New ICONDIRENTRY(instance.iconDir.Count - 1) {}
instance.iconImage = New Byte(instance.iconDir.Count - 1)() {}
Dim offset = Marshal.SizeOf(instance.iconDir)
Dim iconDirEntryType = GetType(ICONDIRENTRY)
Dim size = Marshal.SizeOf(iconDirEntryType)
For i = 0 To instance.iconDir.Count - 1
Dim entry = DirectCast(Marshal.PtrToStructure(New IntPtr(pinnedBytes.AddrOfPinnedObject.ToInt64 + offset), iconDirEntryType), ICONDIRENTRY)
instance.iconEntry(i) = entry
instance.iconImage(i) = New Byte(entry.BytesInRes - 1) {}
Buffer.BlockCopy(fileBytes, entry.ImageOffset, instance.iconImage(i), 0, entry.BytesInRes)
offset += size
Next
pinnedBytes.Free()
Return instance
End Function
Public Function CreateIconGroupData(ByVal iconBaseID As UInteger) As Byte()
Dim sizeOfIconGroupData As Integer = Marshal.SizeOf(GetType(ICONDIR)) + Marshal.SizeOf(GetType(GRPICONDIRENTRY)) * ImageCount
Dim data(sizeOfIconGroupData - 1) As Byte
Dim pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned)
Marshal.StructureToPtr(iconDir, pinnedData.AddrOfPinnedObject, False)
Dim offset = Marshal.SizeOf(iconDir)
For i = 0 To ImageCount - 1
Dim grpEntry As New GRPICONDIRENTRY
Dim bitmapheader As New BITMAPINFOHEADER
Dim pinnedBitmapInfoHeader = GCHandle.Alloc(bitmapheader, GCHandleType.Pinned)
Marshal.Copy(ImageData(i), 0, pinnedBitmapInfoHeader.AddrOfPinnedObject, Marshal.SizeOf(GetType(BITMAPINFOHEADER)))
pinnedBitmapInfoHeader.Free()
grpEntry.Width = iconEntry(i).Width
grpEntry.Height = iconEntry(i).Height
grpEntry.ColorCount = iconEntry(i).ColorCount
grpEntry.Reserved = iconEntry(i).Reserved
grpEntry.Planes = bitmapheader.Planes
grpEntry.BitCount = bitmapheader.BitCount
grpEntry.BytesInRes = iconEntry(i).BytesInRes
grpEntry.ID = CType(iconBaseID + i, UShort)
Marshal.StructureToPtr(grpEntry, New IntPtr(pinnedData.AddrOfPinnedObject.ToInt64 + offset), False)
offset += Marshal.SizeOf(GetType(GRPICONDIRENTRY))
Next
pinnedData.Free()
Return data
End Function
End Class
End Class