Skip to content

Commit 8a3498e

Browse files
author
maul.esel
committed
improve exception messages for ITL_InterfaceWrapper, remove old exception code
1 parent 5ae337d commit 8a3498e

File tree

1 file changed

+30
-45
lines changed

1 file changed

+30
-45
lines changed

ITL_InterfaceWrapper.ahk

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,25 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
2626
, INVOKEKIND_FUNC := 1
2727
, VT_USERDEFINED := 29, VT_RECORD := 36, VT_UNKNOWN := 13, VT_PTR := 26
2828
, TYPEKIND_RECORD := 1, TYPEKIND_INTERFACE := 3
29-
local paramCount, dispparams, rgvarg := 0, hr, info, dispid := DISPID_UNKNOWN, instance, excepInfo, err_index := -1, result, variant, index := -1, funcdesc := 0, vt, fnFill ;, fn
29+
local paramCount, dispparams, rgvarg := 0, hr, info, dispid := DISPID_UNKNOWN, typeName, instance, excepInfo, err_index := -1, result, variant, index := -1, funcdesc := 0, vt, fnFill ;, fn
3030
, refHandle, refInfo := 0, refAttr := 0, refKind, tdesc, indirectionLevel
3131

3232
paramCount := params.maxIndex() > 0 ? params.maxIndex() : 0 ; the ternary is necessary, otherwise it would hold an empty string, causing calculations to fail
3333
, info := this.base[ITL.Properties.TYPE_TYPEINFO]
3434
, instance := this[ITL.Properties.INSTANCE_POINTER]
35+
, typeName := this.base[ITL.Properties.TYPE_NAME]
3536

3637
; init structures
3738
if (VarSetCapacity(dispparams, sizeof_DISPPARAMS, 00) < sizeof_DISPPARAMS)
3839
{
39-
;throw Exception("Out of memory.", -1)
4040
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for DISPPARAMS failed.", ErrorLevel)*)
4141
}
4242
if (VarSetCapacity(result, sizeof_VARIANT, 00) < sizeof_VARIANT)
4343
{
44-
;throw Exception("Out of memory.", -1)
4544
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for the result VARIANT failed.", ErrorLevel)*)
4645
}
4746
if (VarSetCapacity(excepInfo, sizeof_EXCEPINFO, 00) < sizeof_EXCEPINFO)
4847
{
49-
;throw Exception("Out of memory.", -1)
5048
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for EXCEPINFO failed.", ErrorLevel)*)
5149
}
5250

