Skip to content

Commit 1ee9866

Browse files
author
cyruz-git
committed
Improved error management
1 parent 28ec3de commit 1ee9866

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

Diff for: UpdRes.ahk

+50-38
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
; ----------------------------------------------------------------------------------------------------------------------
1414
; Function .....: UpdRes_LockResource
1515
; 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.
1819
; ..............: nResType - The resource type.
1920
; ..............: szData - Byref parameter containing the size of the resource data.
2021
; Return .......: A pointer to the first byte of the resource, 0 on error.
@@ -26,25 +27,30 @@ UpdRes_LockResource(sBinFile, sResName, nResType, ByRef szData)
2627
{
2728
; If the DLL isn't already loaded, load it as a data file.
2829
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
3031
bLoaded := 1
3132
}
3233

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+
}
4854

4955
Return pData
5056
}
@@ -65,13 +71,13 @@ UpdRes_LockResource(sBinFile, sResName, nResType, ByRef szData)
6571
UpdRes_UpdateResource(sBinFile, bDelOld, sResName, nResType, nLangId, pData, szData)
6672
{
6773
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
6975

7076
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
7278

7379
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
7581

7682
Return 1
7783
}
@@ -92,10 +98,10 @@ UpdRes_UpdateResource(sBinFile, bDelOld, sResName, nResType, nLangId, pData, szD
9298
UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
9399
{
94100
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."
96102

97103
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
99105

100106
Loop % objRes.MaxIndex()
101107
{
@@ -107,7 +113,7 @@ UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
107113
}
108114

109115
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
111117

112118
Return nUpdated
113119
}
@@ -127,9 +133,9 @@ UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, ByRef objRes)
127133
UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
128134
{
129135
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."
131137

132-
try
138+
Try
133139
{
134140
objRes := Object()
135141
Loop, %sResDir%\*.*
@@ -158,7 +164,7 @@ UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
158164
}
159165
nUpdated := UpdRes_UpdateArrayOfResources(sBinFile, bDelOld, objRes)
160166
}
161-
finally
167+
Finally
162168
{
163169
Loop % objRes.MaxIndex()
164170
DllCall( "UnmapViewOfFile", Ptr,objRes[A_Index].__pMap )
@@ -173,9 +179,10 @@ UpdRes_UpdateDirOfResources(sResDir, sBinFile, bDelOld, nResType, nLangId)
173179
; ----------------------------------------------------------------------------------------------------------------------
174180
; Function .....: UpdRes_EnumerateResources
175181
; 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.
177184
; ..............: nResType - The resource type.
178-
; Return .......: List of enumerated resources.
185+
; Return .......: List of enumerated resources, 0 on error.
179186
; Info .........: EnumResourceNames: https://msdn.microsoft.com/en-us/library/windows/desktop/ms648037(v=vs.85).aspx
180187
; ----------------------------------------------------------------------------------------------------------------------
181188
UpdRes_EnumerateResources(sBinFile, nResType)
@@ -184,18 +191,23 @@ UpdRes_EnumerateResources(sBinFile, nResType)
184191
{
185192
; If the DLL isn't already loaded, load it as a data file.
186193
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
188195
bLoaded := 1
189196
}
190197

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+
}
199211

200212
; Recover the address of the enumeration string and get it.
201213
Return StrGet(NumGet(&adrEnumList))

0 commit comments

Comments
 (0)