def CopyIcons(dstpath, srcpath): import os.path, string index = None try: srcpath, index = map(string.strip, string.split(srcpath, ',')) index = int(index) except: pass print "I: PATH, INDEX", srcpath, index srcext = os.path.splitext(srcpath)[1] if string.lower(srcext) == '.ico': return CopyIcons_FromIco(dstpath, srcpath) if index is not None: print "I: Updating icons from", srcpath, ", %d to" % index, dstpath else: print "I: Updating icons from", srcpath, "to", dstpath import win32api #, win32con hdst = win32api.BeginUpdateResource(dstpath, 0) hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE) if index is None: grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0] elif index >= 0: grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index] else: grpname = -index data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname) win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data) for iconname in win32api.EnumResourceNames(hsrc, RT_ICON): data = win32api.LoadResource(hsrc, RT_ICON, iconname) win32api.UpdateResource(hdst, RT_ICON, iconname, data) win32api.FreeLibrary(hsrc) win32api.EndUpdateResource(hdst, 0)
def CopyIcons(dstpath, srcpath): import os.path if type(srcpath) in StringTypes: srcpath = [srcpath] def splitter(s): try: srcpath, index = s.split(',') return srcpath.strip(), int(index) except ValueError: return s, None srcpath = list(map(splitter, srcpath)) logger.info("SRCPATH %s", srcpath) if len(srcpath) > 1: # At the moment, we support multiple icons only from .ico files srcs = [] for s in srcpath: e = os.path.splitext(s[0])[1] if e.lower() != '.ico': raise ValueError( 'Multiple icons supported only from .ico files') if s[1] is not None: raise ValueError('index not allowed for .ico files') srcs.append(s[0]) return CopyIcons_FromIco(dstpath, srcs) srcpath, index = srcpath[0] srcext = os.path.splitext(srcpath)[1] if srcext.lower() == '.ico': return CopyIcons_FromIco(dstpath, [srcpath]) if index is not None: logger.info("Updating icons from %s, %d to %s", srcpath, index, dstpath) else: logger.info("Updating icons from %s to %s", srcpath, dstpath) import win32api #, win32con hdst = win32api.BeginUpdateResource(dstpath, 0) hsrc = win32api.LoadLibraryEx(srcpath, 0, LOAD_LIBRARY_AS_DATAFILE) if index is None: grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[0] elif index >= 0: grpname = win32api.EnumResourceNames(hsrc, RT_GROUP_ICON)[index] else: grpname = -index data = win32api.LoadResource(hsrc, RT_GROUP_ICON, grpname) win32api.UpdateResource(hdst, RT_GROUP_ICON, grpname, data) for iconname in win32api.EnumResourceNames(hsrc, RT_ICON): data = win32api.LoadResource(hsrc, RT_ICON, iconname) win32api.UpdateResource(hdst, RT_ICON, iconname, data) win32api.FreeLibrary(hsrc) win32api.EndUpdateResource(hdst, 0)
def CopyIcons_FromIco(dstpath, srcpath): f = IconFile(srcpath) print "I: Updating icons from", srcpath, "to", dstpath import win32api #, win32con hdst = win32api.BeginUpdateResource(dstpath, 0) data = f.grp_icon_dir() data = data + f.grp_icondir_entries() win32api.UpdateResource(hdst, RT_GROUP_ICON, 1, data) print "I: Writing RT_GROUP_ICON resource with %d bytes" % len(data) i = 1 for data in f.images: win32api.UpdateResource(hdst, RT_ICON, i, data) print "I: Writing RT_ICON resource with %d bytes" % len(data) i = i + 1 win32api.EndUpdateResource(hdst, 0)
def UpdateResources(dstpath, data, type_, names=None, languages=None): """ Update or add resource data in dll/exe file dstpath. type_ = resource type to update names = a list of resource names to update (None = all) languages = a list of resource languages to update (None = all) """ # look for existing resources res = GetResources(dstpath, [type_], names, languages) # add type_, names and languages not already present in existing resources if not type_ in res and type_ != "*": res[type_] = {} if names: for name in names: if not name in res[type_] and name != "*": res[type_][name] = [] if languages: for language in languages: if not language in res[type_][name] and language != "*": res[type_][name].append(language) # add resource to destination, overwriting existing resources hdst = win32api.BeginUpdateResource(dstpath, 0) for type_ in res: for name in res[type_]: for language in res[type_][name]: if not silent: print "I: Updating resource type", type_, "name", name, \ "language", language win32api.UpdateResource(hdst, type_, name, data, language) win32api.EndUpdateResource(hdst, 0)
def UpdateResources(dstpath, data, type_, names=None, languages=None): """ Update or add resource data in dll/exe file dstpath. type_ = resource type to update names = a list of resource names to update (None = all) languages = a list of resource languages to update (None = all) """ # Look for existing resources. res = GetResources(dstpath, [type_], names, languages) # add type_, names and languages not already present in existing resources if not type_ in res and type_ != "*": res[type_] = {} if names: for name in names: if not name in res[type_] and name != "*": res[type_][name] = [] if languages: for language in languages: if not language in res[type_][name] and language != "*": res[type_][name].append(language) # add resource to destination, overwriting existing resources hdst = win32api.BeginUpdateResource(dstpath, 0) for type_ in res: for name in res[type_]: for language in res[type_][name]: logger.info("Updating resource type %s name %s language %s", type_, name, language) win32api.UpdateResource(hdst, type_, name, data.encode('UTF-8'), language) win32api.EndUpdateResource(hdst, 0)
def updateExecutableIcon(executablePath, iconPath): """ Updates the icon of a Windows executable file. """ import win32api, win32con handle = win32api.BeginUpdateResource(executablePath, False) icon = open(iconPath, "rb") fileheader = icon.read(6) # Read icon data image_type, image_count = struct.unpack("xxHH", fileheader) icon_group_desc = struct.pack("<HHH", 0, image_type, image_count) icon_sizes = [] icon_offsets = [] # Read data of all included icons for i in range(1, image_count + 1): imageheader = icon.read(16) width, height, colors, panes, bits_per_pixel, image_size, offset = struct.unpack("BBBxHHLL", imageheader) icon_group_desc = icon_group_desc + struct.pack("<BBBBHHIH", width, # Icon width height, # Icon height colors, # Colors (0 for 256 colors) 0, # Reserved2 (must be 0) panes, # Color planes bits_per_pixel, # Bits per pixel image_size, # ImageSize i # Resource ID ) icon_sizes.append(image_size) icon_offsets.append(offset) # Read icon content and write it to executable file for i in range(1, image_count + 1): icon_content = icon.read(icon_sizes[i - 1]) win32api.UpdateResource(handle, win32con.RT_ICON, i, icon_content) win32api.UpdateResource(handle, win32con.RT_GROUP_ICON, "MAINICON", icon_group_desc) win32api.EndUpdateResource(handle, False)
def SetVersion(exenm, versionfile): if isinstance(versionfile, VSVersionInfo): vs = versionfile else: txt = open(versionfile, 'rU').read() vs = eval(txt) hdst = win32api.BeginUpdateResource(exenm, 0) win32api.UpdateResource(hdst, RT_VERSION, 1, vs.toRaw()) win32api.EndUpdateResource (hdst, 0)
def add_icon_to_exe(source_icon_file: Path, target_exe_file: Path): target_exe_file = target_exe_file.absolute().resolve() if not target_exe_file.is_file(): raise FileNotFoundError( f"The target executable file could not be found or is not a valid file: {target_exe_file}" ) source_icon_file = source_icon_file.absolute().resolve() if not source_icon_file.is_file(): raise FileNotFoundError( f"The icon file could not be found or is not a valid file: {source_icon_file}" ) icon = Icon(file_name=source_icon_file) ur_handle = win32api.BeginUpdateResource(str(target_exe_file), 0) win32api.UpdateResource( ur_handle, RT_GROUP_ICON, 0, icon.get_header_and_group_icon_dir_data() ) for i, icon_data in enumerate(icon.get_icon_data()): win32api.UpdateResource(ur_handle, RT_ICON, i + 1, icon_data) win32api.EndUpdateResource(ur_handle, 0)
def CopyIcons_FromIco(dstpath, srcpath, id=1): import win32api #, win32con icons = map(IconFile, srcpath) logger.info("Updating icons from %s to %s", srcpath, dstpath) hdst = win32api.BeginUpdateResource(dstpath, 0) iconid = 1 for i, f in enumerate(icons): data = f.grp_icon_dir() data = data + f.grp_icondir_entries(iconid) win32api.UpdateResource(hdst, RT_GROUP_ICON, i, data) logger.info("Writing RT_GROUP_ICON %d resource with %d bytes", i, len(data)) for data in f.images: win32api.UpdateResource(hdst, RT_ICON, iconid, data) logger.info("Writing RT_ICON %d resource with %d bytes", iconid, len(data)) iconid = iconid + 1 win32api.EndUpdateResource(hdst, 0)
def SetVersion(exenm, versionfile): if isinstance(versionfile, VSVersionInfo): vs = versionfile else: fp = codecs.open(versionfile, 'rU', 'utf-8') txt = fp.read() fp.close() vs = eval(txt) hdst = win32api.BeginUpdateResource(exenm, 0) win32api.UpdateResource(hdst, RESOURCE_TYPE['RT_VERSION'], 1, vs.toRaw()) win32api.EndUpdateResource (hdst, 0)
def set_icon(exe_filename, ico_filename): """ Set the icon on a windows executable. """ # Icon file icon = Icon(ico_filename) # Begin update of executable hdst = win32api.BeginUpdateResource(exe_filename, 0) # Update entries data = icon._header + reduce(str.__add__, icon._entries) win32api.UpdateResource(hdst, 14, 1, data) # Update images for i, image in enumerate(icon._images): win32api.UpdateResource(hdst, 3, i + 1, image) # Done win32api.EndUpdateResource(hdst, 0)
def CopyIcons_FromIco(dstpath, srcpath, id=1): import win32api #, win32con icons = map(IconFile, srcpath) print "I: Updating icons from", srcpath, "to", dstpath hdst = win32api.BeginUpdateResource(dstpath, 0) iconid = 1 for i in range(len(icons)): f = icons[i] data = f.grp_icon_dir() data = data + f.grp_icondir_entries(iconid) win32api.UpdateResource(hdst, RT_GROUP_ICON, i, data) print "I: Writing RT_GROUP_ICON %d resource with %d bytes" % ( i, len(data)) for data in f.images: win32api.UpdateResource(hdst, RT_ICON, iconid, data) print "I: Writing RT_ICON %d resource with %d bytes" % (iconid, len(data)) iconid = iconid + 1 win32api.EndUpdateResource(hdst, 0)
def UpdateResourceData(self, res_type, res_lang, res_name, data): """Inserts or updates a given resource with the given data. Args: res_type: the type of the resource, e.g. "B7". res_lang: the language id of the resource, e.g. 1033. res_name: the name of the resource, e.g. "SETUP.EXE". data: the new resource data. """ _LOGGER.info('Writing resource "%s:%s"', res_type, res_name) win32api.UpdateResource(self.update_handle, res_type, res_name, data, res_lang) self._modified = True
def UpdateResource(self, res_type, res_lang, res_name, file_path): """Inserts or updates a given resource with the contents of a file. Args: res_type: the type of the resource, e.g. "B7". res_lang: the language id of the resource, e.g. 1033. res_name: the name of the resource, e.g. "SETUP.EXE". file_path: path to the file containing the new resource data. """ _LOGGER.info('Writing resource "%s:%s" from file %s.', res_type, res_name, file_path) with open(file_path, 'rb') as f: win32api.UpdateResource(self.update_handle, res_type, res_name, f.read(), res_lang) self._modified = True
def SetVersion(exenm, versionfile): txt = open(versionfile, 'r').read() vs = eval(txt + '\n', globals()) hdst = win32api.BeginUpdateResource(exenm, 0) win32api.UpdateResource(hdst, RT_VERSION, 1, vs.toRaw()) win32api.EndUpdateResource(hdst, 0)
def update_manifest(dll, rnum, manifest): import win32api h = win32api.BeginUpdateResource(dll, 0) win32api.UpdateResource(h, 24, rnum, manifest) win32api.EndUpdateResource(h, 0)