Jump to content

update resource


bub
 Share

Recommended Posts

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPI.au3>
_Res_Update("my.exe","ciao.txt",10,"index")
Func _RES_UPDATE($ScriptFile_Out, $INPRESFILE, $RTYPE, $RNAME, $RLANGUAGE = 1033)
$rh = DllCall("kernel32.dll", "ptr", "BeginUpdateResourceW", "wstr", $ScriptFile_Out, "int", 0)
$rh = $rh[0]
Local $RESULT, $HFILE, $TSIZE, $TBUFFER, $PBUFFER, $BREAD = 0,$ICONRESBASE
Local $RTYPE_TYPE, $RNAME_TYPE
Local $ARESOURCE_TYPES[24] = ["RT_CURSOR", "RT_BITMAP", "RT_ICON", "RT_MENU", "RT_DIALOG", "RT_STRING", "RT_FONTDIR", "RT_FONT", "RT_ACCELERATOR", "RT_RCDATA", "RT_MESSAGETABLE", "RT_GROUPCURSOR", "", "RT_GROUPICON", "", "RT_VERSION", "RT_DLGINCLUDE", "", "RT_PLUGPLAY", "RT_VXD", "RT_ANICURSOR", "RT_ANIICON", "RT_HTML", "RT_MANIFEST"]
If StringIsDigit($RTYPE) Then $RTYPE = Number($RTYPE)
If StringIsDigit($RNAME) Then $RNAME = Number($RNAME)
If IsString($RTYPE) Then
  For $K = 0 To UBound($ARESOURCE_TYPES) - 1
   If $RTYPE = $ARESOURCE_TYPES[$K] Then
    $RTYPE = $K + 1
    $RTYPE_TYPE = "long"
   EndIf
  Next
EndIf
If IsString($RTYPE) Then
  $RTYPE_TYPE = "wstr"
  $RTYPE = StringUpper($RTYPE)
Else
  $RTYPE_TYPE = "long"
EndIf
If IsString($RNAME) Then
  $RNAME_TYPE = "wstr"
  $RNAME = StringUpper($RNAME)
Else
  $RNAME_TYPE = "long"
EndIf
If $INPRESFILE = "" Then
  $RESULT = DllCall("kernel32.dll", "int", "UpdateResourceW", "ptr", $RH, $RTYPE_TYPE, $RTYPE, $RNAME_TYPE, $RNAME, "ushort", $RLANGUAGE, "ptr", 0, "dword", 0)
  Return
EndIf
If Not FileExists($INPRESFILE) Then
  Return
EndIf
$HFILE = _WINAPI_CREATEFILE($INPRESFILE, 2, 2)
If Not $HFILE Then
  Return