@@ -63,9 +61,8 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
6361
}
6462
}
6563
*/
66-
;throw Exception("GetIDsOfNames() for """ method """ failed.", -1, ITL_FormatError(hr))
67-
throw Exception(ITL_FormatException("Failed to call a method."
68-
, "ITypeInfo::GetIDsOfNames() for """ method """ failed."
64+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
65+
, "ITypeInfo::GetIDsOfNames() failed."
6966
, ErrorLevel, hr
7067
, dispid != DISPID_UNKNOWN, "Invalid DISPID: " dispid)*)
7168
}
@@ -78,28 +75,25 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
7875
hr := DllCall(NumGet(NumGet(info+0), 24*A_PtrSize, "Ptr"), "Ptr", info, "UInt", dispid, "UInt", INVOKEKIND_FUNC, "UInt*", index) ; ITypeInfo2::GetFuncIndexOfMemId(_this, dispid, invkind, [out] index)
7976
if (ITL_FAILED(hr) || index == -1)
8077
{
81-
;throw Exception("ITypeInfo2::GetFuncIndexOfMemId() failed.", -1, ITL_FormatError(hr))
82-
throw Exception(ITL_FormatException("Failed to call a method."
83-
, "ITypeInfo2::GetFuncIndexOfMemId() for """ method """ failed."
78+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
79+
, "ITypeInfo2::GetFuncIndexOfMemId() failed."
8480
, ErrorLevel, hr
8581
, index == -1, "Invalid function index: " index)*)
8682
}
8783

8884
hr := DllCall(NumGet(NumGet(info+0), 05*A_PtrSize, "Ptr"), "Ptr", info, "UInt", index, "Ptr*", funcdesc) ; ITypeInfo::GetFuncDesc(_this, index, [out] funcdesc)
8985
if (ITL_FAILED(hr) || !funcdesc)
9086
{
91-
;throw Exception("ITypeInfo::GetFuncDesc() failed.", -1, ITL_FormatError(hr))
92-
throw Exception(ITL_FormatException("Failed to call a method."
93-
, "ITypeInfo::GetFuncDesc() for """ method """ (index " index ") failed."
87+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
88+
, "ITypeInfo::GetFuncDesc() failed (index " index ")."
9489
, ErrorLevel, hr
9590
, !funcdesc, "Invalid FUNCDESC pointer: " funcdesc)*)
9691
}
9792

9893
paramArray := NumGet(1*funcdesc, 04 + A_PtrSize, "Ptr") ; FUNCDESC::lprgelemdescParam
9994
if (!paramArray)
10095
{
101-
;throw Exception("Array of parameter descriptions could not be read.", -1)
102-
throw Exception(ITL_FormatException("Failed to call a method."
96+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
10397
, "The array of parameter descriptions (FUNCDESC::lprgelemdescParam) could not be read."
10498
, ErrorLevel, ""
10599
, !paramArray, "Invalid ELEMDESC[] pointer: " paramArray)*)
@@ -129,15 +123,15 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
129123
hr := DllCall(NumGet(NumGet(info+0), 14*A_PtrSize, "Ptr"), "Ptr", info, "UInt", refHandle, "Ptr*", refInfo, "Int") ; ITypeInfo::GetRefTypeInfo()
130124
if (ITL_FAILED(hr) || !refInfo)
131125
{
132-
throw Exception(ITL_FormatException("Failed to call a method."
126+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
133127
, "ITypeInfo::GetRefTypeInfo() for param " A_Index " (handle: " refHandle ") failed."
134128
, ErrorLevel, hr
135129
, !refInfo, "Invalid ITypeInfo pointer: " refInfo)*)
136130
}
137131
hr := DllCall(NumGet(NumGet(refInfo+0), 03*A_PtrSize, "Ptr"), "Ptr", refInfo, "Ptr*", refAttr, "Int") ; ITypeInfo::GetTypeAttr()
138132
if (ITL_FAILED(hr) || !refAttr)
139133
{
140-
throw Exception(ITL_FormatException("Failed to call a method."
134+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
141135
, "ITypeInfo::GetTypeAttr() for param " A_Index " failed."
142136
, ErrorLevel, hr
143137
, !refAttr, "Invalid TYPEATTR pointer: " refAttr)*)
@@ -156,7 +150,7 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
156150
{
157151
if (indirectionLevel < 1)
158152
{
159-
throw Exception(ITL_FormatException("Failed to call a method."
153+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
160154
, "Interfaces cannot be passed by value."
161155
, ErrorLevel, ""
162156
, indirectionLevel < 1, "Invalid indirection level: " indirectionLevel)*)
@@ -166,7 +160,7 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
166160
else
167161
{
168162
ObjRelease(refInfo) ; cleanup
169-
throw Exception(ITL_FormatException("Failed to call a method."
163+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
170164
, "Cannot handle other wrappers than interfaces and structures."
171165
, ErrorLevel, "")*)
172166
}
@@ -207,13 +201,12 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
207201
if (fnFill)
208202
DllCall(fnFill, "Ptr", &excepInfo)
209203
hr := (hr := NumGet(excepInfo, 08+5*A_PtrSize, "Int")) ? hr : NumGet(excepInfo, 00, "UShort") ; get EXCEPINFO::scode or EXCEPINFO::wCode
210-
throw Exception(ITL_FormatException("Failed to call a method."
204+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
211205
, "The called method raised an exception: Source=""" StrGet(NumGet(excepInfo, 04, "Ptr")) """, Message=""" StrGet(NumGet(excepInfo, 04 + A_PtrSize, "Ptr")) """"
212206
, ErrorLevel, hr)*)
213207
}
214-
;throw Exception("""" method "()"" could not be called.", -1, ITL_FormatError(hr))
215-
throw Exception(ITL_FormatException("Failed to call a method."
216-
, "ITypeInfo::Invoke() failed for """ method """."
208+
throw Exception(ITL_FormatException("Failed to call method """ typeName "::" method "()""!"
209+
, "ITypeInfo::Invoke() failed."
217210
, ErrorLevel, hr
218211
, (hr == DISP_E_TYPEMISMATCH || hr == DISP_E_PARAMNOTFOUND || hr == DISP_E_BADVARTYPE), "Invalid argument: #" err_index)*)
219212
}
@@ -228,37 +221,34 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
228221
static DISPATCH_PROPERTYGET := 0x2, DISPATCH_METHOD := 0x1
229222
, DISPID_UNKNOWN := -1
230223
, sizeof_DISPPARAMS := 8 + 2 * A_PtrSize, sizeof_EXCEPINFO := 12 + 5 * A_PtrSize, sizeof_VARIANT := 8 + 2 * A_PtrSize
231-
local dispparams, hr, info, dispid := DISPID_UNKNOWN, instance, excepInfo, err_index, result
224+
local dispparams, hr, info, dispid := DISPID_UNKNOWN, instance, excepInfo, err_index, result, typeName
232225

