Resource.au3


SUBMITTED BY: Guest

DATE: May 28, 2013, 6:19 a.m.

FORMAT: Text only

SIZE: 9.6 kB

HITS: 2106

  1. #include <Constants.au3>
  2. #include <WinAPI.au3>
  3. #include <Memory.au3>
  4. Global Const $RT_ACCELERATOR = 9
  5. Global Const $RT_ANICURSOR = 21
  6. Global Const $RT_ANIICON = 22
  7. Global Const $RT_BITMAP = 2
  8. Global Const $RT_CURSOR = 1
  9. Global Const $RT_DIALOG = 5
  10. Global Const $RT_DLGINCLUDE = 17
  11. Global Const $RT_FONT = 8
  12. Global Const $RT_FONTDIR = 7
  13. Global Const $RT_GROUP_CURSOR = ($RT_CURSOR + 11)
  14. Global Const $RT_ICON = 3
  15. Global Const $RT_GROUP_ICON = ($RT_ICON + 11)
  16. Global Const $RT_HTML = 23
  17. Global Const $RT_MANIFEST = 24
  18. Global Const $RT_MENU = 4
  19. Global Const $RT_MESSAGETABLE = 11
  20. Global Const $RT_PLUGPLAY = 19
  21. Global Const $RT_RCDATA = 10
  22. Global Const $RT_STRING = 6
  23. Global Const $RT_VERSION = 16
  24. Global Const $RT_VXD = 20
  25. Func ResGet($ResType,$ResName,$ResLanguage = -1,$ModuleName = "")
  26. Local $DataType1 = "LONG",$DataType2 = "LONG"
  27. Local $LPVOID,$HRSRC,$ResSize,$HGLOBAL
  28. if IsString($ResName) Then
  29. $ResName = StringUpper($ResName)
  30. $DataType1 = "WSTR"
  31. EndIf
  32. If IsString($ResType) Then
  33. $ResType = StringUpper($ResType)
  34. $DataType2 = "WSTR"
  35. EndIf
  36. if StringLen($ModuleName) Then
  37. $HModule = _WinAPI_LoadLibraryEx($ModuleName,$LOAD_LIBRARY_AS_DATAFILE)
  38. if $HModule = 0 Then Return SetError(1,0,0)
  39. Else
  40. $HModule = 0
  41. EndIf
  42. if ($ResLanguage <> -1) Then
  43. $HRSRC = DllCall("Kernel32.dll","ptr","FindResourceExW","ptr",$HModule, _
  44. $DataType2,$ResType,$DataType1,$ResName,"WORD",$ResLanguage)
  45. if @error Or $HRSRC[0] = 0 Then Return SetError(2,0,0)
  46. Else
  47. $HRSRC = DllCall("Kernel32.dll","ptr","FindResourceW","ptr",$HModule, _
  48. $DataType1,$ResName,$DataType2,$ResType)
  49. if @error Or $HRSRC[0] = 0 Then Return SetError(2,0,0)
  50. EndIf
  51. $HRSRC = $HRSRC[0]
  52. $ResSize = DllCall("Kernel32.dll","DWORD","SizeofResource","ptr",$HModule,"ptr",$HRSRC)
  53. if (@error Or $ResSize[0] = 0) Then Return SetError(3,0,0)
  54. $ResSize = $ResSize[0]
  55. $HGLOBAL = DllCall("Kernel32.dll","ptr","LoadResource","ptr",$HModule,"ptr",$HRSRC)
  56. if @error Or $HGLOBAL[0] = 0 Then Return SetError(4,0,0)
  57. $HGLOBAL = $HGLOBAL[0]
  58. $LPVOID = DllCall("Kernel32.dll","ptr","LockResource","ptr",$HGLOBAL)
  59. if @error Or $LPVOID[0] = 0 Then
  60. $BOOL = DllCall("Kernel32.dll","BOOL","FreeResource","ptr",$HGLOBAL)
  61. Return SetError(5,0,0)
  62. EndIf
  63. $LPVOID = $LPVOID[0]
  64. $ByteStruct = DllStructCreate("BYTE[" & $ResSize & "]")
  65. _MemMoveMemory($LPVOID,DllStructGetPtr($ByteStruct),$ResSize)
  66. $BOOL = DllCall("Kernel32.dll","BOOL","FreeResource","ptr",$HGLOBAL)
  67. if ($HModule) Then _WinAPI_FreeLibrary($HModule)
  68. Return SetError(0,$ResSize,$ByteStruct)
  69. EndFunc
  70. Func ResToFile($FileName,$ResType,$ResName,$ResLanguage = -1,$ModuleName = "",$Mode = 10)
  71. $ByteStruct = ResGet($ResType,$ResName,$ResLanguage,$ModuleName)
  72. if @error Then Return SetError(1,0,False)
  73. $OpeFile = FileOpen($FileName,$Mode)
  74. if @error Then Return SetError(2,0,False)
  75. FileWrite($OpeFile,Binary(DllStructGetData($ByteStruct,1)))
  76. if @error Then Return SetError(3,0,False)
  77. FileClose($OpeFile)
  78. Return SetError(0,0,True)
  79. EndFunc
  80. Func StringResGet($ResType,$ResName,$ResLanguage = -1,$Unicode = False,$ModuleName = "")
  81. Local $DtatType = "CHAR"
  82. $ByteStruct = ResGet($ResType,$ResName,$ResLanguage,$ModuleName)
  83. if @error Then Return SetError(1,0,0)
  84. $ResSize = @extended
  85. $LPVOID = DllStructGetPtr($ByteStruct)
  86. if ($Unicode) Then
  87. $DtatType = "WCHAR"
  88. $ResSize = Int($ResSize/2)
  89. EndIf
  90. $StrStruct = DllStructCreate($DtatType & "[" & $ResSize & "]",$LPVOID)
  91. if @error Then Return SetError(2,0,0)
  92. $nString = DllStructGetData($StrStruct,1)
  93. Return SetError(0,StringLen($nString),$nString)
  94. EndFunc
  95. Func ResGetImage($ResType,$ResName,$ResLanguage = -1,$ModuleName = "")
  96. Switch $ResType
  97. Case $RT_BITMAP
  98. $hImage = LoadBitmap($ResName,$ModuleName)
  99. if @error Then Return SetError(1,0,0)
  100. Return SetError(0,1,$hImage)
  101. Case $RT_ICON
  102. $hImage = LoadIcon($ResName,$ModuleName)
  103. if @error Then Return SetError(2,0,0)
  104. Return SetError(0,3,$hImage)
  105. Case $RT_CURSOR
  106. $hImage = LoadCursor($ResName,$ModuleName)
  107. if @error Then Return SetError(3,0,0)
  108. Return SetError(0,5,$hImage)
  109. Case Else
  110. $ByteStruct = ResGet($ResType,$ResName,$ResLanguage,$ModuleName)
  111. if @error Then Return SetError(4,0,0)
  112. $ResSize = @extended
  113. $hMemory = _MemGlobalAlloc($ResSize,$GHND)
  114. if Not($hMemory) Then Return SetError(5,0,0)
  115. $hLock = _MemGlobalLock($hMemory)
  116. if Not($hLock) Then
  117. _MemGlobalFree($hMemory)
  118. Return SetError(6,0,0)
  119. EndIf
  120. _MemMoveMemory(DllStructGetPtr($ByteStruct),$hLock,$ResSize)
  121. $lpstream = CreateStreamOnHGlobal($hLock,True)
  122. if @error Then
  123. _MemGlobalFree($hMemory)
  124. Return SetError(7,0,0)
  125. EndIf
  126. $riid = _WinAPI_GUIDFromString("{7BF80981-BF32-101A-8BBB-00AA00300CAB}")
  127. if @error Then
  128. _MemGlobalFree($hMemory)
  129. Return SetError(8,0,0)
  130. EndIf
  131. $HRESULT = DllCall("OleAut32.dll","LONG","OleLoadPicture","PTR",$lpstream,"LONG", _
  132. $ResSize,"BOOL",True,"ptr",DllStructGetPtr($riid),"idispatch*",0)
  133. if @error Or $HRESULT[0] <> 0 Then
  134. _MemGlobalFree($hMemory)
  135. Return SetError(9,0,0)
  136. EndIf
  137. $iPicture = $HRESULT[5]
  138. _MemGlobalFree($hMemory)
  139. Return SetError(0,Int($iPicture.Type()),Ptr($iPicture.Handle()))
  140. EndSwitch
  141. ;PICTYPE_UNINITIALIZED (-1)
  142. ;The picture object is currently uninitialized. This value is only valid as a return value
  143. ;from IPicture::get_Type and is not valid with the PICTDESC structure.
  144. ;PICTYPE_NONE = 0
  145. ;A new picture object is to be created without an initialized state. This
  146. ;value is valid only in the PICTDESC structure.
  147. ;PICTYPE_BITMAP = 1
  148. ;The picture type is a bitmap. When this value occurs in the PICTDESC structure,
  149. ;it means that the bmp field of that structure contains the relevant initialization parameters.
  150. ;PICTYPE_METAFILE = 2
  151. ;The picture type is a metafile. When this value occurs in the PICTDESC structure,
  152. ;it means that the wmf field of that structure contains the relevant initialization parameters.
  153. ;PICTYPE_ICON = 3
  154. ;The picture type is an icon. When this value occurs in the PICTDESC structure,
  155. ;it means that the icon field of that structure contains the relevant initialization parameters.
  156. ;PICTYPE_ENHMETAFILE = 4
  157. ;The picture type is an enhanced metafile. When this value occurs in the PICTDESC structure,
  158. ;it means that the emf field of that structure contains the relevant initialization parameters.
  159. EndFunc
  160. Func LoadBitmap($lpBitmapName,$ModuleName = "")
  161. Local $DataType = "LONG" , $TestModule = StringLen($ModuleName) <> 0
  162. if ($TestModule) Then
  163. $HModule = _WinAPI_LoadLibraryEx($ModuleName,$LOAD_LIBRARY_AS_DATAFILE)
  164. if $HModule = 0 Then Return SetError(1,0,0)
  165. Else
  166. $HModule = _WinAPI_GetModuleHandle(0)
  167. EndIf
  168. if IsString($lpBitmapName) Then
  169. $lpBitmapName = StringUpper($lpBitmapName)
  170. $DataType = "WSTR"
  171. EndIf
  172. $HBITMAP = DllCall("User32.dll","PTR","LoadBitmapW","PTR",$HModule,$DataType,$lpBitmapName)
  173. if @error Or $HBITMAP[0] = 0 Then
  174. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  175. Return SetError(2,0,0)
  176. EndIf
  177. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  178. Return SetError(0,0,$HBITMAP[0])
  179. EndFunc
  180. Func LoadIcon($lpIconName,$ModuleName = "")
  181. Local $DataType = "LONG" , $TestModule = StringLen($ModuleName) <> 0
  182. if ($TestModule) Then
  183. $HModule = _WinAPI_LoadLibraryEx($ModuleName,$LOAD_LIBRARY_AS_DATAFILE)
  184. if $HModule = 0 Then Return SetError(1,0,0)
  185. Else
  186. $HModule = _WinAPI_GetModuleHandle(0)
  187. EndIf
  188. if IsString($lpIconName) Then
  189. $lpIconName = StringUpper($lpIconName)
  190. $DataType = "WSTR"
  191. EndIf
  192. $HICON = DllCall("User32.dll","PTR","LoadIconW","PTR",$HModule,$DataType,$lpIconName)
  193. if @error Or $HICON[0] = 0 Then
  194. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  195. Return SetError(2,0,0)
  196. EndIf
  197. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  198. Return SetError(0,0,$HICON[0])
  199. EndFunc
  200. Func LoadCursor($lpCursorName,$ModuleName = "")
  201. Local $DataType = "LONG" , $TestModule = StringLen($ModuleName) <> 0
  202. if ($TestModule) Then
  203. $HModule = _WinAPI_LoadLibraryEx($ModuleName,$LOAD_LIBRARY_AS_DATAFILE)
  204. if $HModule = 0 Then Return SetError(1,0,0)
  205. Else
  206. $HModule = _WinAPI_GetModuleHandle(0)
  207. EndIf
  208. if IsString($lpCursorName) Then
  209. $lpCursorName = StringUpper($lpCursorName)
  210. $DataType = "WSTR"
  211. EndIf
  212. $HCURSOR= DllCall("User32.dll","PTR","LoadCursorW","PTR",$HModule,$DataType,$lpCursorName)
  213. if @error Or $HCURSOR[0] = 0 Then
  214. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  215. Return SetError(2,0,0)
  216. EndIf
  217. if ($TestModule) Then _WinAPI_FreeLibrary($HModule)
  218. Return SetError(0,0,$HCURSOR[0])
  219. EndFunc
  220. Func CreateStreamOnHGlobal($hGlobal,$fDeleteOnRelease)
  221. $WINOLE = DllCall("Ole32.dll","PTR","CreateStreamOnHGlobal","ptr", _
  222. $hGlobal,"BOOL",$fDeleteOnRelease,"PTR*",0)
  223. if @error Or $WINOLE[0] <> 0 Then Return SetError(1,0,0)
  224. Return SetError(0,0,$WINOLE[3])
  225. EndFunc

comments powered by Disqus