EndIf
Switch $RTYPE
  Case 2
   $TSIZE = FileGetSize($INPRESFILE) - 14
   $TBUFFER = DllStructCreate("char Text[" & $TSIZE & "]")
   $PBUFFER = DllStructGetPtr($TBUFFER)
   _WINAPI_SETFILEPOINTER($HFILE, 14)
   _WINAPI_READFILE($HFILE, $PBUFFER, $TSIZE, $BREAD, 0)
   If $HFILE Then _WINAPI_CLOSEHANDLE($HFILE)
   If $BREAD > 0 Then
    $RESULT = DllCall("kernel32.dll", "int", "UpdateResourceW", "ptr", $RH, $RTYPE_TYPE, $RTYPE, $RNAME_TYPE, $RNAME, "ushort", $RLANGUAGE, "ptr", $PBUFFER, "dword", $TSIZE)
    If $RESULT[0] <> 1 Then ConsoleWrite("UpdateResources other: $result[0] = " & $RESULT[0] & " - LastError:" & _WINAPI_GETLASTERROR() & ":" & _WINAPI_GETLASTERRORMESSAGE())
   EndIf
  Case 3
   $TSIZE = FileGetSize($INPRESFILE) - 6
   Local $TB_INPUT_HEADER = DllStructCreate("short res;short type;short ImageCount;char rest[" & $TSIZE + 1 & "]")
   Local $PB_INPUT_HEADER = DllStructGetPtr($TB_INPUT_HEADER)
   _WINAPI_READFILE($HFILE, $PB_INPUT_HEADER, FileGetSize($INPRESFILE), $BREAD, 0)
   If $HFILE Then
    $RC = _WINAPI_CLOSEHANDLE($HFILE)
   EndIf
   Local $ICONTYPE = DllStructGetData($TB_INPUT_HEADER, "Type")
   Local $ICONCOUNT = DllStructGetData($TB_INPUT_HEADER, "ImageCount")
   Local $TB_ICONGROUPHEADER = DllStructCreate("short res;short type;short ImageCount;char rest[" & $ICONCOUNT * 14 & "]")
   Local $PB_ICONGROUPHEADER = DllStructGetPtr($TB_ICONGROUPHEADER)
   DllStructSetData($TB_ICONGROUPHEADER, "Res", 0)
   DllStructSetData($TB_ICONGROUPHEADER, "Type", $ICONTYPE)
   DllStructSetData($TB_ICONGROUPHEADER, "ImageCount", $ICONCOUNT)
   For $X = 1 To $ICONCOUNT
    Local $PB_INPUT_ICONHEADER = DllStructGetPtr($TB_INPUT_HEADER, 4) + ($X - 1) * 16
    Local $TB_INPUT_ICONHEADER = DllStructCreate("byte Width;byte Heigth;Byte Colors;Byte res;Short Planes;Short BitPerPixel;dword ImageSize;dword ImageOffset", $PB_INPUT_ICONHEADER)
    Local $ICONWIDTH = DllStructGetData($TB_INPUT_ICONHEADER, "Width")
    Local $ICONHEIGTH = DllStructGetData($TB_INPUT_ICONHEADER, "Heigth")
    Local $ICONCOLORS = DllStructGetData($TB_INPUT_ICONHEADER, "Colors")
    Local $ICONPLANES = DllStructGetData($TB_INPUT_ICONHEADER, "Planes")
    Local $ICONBITPERPIXEL = DllStructGetData($TB_INPUT_ICONHEADER, "BitPerPixel")
    Local $ICONIMAGESIZE = DllStructGetData($TB_INPUT_ICONHEADER, "ImageSize")
    Local $ICONIMAGEOFFSET = DllStructGetData($TB_INPUT_ICONHEADER, "ImageOffset")
    $PB_ICONGROUPHEADER = DllStructGetPtr($TB_ICONGROUPHEADER, 4) + ($X - 1) * 14
    Local $TB_GROUPICON = DllStructCreate("byte Width;byte Heigth;Byte Colors;Byte res;Short Planes;Short BitPerPixel;dword ImageSize;byte ResourceID", $PB_ICONGROUPHEADER)
    DllStructSetData($TB_GROUPICON, "Width", $ICONWIDTH)
    DllStructSetData($TB_GROUPICON, "Heigth", $ICONHEIGTH)
    DllStructSetData($TB_GROUPICON, "Colors", $ICONCOLORS)
    DllStructSetData($TB_GROUPICON, "res", 0)
    DllStructSetData($TB_GROUPICON, "Planes", $ICONPLANES)
    DllStructSetData($TB_GROUPICON, "BitPerPixel", $ICONBITPERPIXEL)
    DllStructSetData($TB_GROUPICON, "ImageSize", $ICONIMAGESIZE)
    $ICONRESBASE += 1
    DllStructSetData($TB_GROUPICON, "ResourceID", $ICONRESBASE)
    Local $PB_ICONDATA = DllStructGetPtr($TB_INPUT_HEADER) + $ICONIMAGEOFFSET
    $RESULT = DllCall("kernel32.dll", "int", "UpdateResourceW", "ptr", $RH, "long", 3, "long", $ICONRESBASE, "ushort", $RLANGUAGE, "ptr", $PB_ICONDATA, "dword", $ICONIMAGESIZE)
    If $RESULT[0] <> 1 Then
     ConsoleWrite("Icon UpdateResources: $result[0] = " & $RESULT[0] & " - LastError:" & _WINAPI_GETLASTERROR() & ":" & _WINAPI_GETLASTERRORMESSAGE())
    EndIf
   Next
   $PB_ICONGROUPHEADER = DllStructGetPtr($TB_ICONGROUPHEADER)
   $RESULT = DllCall("kernel32.dll", "int", "UpdateResourceW", "ptr", $RH, "long", 14, $RNAME_TYPE, $RNAME, "ushort", $RLANGUAGE, "ptr", $PB_ICONGROUPHEADER, "dword", DllStructGetSize($TB_ICONGROUPHEADER))
   If $RESULT[0] <> 1 Then ConsoleWrite("GroupIconUpdateResources: $result[0] = " & $RESULT[0] & " - LastError:" & _WINAPI_GETLASTERROR() & ":" & _WINAPI_GETLASTERRORMESSAGE())
  Case Else
   $TSIZE = FileGetSize($INPRESFILE)
   $TBUFFER = DllStructCreate("char Text[" & $TSIZE & "]")
   $PBUFFER = DllStructGetPtr($TBUFFER)
   _WINAPI_READFILE($HFILE, $PBUFFER, $TSIZE, $BREAD, 0)
   If $HFILE Then _WINAPI_CLOSEHANDLE($HFILE)
   If $BREAD > 0 Then
    $RESULT = DllCall("kernel32.dll", "int", "UpdateResourceW", "ptr", $RH, $RTYPE_TYPE, $RTYPE, $RNAME_TYPE, $RNAME, "ushort", $RLANGUAGE, "ptr", $PBUFFER, "dword", $TSIZE)
    If $RESULT[0] <> 1 Then ConsoleWrite("UpdateResources other: $result[0] = " & $RESULT[0] & " - LastError:" & _WINAPI_GETLASTERROR() & ":" & _WINAPI_GETLASTERRORMESSAGE())
   EndIf
EndSwitch
EndFunc

this function,does not harm the exe file does but even not update its resources

Link to comment
Share on other sites

Compilation term, when talking about AutoIt, means adding encoded script to the body of an interpreter. The result is windows executable with appended binary data representing your script.

Windows-native resource-adding functions will strip everything from the windows executable that's beyond EOF, meaning appended data will be gone after successful resource update.

All that to say that you should add/update resources before you compile or use some of the scripts to save appended data before you update wanted resource and restore it later.

Conveniently I wrote some code that does exactly that, you can search the forums for it, AddResToMe.au3 is the name of the script, I believe. You just want one function from it, not the whole script.

In fact that code is used to write implementation of the procedure for one very popular AutoIt tool.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...