13
13
; ----------------------------------------------------------------------------------------------------------------------
14
14
; Function .....: UpdRes_LockResource
15
15
; Description ..: Load the specified resource and retrieve a pointer to its binary data.
16
- ; Parameters ...: sBinFile - PE file whose resource is to be retrieved.
17
- ; ..............: sResName - The name of the resource.
16
+ ; Parameters ...: sBinFile - PE file whose resource is to be retrieved. If = 0 the resource will be retrieved in the
17
+ ; ..............: current process.
18
+ ; ..............: sResName - The name of the resource or its integer identifier.
18
19
; ..............: nResType - The resource type.
19
20
; ..............: szData - Byref parameter containing the size of the resource data.
20
21
; Return .......: A pointer to the first byte of the resource, 0 on error.
@@ -26,25 +27,30 @@ UpdRes_LockResource(sBinFile, sResName, nResType, ByRef szData)
26
27
{
27
28
; If the DLL isn't already loaded, load it as a data file.
28
29
If ( ! hLib := DllCall ( " LoadLibraryEx" , Str,sBinFile, Ptr,0 , UInt,0x2 ) )
29
- Return 0 , A_LastError := " LoadLibraryEx error. `nReturn value >>`t " hLib " `nLast error >>`t " A_LastError
30
+ Return 0 , ErrorLevel := " LoadLibraryEx error`nReturn value = " hLib " `nLast error = " A_LastError
30
31
bLoaded := 1
31
32
}
32
33
33
- If ( ! hRes := DllCall ( " FindResource" , Ptr,hLib, Str,sResName, Ptr,nResType ) )
34
- Return 0 , A_LastError := " FindResource error.`nReturn value >>`t" hRes " `nLast error >>`t" A_LastError
35
-
36
- If ( ! szData := DllCall ( " SizeofResource" , Ptr,hLib, Ptr,hRes ) )
37
- Return 0 , A_LastError := " SizeofResource error.`nReturn value >>`t" szData " `nLast error >>`t" A_LastError
38
-
39
- If ( ! hData := DllCall ( " LoadResource" , Ptr,hLib, Ptr,hRes ) )
40
- Return 0 , A_LastError := " LoadResource error.`nReturn value >>`t" hData " `nLast error >>`t" A_LastError
41
-
42
- If ( ! pData := DllCall ( " LockResource" , Ptr,hData ) )
43
- Return 0 , A_LastError := " LockResource error.`nReturn value >>`t" pData " - Last error >>`t" A_LastError
44
-
45
- ; If we loaded the DLL, free it now.
46
- If ( bLoaded )
47
- DllCall ( " FreeLibrary" , Ptr,hLib )
34
+ Try
35
+ {
36
+ If ( ! hRes := DllCall ( " FindResource" , Ptr,hLib, Ptr,(sResName+ 0 ==sResName?sResname:& sResName), Ptr,nResType ) )
37
+ Return 0 , ErrorLevel := " FindResource error`nReturn value = " hRes " `nLast error = " A_LastError
38
+
39
+ If ( ! szData := DllCall ( " SizeofResource" , Ptr,hLib, Ptr,hRes ) )
40
+ Return 0 , ErrorLevel := " SizeofResource error`nReturn value = " szData " `nLast error = " A_LastError
41
+
42
+ If ( ! hData := DllCall ( " LoadResource" , Ptr,hLib, Ptr,hRes ) )
43
+ Return 0 , ErrorLevel := " LoadResource error`nReturn value = " hData " `nLast error = " A_LastError
44
+
45
+ If ( ! pData := DllCall ( " LockResource" , Ptr,hData ) )
46
+ Return 0 , ErrorLevel := " LockResource error`nReturn value = " pData " `nLast error = " A_LastError
47
+ }
48
+ Finally
49
+ {
50
+ ; If we loaded the DLL, free it now.
51
+ If ( bLoaded )
52
+ DllCall ( " FreeLibrary" , Ptr,hLib )
53
+ }
48
54
49
55
Return pData
50
56
}
@@ -65,13 +71,13 @@ UpdRes_LockResource(sBinFile, sResName, nResType, ByRef szData)
65
71
UpdRes_UpdateResource(sBinFile, bDelOld, sResName, nResType, nLangId, pData, szData)
66
72
{
67
73
If ( ! hMod := DllCall ( " BeginUpdateResource" , Str,sBinFile, Int,bDelOld ) )
68
- Return 0 , A_LastError := " BeginUpdateResource error. `nReturn value >>`t " hMod " - Last error >>`t " A_LastError
74
+ Return 0 , ErrorLevel := " BeginUpdateResource error`nReturn value = " hMod " `nLast error = " A_LastError
69
75
70
76
If ( ! e := DllCall ( " UpdateResource" , Ptr,hMod, Ptr,nResType, Str,sResName, UInt,nLangId, Ptr,pData, UInt,szData ) )
71
- Return 0 , A_LastError := " UpdateResource error. `nReturn value >>`t " e " - Last error >>`t " A_LastError
77
+ Return 0 , ErrorLevel := " UpdateResource error`nReturn value = " e " `nLast error = " A_LastError
72
78
73
79
If ( ! e := DllCall ( " EndUpdateResource" , Ptr,hMod, Int,0 ) )
74
- Return 0 , A_LastError := " EndUpdateResource error. `nReturn value >>`t " e " - Last error >>`t " A_LastError
80
+ Return 0 , ErrorLevel := " EndUpdateResource error`nReturn value = " e " `nLast error = " A_LastError
75
81
76
82
Return 1
77
83
}
@@ -92,10 +98,10 @@ UpdRes_UpdateResource(sBinFile, bDelOld, sResName, nResType, nLangId, pData, szD
92
98
UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
93
99
{
94
100
If ( ! IsObject (objRes) )
95
- Return 0 , A_LastError := " Array of resources object is not an object."
101
+ Return 0 , ErrorLevel := " Array of resources object is not an object."
96
102
97
103
If ( ! hMod := DllCall ( " BeginUpdateResource" , Str,sBinFile, Int,bDelOld ) )
98
- Return 0 , A_LastError := " BeginUpdateResource error. `nReturn value >>`t " hMod " - Last error >>`t " A_LastError
104
+ Return 0 , ErrorLevel := " BeginUpdateResource error`nReturn value = " hMod " `nLast error = " A_LastError
99
105
100
106
Loop % objRes.MaxIndex ()
101
107
{
@@ -107,7 +113,7 @@ UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
107
113
}
108
114
109
115
If ( ! e := DllCall ( " EndUpdateResource" , Ptr,hMod, Int,0 ) )
110
- Return 0 , A_LastError := " EndUpdateResource error. `nReturn value >>`t " e " - Last error >>`t " A_LastError
116
+ Return 0 , ErrorLevel := " EndUpdateResource error`nReturn value = " e " `nLast error = " A_LastError
111
117
112
118
Return nUpdated
113
119
}
@@ -127,9 +133,9 @@ UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
127
133
UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
128
134
{
129
135
If ( ! FileExist (sBinFile) || ! InStr (FileExist (sResDir), " D" ) )
130
- Return 0 , A_LastError := " Binary file or resources directory not existing."
136
+ Return 0 , ErrorLevel := " Binary file or resources directory not existing."
131
137
132
- try
138
+ Try
133
139
{
134
140
objRes := Object()
135
141
Loop , %sResDir%\* .*
@@ -158,7 +164,7 @@ UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
158
164
}
159
165
nUpdated := UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, objRes)
160
166
}
161
- finally
167
+ Finally
162
168
{
163
169
Loop % objRes.MaxIndex ()
164
170
DllCall ( " UnmapViewOfFile" , Ptr,objRes[A_Index ].__pMap )
@@ -173,9 +179,10 @@ UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
173
179
; ----------------------------------------------------------------------------------------------------------------------
174
180
; Function .....: UpdRes_EnumerateResources
175
181
; Description ..: Enumerate all the resources of a specific type inside a binary file.
176
- ; Parameters ...: sBinFile - PE file whose resources are to be enumerated.
182
+ ; Parameters ...: sBinFile - PE file whose resources are to be enumerated. If = 0 the current process resources will be
183
+ ; ..............: enumerated.
177
184
; ..............: nResType - The resource type.
178
- ; Return .......: List of enumerated resources.
185
+ ; Return .......: List of enumerated resources, 0 on error .
179
186
; Info .........: EnumResourceNames: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648037(v=vs.85).aspx
180
187
; ----------------------------------------------------------------------------------------------------------------------
181
188
UpdRes_EnumerateResources(sBinFile, nResType)
@@ -184,18 +191,23 @@ UpdRes_EnumerateResources(sBinFile, nResType)
184
191
{
185
192
; If the DLL isn't already loaded, load it as a data file.
186
193
If ( ! hLib := DllCall ( " LoadLibraryEx" , Str,sBinFile, Ptr,0 , UInt,0x2 ) )
187
- Return " LoadLibraryEx error. `nReturn value >>`t " hLib " `nLast error >>`t " A_LastError
194
+ Return 0 , ErrorLevel := " LoadLibraryEx error`nReturn value = " hLib " `nLast error = " A_LastError
188
195
bLoaded := 1
189
196
}
190
197
191
- ; Enumerate the resources of type nResType.
192
- cbEnum := RegisterCallback ( " __UpdRes_EnumeratorCallback" ), adrEnumList := 0
193
- DllCall ( " EnumResourceNames" , Ptr,hLib, Ptr,nResType, Ptr,cbEnum, Ptr,& adrEnumList )
194
- DllCall ( " GlobalFree" , Ptr,cbEnum )
195
-
196
- ; If we loaded the DLL, free it now.
197
- If ( bLoaded )
198
- DllCall ( " FreeLibrary" , Ptr,hLib )
198
+ Try
199
+ {
200
+ ; Enumerate the resources of type nResType.
201
+ cbEnum := RegisterCallback ( " __UpdRes_EnumeratorCallback" ), adrEnumList := 0
202
+ DllCall ( " EnumResourceNames" , Ptr,hLib, Ptr,nResType, Ptr,cbEnum, Ptr,& adrEnumList )
203
+ DllCall ( " GlobalFree" , Ptr,cbEnum )
204
+ }
205
+ Finally
206
+ {
207
+ ; If we loaded the DLL, free it now.
208
+ If ( bLoaded )
209
+ DllCall ( " FreeLibrary" , Ptr,hLib )
210
+ }
199
211
200
212
; Recover the address of the enumeration string and get it.
201
213
Return StrGet (NumGet (& adrEnumList))
0 commit comments