Function Reference


_WinAPI_UpdateResource

Adds, deletes, or replaces a resource in a portable executable (PE) file

#include <WinAPIRes.au3>
_WinAPI_UpdateResource ( $hUpdate, $sType, $sName, $iLanguage, $pData, $iSize )

Parameters

$hUpdate A module handle returned by the _WinAPI_BeginUpdateResource(), referencing the file to be updated.
$sType The resource type to be updated. Alternatively, rather than a pointer, this parameter can be an
integer value representing a predefined resource type. If the first character of the string is a
pound sign (#), then the remaining characters represent a decimal number that specifies the integer
identifier of the resource type. For example, the string "#258" represents the identifier 258.
Also, you can use a predefined resource types ($RT_*).
$sName The name of the resource to be updated. This parameter can be string or integer value.
$iLanguage The language identifier of the resource.
$pData The resource data to be inserted into the file indicated by $hUpdate parameter. If the resource is
one of the predefined types, the data must be valid and properly aligned. Note that this is the raw
binary data, not the data provided by _WinAPI_LoadIcon(), _WinAPI_LoadString(), or other resource-
specific load functions. All data containing strings or text must be in Unicode format. If $pData
is 0 and $iSize is 0, the specified resource is deleted from the file indicated by $hUpdate.
$iSize The size, in bytes, of the resource data at $pData.

Return Value

Success: True.
Failure: False.

Remarks

It is recommended that the resource file is not loaded before this function is called. However, if that file
is already loaded, it will not cause an error to be returned.

An application can use _WinAPI_UpdateResource() repeatedly to make changes to the resource data. Each call
to _WinAPI_UpdateResource() contributes to an internal list of additions, deletions, and replacements but does
not actually write the data to the file. The application must use the _WinAPI_EndUpdateResource() function to
write the accumulated changes.

If $pData is 0 and $iSize is 0, the specified resource is deleted from the file indicated by $hUpdate.

Related

_WinAPI_BeginUpdateResource, _WinAPI_EndUpdateResource, _WinAPI_LoadIcon, _WinAPI_LoadString

See Also

Search UpdateResource in MSDN Library.

Example

#include <APIResConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>
#include <WinAPIRes.au3>

Global Const $g_sExe = @TempDir & '\MyProg.exe'

Global Const $tagICONRESDIR = 'byte Width;byte Height;byte ColorCount;byte Reserved;ushort Planes;ushort BitCount;dword BytesInRes;ushort IconId;'
Global Const $tagNEWHEADER = 'ushort Reserved;ushort ResType;ushort ResCount;' ; & $tagICONRESDIR[ResCount]

; Select icon to update resource
Local $sIcon = FileOpenDialog('Select File', @ScriptDir & '\Extras', 'Icon Files (*.ico)', BitOR($FD_FILEMUSTEXIST, $FD_PATHMUSTEXIST), 'Script.ico')
If Not $sIcon Then
        Exit
EndIf

; Create simple executable file (MyProg.exe) in which will be added icon
If Not FileCopy(@ScriptDir & '\Extras\MyProg.exe', $g_sExe, $FC_OVERWRITE) Then
        If MsgBox(($MB_OKCANCEL + $MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unable to copy MyProg.exe or file already exist in the current directory.') <> 1 Then
                Exit
        EndIf
        FileCopy(@ScriptDir & '\Extras\MyProg.exe', $g_sExe, $FC_OVERWRITE)
EndIf

Local $iError = 1
Do
        ; Begin update resources
        Local $hUpdate = _WinAPI_BeginUpdateResource($g_sExe)
        If @error Then
                ExitLoop
        EndIf
        ; Read .ico file as raw binary data into the structure
        Local $tIcon = DllStructCreate('ushort Reserved;ushort Type;ushort Count;byte[' & (FileGetSize($sIcon) - 6) & ']')
        Local $hFile = _WinAPI_CreateFile($sIcon, 2, 2)
        If Not $hFile Then
                ExitLoop
        EndIf
        Local $iBytes = 0
        _WinAPI_ReadFile($hFile, $tIcon, DllStructGetSize($tIcon), $iBytes)
        _WinAPI_CloseHandle($hFile)
        If Not $iBytes Then
                ExitLoop
        EndIf
        ; Add all icons from .ico file into the RT_ICON resources identified as 400, 401, etc., and fill group icons structure
        Local $iCount = DllStructGetData($tIcon, 'Count')
        Local $tDir = DllStructCreate($tagNEWHEADER & 'byte[' & (14 * $iCount) & ']')
        Local $pDir = DllStructGetPtr($tDir)
        DllStructSetData($tDir, 'Reserved', 0)
        DllStructSetData($tDir, 'ResType', 1)
        DllStructSetData($tDir, 'ResCount', $iCount)
        Local $tInfo, $iSize, $tData, $iID = 400
        Local $pIcon = DllStructGetPtr($tIcon)
        For $i = 1 To $iCount
                $tInfo = DllStructCreate('byte Width;byte Heigth;byte Colors;byte Reserved;ushort Planes;ushort BPP;dword Size;dword Offset', $pIcon + 6 + 16 * ($i - 1))
                $iSize = DllStructGetData($tInfo, 'Size')
                If Not _WinAPI_UpdateResource($hUpdate, $RT_ICON, $iID, 0, $pIcon + DllStructGetData($tInfo, 'Offset'), $iSize) Then
                        ExitLoop 2
                EndIf
                $tData = DllStructCreate($tagICONRESDIR, $pDir + 6 + 14 * ($i - 1))
                DllStructSetData($tData, 'Width', DllStructGetData($tInfo, 'Width'))
                DllStructSetData($tData, 'Height', DllStructGetData($tInfo, 'Heigth'))
                DllStructSetData($tData, 'ColorCount', DllStructGetData($tInfo, 'Colors'))
                DllStructSetData($tData, 'Reserved', 0)
                DllStructSetData($tData, 'Planes', DllStructGetData($tInfo, 'Planes'))
                DllStructSetData($tData, 'BitCount', DllStructGetData($tInfo, 'BPP'))
                DllStructSetData($tData, 'BytesInRes', $iSize)
                DllStructSetData($tData, 'IconId', $iID)
                $iID += 1
        Next
        ; Add new RT_GROUP_ICON resource named as "MAINICON"
        If Not _WinAPI_UpdateResource($hUpdate, $RT_GROUP_ICON, 'MAINICON', 0, $pDir, DllStructGetSize($tDir)) Then
                ExitLoop
        EndIf
        $iError = 0
Until 1

; Save or discard changes of the resources within an executable file
If Not _WinAPI_EndUpdateResource($hUpdate, $iError) Then
        $iError = 1
EndIf

; Show message if an error occurred
If $iError Then
        MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', 'Unable to change resources.')
EndIf