PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Storage/Storage.ahk

http://github.com/maul-esel/COM-Classes
AutoHotKey | 323 lines | 102 code | 16 blank | 205 comment | 4 complexity | 1cba574561d7657a5f79dcadfa71f605 MD5 | raw file
  1. /*
  2. class: Storage
  3. wraps the *IStorage* interface and supports the creation and management of structured storage objects.
  4. Structured storage allows hierarchical storage of information within a single file, and is often referred to as "a file system within a file".
  5. Authors:
  6. - maul.esel (https://github.com/maul-esel)
  7. License:
  8. - *LGPL* (http://www.gnu.org/licenses/lgpl-2.1.txt)
  9. Documentation:
  10. - *class documentation* (http://maul-esel.github.com/COM-Classes/master/Storage)
  11. - *msdn* (http://msdn.microsoft.com/en-us/library/windows/desktop/aa380015%28v=vs.85%29.aspx)
  12. Requirements:
  13. AutoHotkey - AHK v2 alpha
  14. OS - Windows 2000 Professional / Windows 2000 Server or higher
  15. Base classes - _CCF_Error_Handler_, Unknown
  16. Constant classes - STGM, STGC, STGMOVE, STATFLAG
  17. Structure classes - FILETIME, STATSTG
  18. Other classes - SequentialStream, Stream, EnumSTATSTG, CCFramework
  19. */
  20. class Storage extends Unknown
  21. {
  22. /*
  23. Field: IID
  24. This is IID_IStorage. It is required to create an instance.
  25. */
  26. static IID := "{0000000b-0000-0000-C000-000000000046}"
  27. /*
  28. Field: ThrowOnCreation
  29. indicates that attempting to create an instance of this class without supplying a valid pointer will throw an exception.
  30. Remarks:
  31. To get a pointer, you might use the StgOpenStorageEx or StgCreateStorageEx functions.
  32. */
  33. static ThrowOnCreation := true
  34. /*
  35. Method: CreateStream
  36. creates and opens a stream object with the specified name contained in this storage object.
  37. All elements within a storage objects, both streams and other storage objects, are kept in the same name space.
  38. Parameters:
  39. STR name - the name of the stream. The name can be used later to open or reopen the stream. The name must not exceed 31 characters in length.
  40. UINT access - Specifies the access mode to use when opening the newly created stream. You may use the fields of the STGM class for convenience.
  41. Returns:
  42. Stream stream - the created stream, either as class instance (if available) or raw interface pointer
  43. */
  44. CreateStream(name, access)
  45. {
  46. local out
  47. this._Error(DllCall(NumGet(this.vt+03*A_PtrSize), "Ptr", this.ptr, "Str", name, "UInt", access, "UInt", 0, "UInt", 0, "Ptr*", out)) ; msdn: param #3/4 are reserved
  48. return IsObject(Stream) ? new Stream(out) : out
  49. }
  50. /*
  51. Method: OpenStream
  52. opens an existing stream object within this storage object in the specified access mode.
  53. Parameters:
  54. STR name - the name of the stream
  55. UINT access - Specifies the access mode to be assigned to the open stream. You may use the fields of the STGM class for convenience.
  56. Returns:
  57. Stream stream - the created stream, either as class instance (if available) or raw interface pointer
  58. */
  59. OpenStream(name, access)
  60. {
  61. local out
  62. this._Error(DllCall(NumGet(this.vt+04*A_PtrSize), "Ptr", this.ptr, "Str", name, "UInt", 0, "UInt", access, "UInt", 0, "Ptr*", out)) ; msdn: param #2/4 are reserved
  63. return IsObject(Stream) ? new Stream(out) : out
  64. }
  65. /*
  66. Method: CreateStorage
  67. creates and opens a new storage object nested within this storage object with the specified name in the specified access mode.
  68. Parameters:
  69. STR name - the name of the stream. The name can be used later to open or reopen the storage. The name must not exceed 31 characters in length.
  70. UINT access - Specifies the access mode to use when opening the newly created storage object. You may use the fields of the STGM class for convenience.
  71. Returns:
  72. Storage stg - the newly created storage, as class instance
  73. */
  74. CreateStorage(name, access)
  75. {
  76. local out
  77. this._Error(DllCall(NumGet(this.vt+05*A_PtrSize), "Ptr", this.ptr, "Str", name, "UInt", access, "UInt", 0, "UInt", 0, "Ptr*", out)) ; msdn: param #3/4 are reserved
  78. return new Storage(out)
  79. }
  80. /*
  81. Method: OpenStorage
  82. opens an existing storage object with the specified name in the specified access mode.
  83. Parameters:
  84. STR name - the name of the storage
  85. UINT access - Specifies the access mode to use when opening the storage object. You may use the fields of the STGM class for convenience.
  86. Returns:
  87. Storage stg - the opened storage, as class instance
  88. */
  89. OpenStorage(name, access)
  90. {
  91. local out
  92. this._Error(DllCall(NumGet(this.vt+06*A_PtrSize), "Ptr", this.ptr, "Str", name, "Ptr", 0, "UInt", access, "Ptr", 0, "UInt", 0, "Ptr*", out)) ; msdn: param #2/4 must be NULL, #5 is reserved
  93. return new Storage(out)
  94. }
  95. /*
  96. Method: CopyTo
  97. copies the entire contents of an open storage object to another storage object.
  98. Parameters:
  99. IID[] iidExclude - an array of IIDs to be excluded. This can be a memory pointer or an AHK array holding either memory pointers or GUID strings
  100. STR[] nameExclude - an array of strings to be excluded. This can either be a memory pointer (a pointer to an array of string pointers, which ends with a NULL pointer) or an AHK array of strings.
  101. Storage dest - the destination storage, either as class instance or raw interface pointer
  102. [opt] UINT iidExcludeCount - if iidExclude is a memory array, set this parameter to the number of elements in the array
  103. Returns:
  104. BOOL success - true on success, false otherwise
  105. */
  106. CopyTo(iidExclude, nameExclude, dest, iidExcludeCount := 0)
  107. {
  108. local mem1, mem2
  109. if IsObject(iidExclude)
  110. {
  111. iidExcludeCount := iidExcludeCount ? iidExcludeCount : iidExclude.maxIndex(), VarSetCapacity(mem1, iidExcludeCount * 16, 00)
  112. Loop iidExcludeCount
  113. {
  114. if CCFramework.isInteger(iidExclude[A_Index])
  115. CCFramework.CopyMemory(iidExclude[A_Index], &mem1 + (A_Index - 1) * 16, 16)
  116. else
  117. CCFramework.String2GUID(iidExclude[A_Index], &mem1 + (A_Index - 1) * 16)
  118. }
  119. iidExclude := &mem1
  120. }
  121. if IsObject(nameExclude)
  122. {
  123. VarSetCapacity(mem2, nameExclude.maxIndex() * A_PtrSize, 00)
  124. Loop nameExclude.maxIndex()
  125. {
  126. NumPut(nameExclude.GetAdress(A_Index), mem2, (A_Index - 1) * A_PtrSize, "Ptr")
  127. }
  128. nameExclude := &mem2
  129. }
  130. return this._Error(DllCall(NumGet(this.vt+07*A_PtrSize), "Ptr", this.ptr, "UInt", iidExcludeCount, "Ptr", iidExclude, "Ptr", nameExclude, "Ptr", IsObject(dest) ? dest.ptr : dest))
  131. }
  132. /*
  133. Method: MoveElementTo
  134. copies or moves a substorage or stream from this storage object to another storage object.
  135. Parameters:
  136. STR name - the name of the element in this storage object to be moved or copied
  137. Storage dest - the destination storage, either as class instance or raw interface pointer
  138. STR newName - the new name for the element in its new storage object
  139. UINT operation - Specifies whether the operation should be a move (STGMOVE.MOVE) or a copy (STGMOVE.COPY). You may use the fields of the STGMOVE class for convenience.
  140. Returns:
  141. BOOL success - true on success, false otherwise
  142. */
  143. MoveElementTo(name, dest, newName, operation)
  144. {
  145. return this._Error(DllCall(NumGet(this.vt+08*A_PtrSize), "Ptr", this.ptr, "Str", name, "Ptr", IsObject(dest) ? dest.ptr : dest, "Str", newName, "UInt", operation))
  146. }
  147. /*
  148. Method: Commit
  149. ensures that any changes made to a storage object open in transacted mode are reflected in the parent storage.
  150. Parameters:
  151. UINT flags - Controls how the changes are committed to the storage object. You may use the fields of the STGC class for convenience.
  152. Returns:
  153. BOOL success - true on success, false otherwise
  154. Remarks:
  155. For nonroot storage objects in direct mode, this method has no effect.
  156. For a root storage, it reflects the changes in the actual device; for example, a file on disk.
  157. For a root storage object opened in direct mode, always call the <Commit> method prior to Release.
  158. <Commit> flushes all memory buffers to the disk for a root storage in direct mode and will return an error code upon failure.
  159. Although Release also flushes memory buffers to disk, it has no capacity to return any error codes upon failure.
  160. Therefore, calling Release without first calling <Commit> causes indeterminate results.
  161. */
  162. Commit(flags)
  163. {
  164. return this._Error(DllCall(NumGet(this.vt+09*A_PtrSize), "Ptr", this.ptr, "UInt", flags))
  165. }
  166. /*
  167. Method: Revert
  168. discards all changes that have been made to the storage object since the last commit operation.
  169. Returns:
  170. BOOL success - true on success, false otherwise
  171. */
  172. Revert()
  173. {
  174. return this._Error(DllCall(NumGet(this.vt+10*A_PtrSize), "Ptr", this.ptr))
  175. }
  176. /*
  177. Method: EnumElements
  178. retrieves an enumerator object that can be used to enumerate the storage and stream objects contained within this storage object.
  179. Returns:
  180. EnumSTATSTG enumerator - the enumerator, either as class instance (if available) or raw interface pointer
  181. Remarks:
  182. The storage object must be open in read mode to allow the enumeration of its elements.
  183. */
  184. EnumElements()
  185. {
  186. local out
  187. this._Error(DllCall(NumGet(this.vt+11*A_PtrSize), "Ptr", this.ptr, "UInt", 0, "Ptr", 0, "UInt", 0, "Ptr*", out)) ; msdn: param #1-#3 are reserved
  188. return IsObject(EnumSTATSTG) ? new EnumSTATSTG(out) : out
  189. }
  190. /*
  191. Method: DestroyElement
  192. removes the specified storage or stream from this storage object.
  193. Parameters:
  194. STR name - the name of the storage or stream to be removed
  195. Returns:
  196. BOOL success - true on success, false otherwise
  197. */
  198. DestroyElement(name)
  199. {
  200. return this._Error(DllCall(NumGet(this.vt+12*A_PtrSize), "Ptr", this.ptr, "Str", name))
  201. }
  202. /*
  203. Method: RenameElement
  204. renames the specified substorage or stream in this storage object.
  205. Parameters:
  206. STR oldName - the name of the substorage or stream to be changed
  207. STR newName - the new name for the specified substorage or stream
  208. Returns:
  209. BOOL success - true on success, false otherwise
  210. */
  211. RenameElement(oldName, newName)
  212. {
  213. return this._Error(DllCall(NumGet(this.vt+13*A_PtrSize), "Ptr", this.ptr, "Str", oldName, "Str", newName))
  214. }
  215. /*
  216. Method: SetElementTimes
  217. sets the modification, access, and creation times of the specified storage element, if the underlying file system supports this method.
  218. Parameters:
  219. [opt] STR name - The name of the storage object element whose times are to be modified. If ommitted, the time is set on the root storage rather than one of its elements.
  220. [opt] FILETIME creationTime - Either the new creation time for the element or NULL if the creation time is not to be modified.
  221. [opt] FILETIME accessTime - Either the new access time for the element or NULL if the access time is not to be modified.
  222. [opt] FILETIME modTime - Either the new modification time for the element or NULL if the modification time is not to be modified.
  223. Returns:
  224. BOOL success - true on success, false otherwise
  225. */
  226. SetElementTimes(name := "", creationTime := 0, accessTime := 0, modTime := 0)
  227. {
  228. return this._Error(DllCall(NumGet(this.vt+14*A_PtrSize), "Ptr", this.ptr, "Ptr", name ? &name : 0, "Ptr", IsObject(creationTime) ? creationTime.ToStructPtr() : creationTime, "Ptr", IsObject(accessTime) ? accessTime.ToStructPtr() : accessTime, "Ptr", IsObject(modTime) ? modTime.ToStructPtr() : modTime))
  229. }
  230. /*
  231. Method: SetClass
  232. assigns the specified class identifier (CLSID) to this storage object.
  233. Parameters:
  234. CLSID clsid - the CLSID, either as string or raw memory pointer
  235. Returns:
  236. BOOL success - true on success, false otherwise
  237. */
  238. SetClass(clsid)
  239. {
  240. local mem
  241. if !CCFramework.isInteger(clsid)
  242. VarSetCapacity(mem, 16, 00), clsid := CCFramework.String2GUID(clsid, &mem)
  243. return this._Error(DllCall(NumGet(this.vt+15*A_PtrSize), "Ptr", this.ptr, "Ptr", clsid))
  244. }
  245. /*
  246. Method: SetStateBits
  247. stores up to 32 bits of state information in this storage object. *This method is reserved for future use.*
  248. Parameters:
  249. UINT state - Specifies the new values of the bits to set. No legal values are defined for these bits; they are all reserved for future use and must not be used by applications.
  250. UINT mask - A binary mask indicating which bits in state are significant in this call.
  251. Returns:
  252. BOOL success - true on success, false otherwise
  253. */
  254. SetStateBits(state, mask)
  255. {
  256. return this._Error(DllCall(NumGet(this.vt+16*A_PtrSize), "Ptr", this.ptr, "UInt", state, "UInt", mask))
  257. }
  258. /*
  259. Method: Stat
  260. retrieves the STATSTG structure for this open storage object.
  261. Parameters:
  262. [opt] UINT flags - Specifies that some of the members in the STATSTG structure are not returned, thus saving a memory allocation operation. You may use the fields of the STATFLAG class for convenience.
  263. Returns:
  264. STATSTG info - the retrieved infromation, either as class instance (if available) or raw memory pointer
  265. */
  266. Stat(flags := 0)
  267. {
  268. local out
  269. this._Error(DllCall(NumGet(this.vt+17*A_PtrSize), "Ptr", this.ptr, "Ptr*", out, "UInt", flags))
  270. return IsObject(STATSTG) ? new STATSTG(out) : out
  271. }
  272. }