wolf9228 Posted June 30, 2009 Posted June 30, 2009 AutoIt_DLLResource.AU3 [autoit]#Include <WinAPI.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #Include "DllFile.au3" Opt('MustDeclareVars', 1) Const $Style_Stretch = 1 , $Style_Tile = 2 , $Style_Center = 3 , $Style_Stretch_Center = 4 , $Style_Stretch_Tile = 5 Global $HICON_WM_SETCURSOR , $StrResName , $WM_ERASEBKGND_ARRAY[1][9] Func UpdateFreeResource($ExeFile_DllFile , $InpResFile , $RsName , $RsType ) Local $nBytes ,$tSize , $hFile, $result ,$ResArray[3][3] ,$paramtype1 , $paramtype2 , $result,$rh , $DATA $ResArray[0][1] = "ResourceName" $ResArray[0][2] = "ResourceType" $ResArray[1][0] = "Name" $ResArray[2][0] = "Type" $hFile = _WinAPI_CreateFile($InpResFile, 2,2) if Not $hFile Then Return -1 $tSize = FileGetSize($InpResFile) + 1 $DATA = DllStructCreate("byte[" & $tSize & "]") _WinAPI_ReadFile($hFile, DllStructGetPtr($DATA), $tSize, $nBytes) if @error Then _WinAPI_CloseHandle($hFile) return -2 EndIf _WinAPI_CloseHandle($hFile) if IsString($RsType) Then $paramtype1 = "str" $RsType = StringUpper($RsType) ELSE $paramtype1 = "long" $RsType = Int($RsType) EndIf if IsString($RsName) Then $paramtype2 = "str" $RsName = StringUpper($RsName) ELSE $paramtype2 = "long" $RsName = Int($RsName) EndIf $result = DllCall("kernel32.dll", "hwnd", "BeginUpdateResource", "str", $ExeFile_DllFile, "int", 0) if Not @error Then $rh = $result[0] if $rh = 0 Then return -3 Else return -3 EndIf $result = DllCall("kernel32.dll", "int", "UpdateResource", "hwnd", $rh, $paramtype1 , $RsType, $paramtype2, $RsName, "short", 0, "ptr", _ DllStructGetPtr($DATA), "long" , DllStructGetSize($DATA)) if Not @error Then if $result[0] = 0 Then return -4 Else return -4 EndIf $result = DllCall("kernel32.dll", "int", "EndUpdateResource", "hwnd", $rh, "int", 0) if Not @error Then if $result[0] = 0 Then return -5 Else return -5 EndIf $ResArray[0][0] = 2 $ResArray[1][1] = $RsName $ResArray[1][2] = $RsType $ResArray[2][1] = VarGetType($RsName) $ResArray[2][2] = VarGetType($RsType) return $ResArray EndFunc Func CreateCursorFromFreeResource($ExeFile_DllFile , $RsName ,$RsType) Local $paramtype1 , $paramtype2 , $HICON , $fIcon , $dwVer , $DllCall Local $LoadLibrary = _WinAPI_LoadLibrary($ExeFile_DllFile) if @error Then Return -1 if IsString($RsType) Then $paramtype1 = "str" $RsType = StringUpper($RsType) ELSE $paramtype1 = "long" $RsType = Int($RsType) EndIf if IsString($RsName) Then $paramtype2 = "str" $RsName = StringUpper($RsName) ELSE $paramtype2 = "long" $RsName = Int($RsName) EndIf Local $FindResource = DllCall("kernel32.dll", "int", "FindResourceA", "int", $LoadLibrary, $paramtype2, $RsName, $paramtype1, $RsType) if @error Then Return -2 Else $FindResource = $FindResource[0] if $FindResource = 0 Then Return -2 EndIf Local $SizeofResource = DllCall("kernel32.dll", "int", "SizeofResource", "int",$LoadLibrary, "int", $FindResource) if @error Then Return -3 Else $SizeofResource = $SizeofResource[0] if $SizeofResource = 0 Then Return -3 EndIf Local $LoadResource = DllCall("kernel32.dll", "ptr", "LoadResource", "int",$LoadLibrary, "int", $FindResource) if @error Then Return -4 Else $LoadResource = $LoadResource[0] if $LoadResource = 0 Then Return -4 EndIf Local $byteStructCreate = DllStructCreate("byte[" & $SizeofResource & "]", $LoadResource) Local $presbits = DllStructGetPtr($byteStructCreate) Local $dwResSize = $SizeofResource Local $fIcon = False , $dwVer = 0x00030000 $DllCall = DllCall("user32.dll","hwnd","CreateIconFromResource","ptr",$presbits,"int",$dwResSize,"int",$fIcon,"long",$dwVer) if @error Then Return -5 Else Local $HICON = $DllCall[0] If $HICON = 0 Then Return -5 EndIf _WinAPI_FreeLibrary($LoadLibrary) Return $HICON EndFunc Func WM_SETCURSOR($hWnd, $Msg, $wParam, $lParam) if ($HICON_WM_SETCURSOR) Then _ _WinAPI_SetCursor($HICON_WM_SETCURSOR) Return True EndFunc Func GUI_REGISTER_MSG_WM_SETCURSOR($ExeFile_DllFile , $RsName, $RsType) $HICON_WM_SETCURSOR = CreateCursorFromFreeResource($ExeFile_DllFile , $RsName ,$RsType) If Not ($HICON_WM_SETCURSOR) Then Return -1 GUIRegisterMsg($WM_SETCURSOR, "WM_SETCURSOR") Return True EndFunc Func UpdateIconResource($ExeFile_DllFile,$InpResFile) Local $nBytes ,$tSize ,$bitmap , $RT_ICON = 3 , $RT_ICON_NAME = "",$RT_GROUP_ICON = 14 ,$RT_GROUP_ICON_NAME = "" , _ $ARRAY_RT_GROUP_ICON_NAME ,$ARRAY_RT_ICON_NAME, $hFile,$DllCall ,$ResArray[1][3], $IconResBase1 , $IconResBase2 $ResArray[0][1] = "ResourceName" $ResArray[0][2] = "ResourceType" $ResArray[0][0] = 0 $tSize = FileGetSize($InpResFile) - 6 Local $typedef_struct_ICONDIR = _ "short idReserved;" & _ "short idType;" & _ "short idCount;" & _ "byte idEntries[" & $tSize + 1 & "]" Local $typedef_struct_ICONDIRENTRY = _ "byte bWidth;" & _ "byte bHeight;" & _ "byte bColorCount;" & _ "byte bReserved;" & _ "Short wPlanes;" & _ "Short wBitCount;" & _ "dword dwbytesInRes;" & _ "dword dwImageOffset;" Local $ICONDIRENTRYSIZE = DllStructGetSize(DllStructCreate($typedef_struct_ICONDIRENTRY)) Local $typedef_struct_GRPICONDIRENTRY = _ "byte bWidth;" & _ "byte bHeight;" & _ "byte bColorCount;" & _ "byte bReserved;" & _ "Short wPlanes;" & _ "Short wBitCount;" & _ "dword dwbytesInRes;" & _ "byte nID;" Local $GRPICONDIRENTRYSIZE = DllStructGetSize(DllStructCreate($typedef_struct_GRPICONDIRENTRY)) Local $hModule = _WinAPI_LoadLibrary($ExeFile_DllFile) if Not ($hModule) Then Return -1 $RT_ICON_NAME = EnumResourceNames($hModule,$RT_ICON) $RT_GROUP_ICON_NAME = EnumResourceNames($hModule,$RT_GROUP_ICON) _WinAPI_FreeLibrary($hModule) $ARRAY_RT_ICON_NAME = StringSplit($RT_ICON_NAME,"|") $ARRAY_RT_GROUP_ICON_NAME = StringSplit($RT_ICON_NAME,"|") Dim $A = 0 , $B = 0 , $C While 1 $A += 1 $C = True For $B = 1 To $ARRAY_RT_ICON_NAME[0] Step 1 If String($A) = String($ARRAY_RT_ICON_NAME[$B]) Then $C = False ExitLoop EndIf Next If $C = True Then $IconResBase1 = $A ExitLoop EndIf WEnd Dim $A = 0 , $B = 0 , $C While 1 $A += 1 $C = True For $B = 1 To $ARRAY_RT_GROUP_ICON_NAME[0] Step 1 If String($A) = String($ARRAY_RT_GROUP_ICON_NAME[$B]) Then $C = False ExitLoop EndIf Next If $C = True Then $IconResBase2 = $A ExitLoop EndIf WEnd $hFile = _WinAPI_CreateFile($InpResFile, 2,2) Local $ICONDIR = DllStructCreate($typedef_struct_ICONDIR) Local $LPICONDIR = DllStructGetPtr($ICONDIR) _WinAPI_ReadFile($hFile, $LPICONDIR, FileGetSize($InpResFile), $nBytes, 0) If $hFile Then _WinAPI_CloseHandle($hFile) Else Return -1 EndIf Local $idType = DllStructGetData($ICONDIR, "idType") Local $idCount = DllStructGetData($ICONDIR, "idCount") Local $ICOGROUPDIR = DllStructCreate($typedef_struct_ICONDIR) DllStructSetData($ICOGROUPDIR, "idReserved", 0) DllStructSetData($ICOGROUPDIR, "idType", $idType) DllStructSetData($ICOGROUPDIR, "idCount", $idCount) For $i = 1 To $idCount Local $LPICONDIRPRT4 = DllStructGetPtr($ICONDIR,4) + ($I - 1) * $ICONDIRENTRYSIZE Local $ICONDIRENTRY = DllStructCreate($typedef_struct_ICONDIRENTRY, $LPICONDIRPRT4) Local $bWidth = DllStructGetData($ICONDIRENTRY, "bWidth") Local $bHeight = DllStructGetData($ICONDIRENTRY, "bHeight") Local $bColorCount = DllStructGetData($ICONDIRENTRY, "bColorCount") Local $wPlanes = DllStructGetData($ICONDIRENTRY, "wPlanes") Local $wBitCount = DllStructGetData($ICONDIRENTRY, "wBitCount") Local $dwbytesInRes = DllStructGetData($ICONDIRENTRY, "dwbytesInRes") Local $dwImageOffset = DllStructGetData($ICONDIRENTRY, "dwImageOffset") Local $LPICOGROUPDIRPRT4 = DllStructGetPtr($ICOGROUPDIR, 4) + ($I - 1) * ($GRPICONDIRENTRYSIZE - 2) Local $GRPICONDIRENTRY = DllStructCreate($typedef_struct_GRPICONDIRENTRY , $LPICOGROUPDIRPRT4) DllStructSetData($GRPICONDIRENTRY, "bWidth", $bWidth) DllStructSetData($GRPICONDIRENTRY, "bHeight", $bHeight) DllStructSetData($GRPICONDIRENTRY, "bColorCount", $bColorCount) DllStructSetData($GRPICONDIRENTRY, "bReserved", 0) DllStructSetData($GRPICONDIRENTRY, "wPlanes", $wPlanes) DllStructSetData($GRPICONDIRENTRY, "wBitCount", $wBitCount) DllStructSetData($GRPICONDIRENTRY, "dwbytesInRese", $dwbytesInRes) DllStructSetData($GRPICONDIRENTRY, "nID", $IconResBase1) Local $RESDATA = DllStructGetPtr($ICONDIR) + $dwImageOffset Local $DllCall = DllCall("kernel32.dll", "hwnd", "BeginUpdateResource", "str", $ExeFile_DllFile, "int", 0) if @error Then Return -2 Local $rh = $DllCall[0] $DllCall = DllCall("kernel32.dll", "int", "UpdateResource", "hwnd", $rh, "Long",$RT_ICON, "Long", $IconResBase1, "short", 0, "ptr", $RESDATA, "long", $dwbytesInRes) $DllCall = DllCall("kernel32.dll", "int", "EndUpdateResource", "hwnd", $rh, "int", 0) ReDim $ResArray[$ResArray[0][0] + 2][3] $ResArray[0][0] += 1 $ResArray[$ResArray[0][0]][1] = $IconResBase1 $ResArray[$ResArray[0][0]][2] = "RT_ICON" $IconResBase1 += 1 Next Local $LPICOGROUPDIR = DllStructGetPtr($ICOGROUPDIR) $DllCall = DllCall("kernel32.dll", "hwnd", "BeginUpdateResource", "str", $ExeFile_DllFile, "int", 0) if @error Then Return -3 $rh = $DllCall[0] $DllCall = DllCall("kernel32.dll", "int", "UpdateResource", "hwnd", $rh, "Long",$RT_GROUP_ICON, "Long",$IconResBase2, "short", 0, "ptr", $LPICOGROUPDIR, "long", DllStructGetSize($ICOGROUPDIR)) $DllCall = DllCall("kernel32.dll", "int", "EndUpdateResource", "hwnd", $rh, "int", 0) ReDim $ResArray[$ResArray[0][0] + 2][3] $ResArray[0][0] += 1 $ResArray[$ResArray[0][0]][1] = $IconResBase2 $ResArray[$ResArray[0][0]][2] = "RT_GROUP_ICON" Return $ResArray EndFunc Func EnumResourceNames($hModule,$lpszType) $StrResName = "" Local $handle = DLLCallbackRegister ("EnumResNameProc", "int", "hwnd;long;long;ptr") Local $DllCall = DllCall("kernel32.dll","int","EnumResourceNames","hwnd",$hModule,"long",$lpszType,"ptr",DllCallbackGetPtr($handle),"ptr",0) if @error Then DllCallbackFree($handle) Return False EndIf DllCallbackFree($handle) if $DllCall[0] = True Then Return $StrResName Else Return False EndIf EndFunc Func EnumResNameProc($hModule,$lpszType,$lpszName,$lParam) if Not StringInStr($StrResName,$lpszName) Then $StrResName &= $lpszName & "|" Return True Else $StrResName = StringTrimRight($StrResName,1) Return False EndIf EndFunc Func LoadIco_FromResources($ExeFile_DllFile , $RsName) Local $paramtype2 Local $LoadLibrary = _WinAPI_LoadLibrary($ExeFile_DllFile) if @error Then Return -1 if IsString($RsName) Then $paramtype2 = "str" $RsName = StringUpper($RsName) ELSE $paramtype2 = "long" $RsName = Int($RsName) EndIf Local $HICON = DllCall("user32.dll","hwnd","LoadIcon","hwnd",$LoadLibrary,$paramtype2,$RsName) if Not @error Then $HICON = $HICON[0] if $HICON = 0 Then return -2 Else return -2 EndIf _WinAPI_FreeLibrary($LoadLibrary) Return $HICON EndFunc Func UpdateBitmapResource($ExeFile_DllFile,$BitmapFile,$RsName) Local $nBytes ,$tSize ,$bitmap , $RT_BITMAP = 2 , $hFile , $tBuffer , $result , $FILE_BEGIN = 0 ,$ResArray[3][3] , _ $IMAGE_BITMAP =0 , $LR_LOADFROMFILE = 0x0010 ,$paramtype2 $ResArray[0][1] = "ResourceName" $ResArray[0][2] = "ResourceType" $ResArray[1][0] = "Name" $ResArray[2][0] = "Type" Local $hBitmap = _WinAPI_LoadImage(_WinAPI_GetModuleHandle(0),$BitmapFile,$IMAGE_BITMAP ,0,0,$LR_LOADFROMFILE) if Not ($hBitmap) Then return -1 if IsString($RsName) Then $paramtype2 = "str" $RsName = StringUpper($RsName) ELSE $paramtype2 = "long" $RsName = Int($RsName) EndIf $hFile = _WinAPI_CreateFile($BitmapFile, 2,2) if Not $hFile Then Return -2 $tSize = FileGetSize($BitmapFile) + 1 $bitmap = DllStructCreate("char[" & $tSize & "]") _WinAPI_SetFilePointer($hFile, 14 , $FILE_BEGIN) _WinAPI_ReadFile($hFile, DllStructGetPtr($bitmap), $tSize, $nBytes) if @error Then _WinAPI_CloseHandle($hFile) return -3 EndIf _WinAPI_CloseHandle($hFile) $result = DllCall("kernel32.dll", "hwnd", "BeginUpdateResource", "str", $ExeFile_DllFile, "int", 0) if Not @error Then Local $rh = $result[0] if $rh = 0 Then return -4 Else return -4 EndIf $result = DllCall("kernel32.dll", "int", "UpdateResource", "hwnd", $rh, "Long", int($RT_BITMAP), $paramtype2 , $RsName, "short", 0, "ptr", _ DllStructGetPtr($bitmap), 'dword', DllStructGetSize($bitmap)) if Not @error Then if $result[0] = 0 Then return -5 Else return -5 EndIf $result = DllCall("kernel32.dll", "int", "EndUpdateResource", "hwnd", $rh, "int", 0) if Not @error Then if $result[0] = 0 Then return -6 Else return -6 EndIf $ResArray[0][0] = 2 $ResArray[1][1] = $RsName $ResArray[1][2] = $RT_BITMAP $ResArray[2][1] = VarGetType($RsName) $ResArray[2][2] = VarGetType($RT_BITMAP) return $ResArray EndFunc Func LoadBitmap_FromResources($ExeFile_DllFile , $resource_Name) Local $paramtype2 Local $LoadLibrary = _WinAPI_LoadLibrary($ExeFile_DllFile) if @error Then Return -1 if IsString($resource_Name) Then $paramtype2 = "str" $resource_Name = StringUpper($resource_Name) ELSE $paramtype2 = "long" $resource_Name = Int($resource_Name) EndIf Local $HBITMAP = DllCall("User32.dll", "hwnd","LoadBitmapA","hwnd",$LoadLibrary,$paramtype2,$resource_Name) if Not @error Then if $HBITMAP[0] = 0 Then return -2 Else return -2 EndIf Return $HBITMAP[0] EndFunc ;BM_SETIMAGE Message ;Associates a new image (icon or bitmap) with the button. Func BUTTON_SETICON($hWnd,$HandleImage,$IconRsName,$ExeFile_DllFile = "") Local $GWL_STYLE = (-16) , $BS_BITMAP = 0x00000080 , $BS_ICON = 0x00000040 , _ $BM_SETIMAGE = 0x00F7 , $paramtype2 If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) if Not BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_ICON) = $BS_ICON Then _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) + $BS_ICON ) ElseIf BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_BITMAP) = $BS_BITMAP Then _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) - $BS_BITMAP) EndIf if $HandleImage <> 0 Then Return _SendMessage($hWnd, $BM_SETIMAGE, 1, $HandleImage) ;BOOL Else Local $HINSTANCE = _WinAPI_LoadLibrary($ExeFile_DllFile) if $HINSTANCE = 0 Then Return False if IsString($IconRsName) Then $paramtype2 = "str" $IconRsName = StringUpper($IconRsName) ELSE $paramtype2 = "long" $IconRsName = Int($IconRsName) EndIf Local $HICON = DllCall("user32.dll","hwnd","LoadIcon","hwnd",$HINSTANCE,$paramtype2,$IconRsName) if $HICON[0] = 0 Then Return False _WinAPI_FreeLibrary($HINSTANCE) Local $lResult = _SendMessage($hWnd, $BM_SETIMAGE, 1, $HICON[0]) ;BOOL _WinAPI_DestroyIcon($HICON[0]) _WinAPI_UpdateWindow($hWnd) Return $lResult EndIf EndFunc Func BUTTON_SETBITMAP($hWnd,$HandleImage,$BitmapRsName,$ExeFile_DllFile = "") Local $GWL_STYLE = (-16) , $BS_BITMAP = 0x00000080 , $BS_ICON = 0x00000040 , _ $BM_SETIMAGE = 0x00F7 , $paramtype2 If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) if Not BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_ICON) = $BS_BITMAP Then _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) + $BS_BITMAP ) ElseIf BitAND(_WinAPI_GetWindowLong($hWnd, $GWL_STYLE), $BS_ICON) = $BS_ICON Then _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, _WinAPI_GetWindowLong($hWnd, $GWL_STYLE) - $BS_ICON) EndIf if $HandleImage <> 0 Then Return _SendMessage($hWnd, $BM_SETIMAGE, 0, $HandleImage) ;BOOL Else Local $HINSTANCE = _WinAPI_LoadLibrary($ExeFile_DllFile) if $HINSTANCE = 0 Then Return False if IsString($BitmapRsName) Then $paramtype2 = "str" $BitmapRsName = StringUpper($BitmapRsName) ELSE $paramtype2 = "long" $BitmapRsName = Int($BitmapRsName) EndIf Local $HBITMAP = DllCall("User32.dll", "hwnd","LoadBitmap","hwnd",$HINSTANCE,$paramtype2,$BitmapRsName) if $HBITMAP[0] = 0 Then Return False _WinAPI_FreeLibrary($HINSTANCE) Local $lResult = _SendMessage($hWnd, $BM_SETIMAGE, 0, $HBITMAP[0]) ;BOOL _WinAPI_UpdateWindow($hWnd) Return $lResult EndIf EndFunc ;STM_SETIMAGE Message ;his message is sent by an application to associate a new image صرح السماء كان هنا
Dougiefresh Posted August 23, 2009 Posted August 23, 2009 (edited) Looks very functional! Can you make it so this code will allow for inserting resources into dlls with different language IDs? Edited August 23, 2009 by Dougiefresh
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