Search code examples
windowsportable-executableresource-editor

Scriptable PE resource editor


I need a tool that's commandline and which supports retrieving version information from the resources of a PE executable and also patch the binary with a new version. What I need to do is to automate incrementing the version of a PE binary.

Any ideas?


Solution

  • Resource Tools library [1, 2] with COM interface. You can use it from VBScript/JScript and automate patching of version information as I already do.

    Import/browse DLL type library to see COM interface syntax and below is code snippet for automation of VersionInfo patching.

    WshShell = WScript.CreateObject("WScript.Shell");
    WScript.Echo("Current Directory: " + WshShell.CurrentDirectory);
    Image = new ActiveXObject("AlaxInfo.ResourceTools.Image");
    Image.Initialize(WshShell.CurrentDirectory + "\\Sample.dll");
    WScript.Echo("Product Version: " + Image.VersionInfo.ProductVersionString);
    WScript.Echo("File Version: " + Image.VersionInfo.FileVersionString);
    
    VersionString = Image.VersionInfo.FileVersionString;
    VersionString = VersionString.replace(" ", "");
    VersionString = VersionString.replace(",", ".");
    if(/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/i.exec(VersionString) == null)
      throw "Incorrect version string";
    V1 = new Number(RegExp.$1);
    V2 = new Number(RegExp.$2);
    V3 = new Number(RegExp.$3);
    V4 = new Number(RegExp.$4);
    WScript.Echo("File Version Components: " + V1 + ", " + V2 + ", " + V3 + ", " + V4);
    V4++;
    VersionString = V1 + "." + V2 + "." + V3 + "." + V4;
    WScript.Echo("New File Version: " + VersionString);
    
    Image.VersionInfo.SetFileVersion(V1 * 65536 + V2, V3 * 65536 + V4);
    Image.VersionInfo.SetString(0, "FileVersion", VersionString);
    Image.VersionInfo.Update();
    Image.EndUpdate(false);
    

    The library is basically a wrapper over BeginUpdateResource function and friends providing you with automation interface into patching not only numbers but other version info components, strings, bitmaps.