PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/CCFramework.ahk

http://github.com/maul-esel/COM-Classes
AutoHotKey | 241 lines | 76 code | 19 blank | 146 comment | 9 complexity | eb99066bb3772dd04ac4fdbdf2193471 MD5 | raw file
  1. /*
  2. class: CCFramework
  3. the main class for the framework that has a lot of methods to ease the handling of other classes.
  4. Remarks:
  5. This class cannot be instantiated.
  6. */
  7. class CCFramework extends _CCF_Error_Handler_
  8. {
  9. /*
  10. Field: heap
  11. static field that holds the process heap. For internal use only.
  12. */
  13. static heap := DllCall("GetProcessHeap", "UPtr")
  14. /*
  15. Method: constructor
  16. throws an exception when an attempt is made to create an instance of this class
  17. */
  18. __New(p*)
  19. {
  20. throw Exception("CCFramework: This class must not be instantiated!", -1)
  21. }
  22. /*
  23. Method: GUID2String
  24. Converts a GUID structure in memory to its string representation.
  25. Parameters:
  26. UPTR guid - the pointer to the GUID structure
  27. Returns:
  28. STR string - the string representation of the GUID.
  29. */
  30. GUID2String(guid)
  31. {
  32. local string
  33. DllCall("Ole32.dll\StringFromCLSID", "UPtr", guid, "UPtr*", string)
  34. return StrGet(string, "UTF-16")
  35. }
  36. /*
  37. Method: String2GUID
  38. Converts a string represntation of a GUID to a GUID structure in memory.
  39. Parameters:
  40. STR string - the string representation
  41. [opt] UPTR guid - the pointer where to place the GUID in memory.
  42. Returns:
  43. UPTR guid - the pointer to the GUID structure.
  44. Remarks:
  45. If the "guid" parameter is ommitted, memory is allocted using <AllocateMemory>. In this case, you may pass the pointer returned by this method to <FreeMemory()> when you don't need the GUID any longer.
  46. */
  47. String2GUID(string, guid := 0)
  48. {
  49. if (!guid)
  50. guid := CCFramework.AllocateMemory(16)
  51. return DllCall("ole32\CLSIDFromString", "Str", string, "UPtr", guid) >= 0 ? guid : 0
  52. }
  53. /*
  54. Method: AllocateMemory
  55. allocates a specified amount of memory
  56. Parameters:
  57. UINT bytes - the number of bytes to allocate
  58. Returns:
  59. UPTR buffer - the pointer to the allocated memory.
  60. Remarks:
  61. When you no longer need the memory, you should pass the pointer returned by this method to <FreeMemory()>.
  62. */
  63. AllocateMemory(bytes)
  64. {
  65. static HEAP_GENERATE_EXCEPTIONS := 0x00000004, HEAP_ZERO_MEMORY := 0x00000008
  66. return DllCall("HeapAlloc", "UPtr", CCFramework.heap, "UInt", HEAP_GENERATE_EXCEPTIONS|HEAP_ZERO_MEMORY, "UInt", bytes)
  67. }
  68. /*
  69. Method: FreeMemory
  70. Frees memory previously allocated by <AllocateMemory>.
  71. Parameters:
  72. UPTR buffer - the memory pointer as returned by <AllocateMemory()>.
  73. Returns:
  74. BOOL success - true on success, false otherwise
  75. */
  76. FreeMemory(buffer)
  77. {
  78. return DllCall("HeapFree", "UPtr", CCFramework.heap, "UInt", 0, "UPtr", buffer)
  79. }
  80. /*
  81. Method: CopyMemory
  82. Copies memory from one locatin to another
  83. Parameters:
  84. UPTR src - the pointer to the source memory
  85. UPTR dest - the pointer to the memory to copy data to
  86. UINT size - the number of bytes to copy
  87. */
  88. CopyMemory(src, dest, size)
  89. {
  90. DllCall("RtlMoveMemory", "UPtr", dest, "UPtr", src, "UInt", size)
  91. }
  92. /*
  93. Method: CreateVARIANT
  94. creates a VARIANT wrapper object from a given value
  95. Parameters:
  96. VAR value - the value to store in the VARIANT structure
  97. Returns:
  98. OBJ variant - an object representing the variant, containing 3 fields:
  99. PTR ref - the pointer to the VARIANT structure
  100. UINT vt - the value type of the VARIANT structure
  101. VAR value - the value in the structure: a string, integer, pointer, COM wrapper object (for dispatch objects), ...
  102. Remarks:
  103. The type is calculated automatically based on the value. If you want it to have a special type, create a value with ComObjParameter:
  104. > dispVariant := CCFramework.CreateVARIANT(ComObjParameter(VT_DISPATCH, disp_ptr))
  105. */
  106. CreateVARIANT(value)
  107. {
  108. static VT_VARIANT := 0xC, VT_BYREF := 0x4000, VT_UNKNOWN := 0xD
  109. local array, arr_data, variant, err
  110. err := ComObjError(false)
  111. if (IsObject(value) && value.HasKey("ref") && value.HasKey("vt") && value.HasKey("value"))
  112. return value
  113. ComObjError(err)
  114. array := ComObjArray(VT_VARIANT, 1)
  115. array[0] := value
  116. DllCall("oleaut32\SafeArrayAccessData", "Ptr", ComObjValue(array), "Ptr*", arr_data)
  117. variant := CCFramework.AllocateMemory(16), CCFramework.CopyMemory(arr_data, variant, 16)
  118. DllCall("oleaut32\SafeArrayUnaccessData", "Ptr", ComObjValue(array))
  119. return { "ref" : variant, "vt" : NumGet(1*variant, 00, "UShort"), "value" : NumGet(1*variant, 00, "UShort") == VT_UNKNOWN ? NumGet(1*variant, 08, "Ptr") : array[0] }
  120. }
  121. /*
  122. Method: CreateVARIANTARG
  123. an alias for <CreateVARIANT>.
  124. */
  125. CreateVARIANTARG(value)
  126. {
  127. return CCFramework.CreateVARIANT(value)
  128. }
  129. /*
  130. Method: BuildVARIANT
  131. builds a VARIANT wrapper object from a given pointer
  132. Parameters:
  133. PTR ptr - the pointer to the VARIANT structure in memory
  134. Returns:
  135. OBJ variant - see <CreateVARIANT>
  136. */
  137. BuildVARIANT(ptr)
  138. {
  139. return CCFramework.CreateVARIANT(ComObjParameter(NumGet(1*ptr, 00, "UShort"), NumGet(1*ptr, 08, "Int64")))
  140. }
  141. /*
  142. Method: BuildVARIANTARG
  143. an alias for <BuildVARIANT>.
  144. */
  145. BuildVARIANTARG(ptr)
  146. {
  147. return CCFramework.BuildVARIANT(ptr)
  148. }
  149. /*
  150. Method: FormatError
  151. retrieves the error message for a HRESULT error code
  152. Parameters:
  153. HRESULT error - the error code, e.g. A_LastError
  154. Returns:
  155. STR description - the error message
  156. Credits:
  157. Inspired by Bentschi's A_LastError() (<http://de.autohotkey.com/forum/viewtopic.php?t=8010>)
  158. */
  159. FormatError(error)
  160. {
  161. static ALLOCATE_BUFFER := 0x00000100, FROM_SYSTEM := 0x00001000, IGNORE_INSERTS := 0x00000200
  162. local size, msg, bufaddr
  163. size := DllCall("FormatMessageW", "UInt", ALLOCATE_BUFFER|FROM_SYSTEM|IGNORE_INSERTS, "UPtr", 0, "UInt", error, "UInt", 0, "UPtr*", bufaddr, "UInt", 0, "UPtr", 0)
  164. msg := StrGet(bufaddr, size, "UTF-16")
  165. return error . " - " . msg
  166. }
  167. /*
  168. Method: isInteger
  169. for AHK v2, this replaces "if var is integer".
  170. Parameters:
  171. VAR value - the value to test
  172. Returns:
  173. BOOL isInt - true if the value is an integer, false otherwise
  174. Credits:
  175. Thanks jaco0646 for this code! (<http://www.autohotkey.com/forum/viewtopic.php?p=507283#507283>)
  176. */
  177. isInteger(value)
  178. {
  179. return Type(value) == "Integer" || RegExMatch(Trim(value),"^[+-]?([[:digit:]]+|(0x)?[[:xdigit:]]+)$")
  180. }
  181. /*
  182. Method: HasEnumFlag
  183. checks if a given binary combination of flags includes a specified flag
  184. Parameters:
  185. UINT var - the combination to test
  186. UINT flag - the flag to test for
  187. Returns:
  188. BOOL included - true if the flag is included, false otherwise
  189. Remarks:
  190. All flags added to "var" as well as "flag" must be powers of 2.
  191. */
  192. HasEnumFlag(var, flag)
  193. {
  194. return (var & flag) == flag
  195. }
  196. }