Skitty Posted March 29, 2011 Share Posted March 29, 2011 (edited) All credit goes to Trancexx as all I did was rip her scripts heart out and ferociously pasted it into this udf for use as a single function. Here is the original at I spent a few hours looking for a way to retrieve binary data from file resources and I found it implemented in one of Trancexx's scripts.Func can pull nearly any data from any file with resources. and if you don't omit a destination file to be created, you get the files binary data instead! The resource extract udf is below. Edited March 31, 2011 by System238 Link to comment Share on other sites More sharing options...
Skitty Posted March 29, 2011 Author Share Posted March 29, 2011 expandcollapse popup$Newico = @DesktopDir & "\Resources\Icons\ICON_" For $I = 0 To 20000 $Binary = _ResExt(@SystemDir & "\shell32.dll",$i,14,0,$Newico & $i & ".ico") Next $Newbmp = @DesktopDir & "\Resources\BitMaps\BMP_" For $I = 0 To 20000 $Binary = _ResExt(@SystemDir & "\shell32.dll",$i,2,0,$Newbmp & $i & ".bmp") Next Func _ResExt($ResFile,$ResName,$ResType,$ResLang,$NewFile = "") Local $RT_CURSOR = 1, $RT_BITMAP = 2, $RT_ICON = 3, $RT_MENU = 4, $RT_DIALOG = 5, $RT_STRING = 6, $RT_FONTDIR = 7, _ $RT_FONT = 8, $RT_ACCELERATOR = 9, $RT_RCDATA = 10, $RT_MESSAGETABLE = 11, $RT_GROUP_CURSOR = 12, $RT_GROUP_ICON = 14, _ $RT_VERSION = 16, $RT_DLGINCLUDE = 17, $RT_PLUGPLAY = 19, $RT_VXD = 20, $RT_ANICURSOR = 21, $RT_ANIICON = 22, _ $RT_HTML = 23, $RT_MANIFEST = 24 Switch $ResType Case $RT_BITMAP Local $bBinary = _ResourceGetAsRaw($RT_BITMAP,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinary) & "]") DllStructSetData($tBinary, 1, $bBinary) Local $tBitmap = DllStructCreate("dword HeaderSize", DllStructGetPtr($tBinary)) Local $iHeaderSize = DllStructGetData($tBitmap, "HeaderSize") Local $iMultiplier Switch $iHeaderSize Case 40 $tBitmap = DllStructCreate("dword HeaderSize;" & _ "dword Width;" & _ "dword Height;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "dword CompressionMethod;" & _ "dword Size;" & _ "dword Hresolution;" & _ "dword Vresolution;" & _ "dword Colors;" & _ "dword ImportantColors", _ DllStructGetPtr($tBinary)) $iMultiplier = 4 Case 12 $tBitmap = DllStructCreate("dword HeaderSize;" & _ "ushort Width;" & _ "ushort Height;" & _ "ushort Planes;" & _ "ushort BitPerPixel", _ DllStructGetPtr($tBinary)) $iMultiplier = 3 Case Else Return SetError(22, 0, "") EndSwitch Local $iExponent = DllStructGetData($tBitmap, "BitPerPixel") Local $tDIB = DllStructCreate("align 2;byte Identifier[2];" & _ "dword BitmapSize;" & _ "short;" & _ "short;" & _ "dword BitmapOffset;" & _ "byte Body[" & BinaryLen($bBinary) & "]") DllStructSetData($tDIB, "Identifier", Binary("BM")) DllStructSetData($tDIB, "BitmapSize", BinaryLen($bBinary) + 14) Local $iRawBitmapSize = DllStructGetData($tBitmap, "Size") If $iRawBitmapSize Then DllStructSetData($tDIB, "BitmapOffset", BinaryLen($bBinary) - $iRawBitmapSize + 14) Else If $iExponent = 24 Then DllStructSetData($tDIB, "BitmapOffset", $iHeaderSize + 14) Else Local $iWidth = DllStructGetData($tBitmap, "Width") Local $iHeight = DllStructGetData($tBitmap, "Height") $iRawBitmapSize = 4 * Floor(($iWidth * $iExponent + 31) / 32) * $iHeight + 2 Local $iOffset_1 = BinaryLen($bBinary) - $iRawBitmapSize + 14 Local $iOffset_2 = 2 ^ $iExponent * $iMultiplier + $iHeaderSize + 14 If $iOffset_2 - $iOffset_1 - 2 <= 0 Then DllStructSetData($tDIB, "BitmapOffset", $iOffset_2) Else DllStructSetData($tDIB, "BitmapOffset", $iOffset_1) EndIf EndIf EndIf DllStructSetData($tDIB, "Body", $bBinary) Local $tBinaryBMP = DllStructCreate("byte[" & DllStructGetSize($tDIB) & "]", DllStructGetPtr($tDIB)) $Binary = DllStructGetData($tBinaryBMP, 1) Case $RT_ICON Local $bBinary = _ResourceGetAsRaw($RT_ICON,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinary) & "]") DllStructSetData($tBinary, 1, $bBinary) Local $tBitmap = DllStructCreate("dword HeaderSize", DllStructGetPtr($tBinary)) Local $iHeaderSize = DllStructGetData($tBitmap, "HeaderSize") Local $iWidth, $iHeight Local $iColors, $iPlanes, $iBitPerPixel Switch $iHeaderSize Case 40 $tBitmap = DllStructCreate("dword HeaderSize;" & _ "dword Width;" & _ "dword Height;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "dword CompressionMethod;" & _ "dword Size;" & _ "dword Hresolution;" & _ "dword Vresolution;" & _ "dword Colors;" & _ "dword ImportantColors;", _ DllStructGetPtr($tBinary)) $iWidth = DllStructGetData($tBitmap, "Width") $iHeight = DllStructGetData($tBitmap, "Height") $iColors = DllStructGetData($tBitmap, "Colors") $iPlanes = DllStructGetData($tBitmap, "Planes") $iBitPerPixel = DllStructGetData($tBitmap, "BitPerPixel") Case 12 $tBitmap = DllStructCreate("dword HeaderSize;" & _ "ushort Width;" & _ "ushort Height;" & _ "ushort Planes;" & _ "ushort BitPerPixel;", _ DllStructGetPtr($tBinary)) $iWidth = DllStructGetData($tBitmap, "Width") $iHeight = DllStructGetData($tBitmap, "Height") $iPlanes = DllStructGetData($tBitmap, "Planes") $iBitPerPixel = DllStructGetData($tBitmap, "BitPerPixel") Case Else Local $tPNG = DllStructCreate("ubyte; char PNG[3]", DllStructGetPtr($tBinary)) If Not (DllStructGetData($tPNG, 1) = 137 And DllStructGetData($tPNG, "PNG") == "PNG") Then Return SetError(0, 1, "") EndIf Local $iIHDROffset = StringInStr(BinaryToString($bBinary), "IHDR", 1) If $iIHDROffset Then Local $tBinaryIHDR = DllStructCreate("byte[13]") DllStructSetData($tBinaryIHDR, 1, BinaryMid($bBinary, $iIHDROffset + 4, 13)) Local $tIHDR = DllStructCreate("byte Width[4];" & _ "byte Height[4];" & _ "ubyte BitDepth;" & _ "ubyte ColorType;" & _ "ubyte CompressionMethod;" & _ "ubyte FilterMethod;" & _ "ubyte InterlaceMethod", _ DllStructGetPtr($tBinaryIHDR)) $iWidth = Dec(Hex(DllStructGetData($tIHDR, "Width"))) $iHeight = Dec(Hex(DllStructGetData($tIHDR, "Height"))) $iBitPerPixel = DllStructGetData($tIHDR, "BitDepth") $iPlanes = 1 EndIf EndSwitch Local $tIcon = DllStructCreate("align 2;ushort;" & _ "ushort Type;" & _ "ushort ImageCount;" & _ "ubyte Width;" & _ "ubyte Height;" & _ "ubyte Colors;" & _ "byte;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "dword BitmapSize;" & _ "dword BitmapOffset;" & _ "byte Body[" & DllStructGetSize($tBinary) & "]") Local $iWidth = DllStructGetData($tBitmap, "Width") DllStructSetData($tIcon, "Type", 1) DllStructSetData($tIcon, "ImageCount", 1) DllStructSetData($tIcon, "Width", $iWidth) DllStructSetData($tIcon, "Height", $iHeight) DllStructSetData($tIcon, "Colors", $iColors) DllStructSetData($tIcon, "Planes", $iPlanes) DllStructSetData($tIcon, "BitPerPixel", $iBitPerPixel) DllStructSetData($tIcon, "BitmapSize", DllStructGetSize($tBinary)) DllStructSetData($tIcon, "BitmapOffset", 22) DllStructSetData($tIcon, "Body", DllStructGetData($tBinary, 1)) Local $tBinaryIcon = DllStructCreate("byte[" & DllStructGetSize($tIcon) & "]", DllStructGetPtr($tIcon)) $Binary = DllStructGetData($tBinaryIcon, 1) Case $RT_RCDATA Local $bBinary = _ResourceGetAsRaw($RT_RCDATA,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf $Binary = $bBinary Case $RT_GROUP_CURSOR Local $bBinary = _ResourceGetAsRaw($RT_GROUP_CURSOR,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinary) & "]") DllStructSetData($tBinary, 1, $bBinary) Local $tCursorGroup = DllStructCreate("ushort;" & _ "ushort Type;" & _ "ushort ImageCount;" & _ "ushort Width;" & _ "ushort Height;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "ushort;" & _ "ushort;" & _ "ushort OrdinalName", _ DllStructGetPtr($tBinary)) Local $iOrdinalName = DllStructGetData($tCursorGroup, "OrdinalName") $bBinary = _ResourceGetAsRaw($RT_CURSOR, $iOrdinalName, $ResLang, $ResFile, 1) If @error Then Return SetError(3, 0, "") EndIf Local $tBinaryRTCursor = DllStructCreate("byte[" & BinaryLen($bBinary) & "]") DllStructSetData($tBinaryRTCursor, 1, $bBinary) Local $tRTCursor = DllStructCreate("ushort Xhotspot;" & _ "ushort Yhotspot;" & _ "byte Body[" & DllStructGetSize($tBinaryRTCursor) - 4 & "]", _ DllStructGetPtr($tBinaryRTCursor)) Local $tCursor = DllStructCreate("align 2;ushort;" & _ "ushort Type;" & _ "ushort ImageCount;" & _ "ubyte Width;" & _ "ubyte Height;" & _ "ubyte Colors;" & _ "ubyte;" & _ "ushort Xhotspot;" & _ "ushort Yhotspot;" & _ "dword BitmapSize;" & _ "dword BitmapOffset;" & _ "byte Body[" & DllStructGetSize($tBinaryRTCursor) - 4 & "]") Local $iWidth = DllStructGetData($tCursorGroup, "Width") If $iWidth > 32 Then $iWidth = 32 EndIf Local $iHeight = $iWidth DllStructSetData($tCursor, "Type", 2) DllStructSetData($tCursor, "ImageCount", 1) DllStructSetData($tCursor, "Width", $iWidth) DllStructSetData($tCursor, "Height", $iHeight) DllStructSetData($tCursor, "Xhotspot", DllStructGetData($tRTCursor, "Xhotspot")) DllStructSetData($tCursor, "Yhotspot", DllStructGetData($tRTCursor, "Yhotspot")) DllStructSetData($tCursor, "BitmapSize", DllStructGetSize($tRTCursor) - 4) DllStructSetData($tCursor, "BitmapOffset", 22) DllStructSetData($tCursor, "Body", DllStructGetData($tRTCursor, "Body")) Local $tBinaryCursor = DllStructCreate("byte[" & DllStructGetSize($tCursor) & "]", DllStructGetPtr($tCursor)) $Binary = DllStructGetData($tBinaryCursor, 1) Case $RT_GROUP_ICON Local $aIconData = _CrackIcon($ResName, $ResLang, $ResFile,$RT_GROUP_ICON) If @error Then Return SetError(2, 0, "") EndIf Local $iIconCount = UBound($aIconData) Local $tIconHeader = DllStructCreate("ushort;" & _ "ushort Type;" & _ "ushort ImageCount") DllStructSetData($tIconHeader, "Type", 1) DllStructSetData($tIconHeader, "ImageCount", $iIconCount) Local $tBinaryIconHeader = DllStructCreate("byte[6]", DllStructGetPtr($tIconHeader)) Local $bBinaryIconHeader = DllStructGetData($tBinaryIconHeader, 1) Local $aIconSizes[$iIconCount] Local $bBinaryBody Local $bBinaryChunk For $i = 0 To $iIconCount - 1 $bBinaryChunk = _ResourceGetAsRaw($RT_ICON, $aIconData[$i][6], $ResLang, $ResFile, 1) $aIconSizes[$i] = BinaryLen($bBinaryChunk) $bBinaryBody = _BinaryConcat($bBinaryBody, $bBinaryChunk) Next Local $tDirectory = DllStructCreate("byte[" & 16 * $iIconCount & "]") Local $tDirectoryChunk Local $tBinaryDirectoryChunk Local $bBinaryDirectoryChunk Local $bBinaryDirectory Local $iOffset = 6 + 16 * $iIconCount For $i = 0 To $iIconCount - 1 $tDirectoryChunk = DllStructCreate("ubyte Width;" & _ "ubyte Height;" & _ "ubyte Colors;" & _ "ubyte;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "dword BitmapSize;" & _ "dword Offset") DllStructSetData($tDirectoryChunk, "Width", $aIconData[$i][0]) DllStructSetData($tDirectoryChunk, "Height", $aIconData[$i][1]) DllStructSetData($tDirectoryChunk, "Colors", $aIconData[$i][2]) DllStructSetData($tDirectoryChunk, "Planes", $aIconData[$i][3]) DllStructSetData($tDirectoryChunk, "BitPerPixel", $aIconData[$i][4]) DllStructSetData($tDirectoryChunk, "BitmapSize", $aIconData[$i][5]) DllStructSetData($tDirectoryChunk, "Offset", $iOffset) $iOffset += $aIconSizes[$i] $tBinaryDirectoryChunk = DllStructCreate("byte[16]", DllStructGetPtr($tDirectoryChunk)) $bBinaryDirectoryChunk = DllStructGetData($tBinaryDirectoryChunk, 1) $bBinaryDirectory = _BinaryConcat($bBinaryDirectory, $bBinaryDirectoryChunk) Next $Binary = _BinaryConcat($bBinaryIconHeader, $bBinaryDirectory) $Binary = _BinaryConcat($Binary, $bBinaryBody) Case $RT_ANICURSOR Local $bBinary = _ResourceGetAsRaw($RT_ANICURSOR,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf $Binary = $bBinary Case $RT_ANIICON Local $bBinary = _ResourceGetAsRaw($RT_ANIICON,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf $Binary = $bBinary Case $RT_HTML, $RT_MANIFEST Local $sData = _ResourceGetAsRaw($ResType,$ResName, $ResLang, $ResFile, 1) If @error Then Return SetError(2, 0, "") EndIf $Binary = $sData EndSwitch If $NewFile = "" Then Return $Binary Else $CR = FileOpen($NewFile,10) FileWrite($CR,$Binary) FileClose($CR) EndIf EndFunc Func _ResourceGetAsRaw($iResType, $iResName, $iResLang, $sModule, $iMode = 0, $iSize = 0) Local $iLoaded Local $a_hCall = DllCall("kernel32.dll", "hwnd", "GetModuleHandleW", "wstr", $sModule) If @error Then Return SetError(1, 0, "") EndIf If Not $a_hCall[0] Then $a_hCall = DllCall("kernel32.dll", "hwnd", "LoadLibraryExW", "wstr", $sModule, "hwnd", 0, "int", 34) If @error Or Not $a_hCall[0] Then Return SetError(2, 0, "") EndIf $iLoaded = 1 EndIf Local $hModule = $a_hCall[0] Switch IsNumber($iResType) + 2 * IsNumber($iResName) Case 0 $a_hCall = DllCall("kernel32.dll", "hwnd", "FindResourceExW", _ "hwnd", $hModule, _ "wstr", $iResType, _ "wstr", $iResName, _ "int", $iResLang) Case 1 $a_hCall = DllCall("kernel32.dll", "hwnd", "FindResourceExW", _ "hwnd", $hModule, _ "int", $iResType, _ "wstr", $iResName, _ "int", $iResLang) Case 2 $a_hCall = DllCall("kernel32.dll", "hwnd", "FindResourceExW", _ "hwnd", $hModule, _ "wstr", $iResType, _ "int", $iResName, _ "int", $iResLang) Case 3 $a_hCall = DllCall("kernel32.dll", "hwnd", "FindResourceExW", _ "hwnd", $hModule, _ "int", $iResType, _ "int", $iResName, _ "int", $iResLang) EndSwitch If @error Or Not $a_hCall[0] Then If $iLoaded Then Local $a_iCall = DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $hModule) If @error Or Not $a_iCall[0] Then Return SetError(7, 0, "") EndIf EndIf Return SetError(3, 0, "") EndIf Local $hResource = $a_hCall[0] Local $a_iCall = DllCall("kernel32.dll", "int", "SizeofResource", "hwnd", $hModule, "hwnd", $hResource) If @error Or Not $a_iCall[0] Then If $iLoaded Then Local $a_iCall = DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $hModule) If @error Or Not $a_iCall[0] Then Return SetError(7, 0, "") EndIf EndIf Return SetError(4, 0, "") EndIf Local $iSizeOfResource = $a_iCall[0] $a_hCall = DllCall("kernel32.dll", "hwnd", "LoadResource", "hwnd", $hModule, "hwnd", $hResource) If @error Or Not $a_hCall[0] Then If $iLoaded Then Local $a_iCall = DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $hModule) If @error Or Not $a_iCall[0] Then Return SetError(7, 0, "") EndIf EndIf Return SetError(5, 0, "") EndIf Local $a_pCall = DllCall("kernel32.dll", "ptr", "LockResource", "hwnd", $a_hCall[0]) If @error Or Not $a_pCall[0] Then If $iLoaded Then Local $a_iCall = DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $hModule) If @error Or Not $a_iCall[0] Then Return SetError(7, 0, "") EndIf EndIf Return SetError(6, 0, "") EndIf Local $tOut Switch $iMode Case 0 $tOut = DllStructCreate("char[" & $iSizeOfResource + 1 & "]", $a_pCall[0]) Case 1 $tOut = DllStructCreate("byte[" & $iSizeOfResource & "]", $a_pCall[0]) EndSwitch Local $sReturnData = DllStructGetData($tOut, 1) If $iLoaded Then Local $a_iCall = DllCall("kernel32.dll", "int", "FreeLibrary", "hwnd", $hModule) If @error Or Not $a_iCall[0] Then Return SetError(7, 0, "") EndIf EndIf Switch $iSize Case 0 Return SetError(0, 0, $sReturnData) Case Else Switch $iMode Case 0 Return SetError(0, 0, StringLeft($sReturnData, $iSize)) Case 1 Return SetError(0, 0, BinaryMid($sReturnData, 1, $iSize)) EndSwitch EndSwitch EndFunc ;==>_ResourceGetAsRaw Func _CrackIcon($iIconName, $iResLang, $sModule,$Type) Local $bBinary = _ResourceGetAsRaw($Type, $iIconName, $iResLang, $sModule, 1) If @error Then Return SetError(@error + 3, 0, "") EndIf Local $tBinary = DllStructCreate("byte[" & BinaryLen($bBinary) & "]") DllStructSetData($tBinary, 1, $bBinary) Local $tResource = DllStructCreate("ushort;" & _ "ushort Type;" & _ "ushort ImageCount;" & _ "byte Body[" & DllStructGetSize($tBinary) - 6 & "]", _ DllStructGetPtr($tBinary)) Local $iIconCount = DllStructGetData($tResource, "ImageCount") If Not $iIconCount Or $iIconCount > 50 Then ; this likely indicates usage of exe compressor Return SetError(0, 1, "") EndIf Local $iWidth, $iHeight Local $aIconsData[$iIconCount][7] Local $tGroupIconData For $i = 0 To $iIconCount - 1 $tGroupIconData = DllStructCreate("ubyte Width;" & _ "ubyte Height;" & _ "ubyte Colors;" & _ "ubyte;" & _ "ushort Planes;" & _ "ushort BitPerPixel;" & _ "dword BitmapSize;" & _ "ushort OrdinalName;", _ DllStructGetPtr($tResource, "Body") + $i * 14) $iWidth = DllStructGetData($tGroupIconData, "Width") If Not $iWidth Then $iWidth = 256 EndIf $iHeight = DllStructGetData($tGroupIconData, "Height") If Not $iHeight Then $iHeight = 256 EndIf $aIconsData[$i][0] = $iWidth $aIconsData[$i][1] = $iHeight $aIconsData[$i][2] = DllStructGetData($tGroupIconData, "Colors") $aIconsData[$i][3] = DllStructGetData($tGroupIconData, "Planes") $aIconsData[$i][4] = DllStructGetData($tGroupIconData, "BitPerPixel") $aIconsData[$i][5] = DllStructGetData($tGroupIconData, "BitmapSize") $aIconsData[$i][6] = DllStructGetData($tGroupIconData, "OrdinalName") Next Return SetError(0, 0, $aIconsData) EndFunc ;==>_CrackIcon Func _BinaryConcat($bBinary1, $bBinary2) If Not IsBinary($bBinary1) Then Return $bBinary2 EndIf Local $tInitial = DllStructCreate("byte[" & BinaryLen($bBinary1) & "];byte[" & BinaryLen($bBinary2) & "]") DllStructSetData($tInitial, 1, $bBinary1) DllStructSetData($tInitial, 2, $bBinary2) Local $tOutput = DllStructCreate("byte[" & DllStructGetSize($tInitial) & "]", DllStructGetPtr($tInitial)) Return SetError(0, 0, DllStructGetData($tOutput, 1)) EndFunc ;==>_BinaryConcat Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now