233226
if (property != "base" && !ITL.Properties.IsInternalProperty(property)) ; ignore base and internal properties (handled by ITL_WrapperBaseClass)
234227
{
235228
; init structures
236229
if (VarSetCapacity(dispparams, sizeof_DISPPARAMS, 00) != sizeof_DISPPARAMS)
237230
{
238-
;throw Exception("Out of memory.", -1)
239231
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for DISPPARAMS failed.", ErrorLevel)*)
240232
}
241233
if (VarSetCapacity(result, sizeof_VARIANT, 00) != sizeof_VARIANT)
242234
{
243-
;throw Exception("Out of memory.", -1)
244235
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for the result VARIANT failed.", ErrorLevel)*)
245236
}
246237
if (VarSetCapacity(excepInfo, sizeof_EXCEPINFO, 00) != sizeof_EXCEPINFO)
247238
{
248-
;throw Exception("Out of memory.", -1)
249239
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for EXCEPINFO failed.", ErrorLevel)*)
250240
}
251241

252242
info := this.base[ITL.Properties.TYPE_TYPEINFO]
253243
, instance := this[ITL.Properties.INSTANCE_POINTER]
244+
, typeName := this.base[ITL.Properties.TYPE_NAME]
254245

255246
; get MEMBERID for the method to be retrieved:
256247
hr := DllCall(NumGet(NumGet(info+0), 10*A_PtrSize, "Ptr"), "Ptr", info, "Str*", property, "UInt", 1, "UInt*", dispid, "Int") ; ITypeInfo::GetIDsOfNames()
257248
if (ITL_FAILED(hr) || dispid == DISPID_UNKNOWN)
258249
{
259-
;throw Exception("GetIDsOfNames() for """ property """ failed.", -1, ITL_FormatError(hr))
260-
throw Exception(ITL_FormatException("Failed to retrieve a property."
261-
, "ITypeInfo::GetIDsOfNames() for """ property """ failed."
250+
throw Exception(ITL_FormatException("Failed to retrieve property """ typeName "::" property """!"
251+
, "ITypeInfo::GetIDsOfNames() failed."
262252
, ErrorLevel, hr
263253
, dispid == DISPID_UNKNOWN, "Invalid DISPID: " dispid)*)
264254
}
@@ -268,9 +258,8 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
268258
hr := DllCall(NumGet(NumGet(info+0), 11*A_PtrSize, "Ptr"), "Ptr", info, "Ptr", instance, "UInt", dispid, "UShort", DISPATCH_METHOD | DISPATCH_PROPERTYGET, "Ptr", &dispparams, "Ptr", &result, "Ptr", &excepInfo, "Ptr", 0, "Int") ; ITypeInfo::Invoke()
269259
if (ITL_FAILED(hr))
270260
{
271-
;throw Exception("""" property """ could not be retrieved.", -1, ITL_FormatError(hr))
272-
throw Exception(ITL_FormatException("Failed to retrieve a property."
273-
, "ITypeInfo::Invoke() for """ property """ failed."
261+
throw Exception(ITL_FormatException("Failed to retrieve property """ typeName "::" property """!"
262+
, "ITypeInfo::Invoke() failed."
274263
, ErrorLevel, hr)*)
275264
}
276265
return ITL_VARIANT_GetValue(&result) ; return the result, i.e. the value of the property
@@ -287,7 +276,7 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
287276
, sizeof_DISPPARAMS := 8 + 2 * A_PtrSize, sizeof_EXCEPINFO := 12 + 5 * A_PtrSize
288277
, VT_UNKNOWN := 13, VT_DISPATCH := 9
289278
, DISP_E_MEMBERNOTFOUND := -2147352573
290-
local variant, dispparams, hr, info, dispid := DISPID_UNKNOWN, vt, instance, excepInfo, err_index := 0, variant
279+
local variant, dispparams, hr, info, dispid := DISPID_UNKNOWN, vt, instance, excepInfo, err_index := 0, variant, typeName
291280

292281
; need to store it that way as "DISPID_PROPERTYPUT := -3, &DISPID_PROPERTYPUT" would be a STRING address
293282
if (!DISPID_PROPERTYPUT)
@@ -298,12 +287,10 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
298287
; init structures
299288
if (VarSetCapacity(dispparams, sizeof_DISPPARAMS, 00) != sizeof_DISPPARAMS)
300289
{
301-
;throw Exception("Out of memory.", -1)
302290
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for DISPPARAMS failed.", ErrorLevel)*)
303291
}
304292
if (VarSetCapacity(excepInfo, sizeof_EXCEPINFO, 00) != sizeof_EXCEPINFO)
305293
{
306-
;throw Exception("Out of memory.", -1)
307294
throw Exception(ITL_FormatException("Out of memory", "Memory allocation for EXCEPINFO failed.", ErrorLevel)*)
308295
}
309296

@@ -317,14 +304,14 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
317304

318305
info := this.base[ITL.Properties.TYPE_TYPEINFO]
319306
, instance := this[ITL.Properties.INSTANCE_POINTER]
307+
, typeName := this.base[ITL.Properties.TYPE_NAME]
320308

321309
; get MEMBERID for the property to be set:
322310
hr := DllCall(NumGet(NumGet(info+0), 10*A_PtrSize, "Ptr"), "Ptr", info, "Str*", property, "UInt", 1, "UInt*", dispid, "Int") ; ITypeInfo::GetIDsOfNames()
323311
if (ITL_FAILED(hr) || dispid == DISPID_UNKNOWN) ; an error code was returned or the ID is invalid
324312
{
325-
;throw Exception("GetIDsOfNames() for """ property """ failed.", -1, ITL_FormatError(hr))
326-
throw Exception(ITL_FormatException("Failed to set a property."
327-
, "ITypeInfo::GetIDsOfNames() for """ property """ failed."
313+
throw Exception(ITL_FormatException("Failed to set property """ typeName "::" property """ to """ value """!"
314+
, "ITypeInfo::GetIDsOfNames() failed."
328315
, ErrorLevel, hr
329316
, dispid == DISPID_UNKNOWN, "Invalid DISPID: " dispid)*)
330317
}
@@ -342,9 +329,8 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
342329
}
343330
else if (hr != DISP_E_MEMBERNOTFOUND) ; if member not found, retry below with DISPATCH_PROPERTYPUT
344331
{
345-
;throw Exception("""" property """ could not be set.", -1, ITL_FormatError(hr)) ; otherwise an error occured
346-
throw Exception(ITL_FormatException("Failed to set a property."
347-
, "ITypeInfo::Invoke() for """ property """ failed."
332+
throw Exception(ITL_FormatException("Failed to set property """ typeName "::" property """ to """ value """!" ; otherwise an error occured
333+
, "ITypeInfo::Invoke() failed."
348334
, ErrorLevel, hr)*)
349335
}
350336
}
@@ -354,9 +340,8 @@ class ITL_InterfaceWrapper extends ITL.ITL_WrapperBaseClass
354340
hr := DllCall(NumGet(NumGet(info+0), 11*A_PtrSize, "Ptr"), "Ptr", info, "Ptr", instance, "UInt", dispid, "UShort", DISPATCH_PROPERTYPUT, "Ptr", &dispparams, "Ptr*", 0, "Ptr", &excepInfo, "UInt*", err_index, "Int") ; ITypeInfo::Invoke()
355341
if (ITL_FAILED(hr))
356342
{
357-
;throw Exception("""" property """ could not be set.", -1, ITL_FormatError(hr))
358-
throw Exception(ITL_FormatException("Failed to set a property."
359-
, "ITypeInfo::Invoke() for """ property """ failed."
343+
throw Exception(ITL_FormatException("Failed to set property """ typeName "::" property """ to """ value """!"
344+
, "ITypeInfo::Invoke() failed."
360345
, ErrorLevel, hr)*)
361346
}
362347
return value ; return the original value to allow "a := obj.prop := value" and similar

0 commit comments

Comments
 (0)