def __restore_mbi(self, hProcess, new_mbi, old_mbi, bSkipMappedFiles,
                      bSkipOnError):
        """
        Used internally by L{restore_memory_snapshot}.
        """

##        print "Restoring %s-%s" % (
##            HexDump.address(old_mbi.BaseAddress, self.get_bits()),
##            HexDump.address(old_mbi.BaseAddress + old_mbi.RegionSize,
##                            self.get_bits()))

        try:

            # Restore the region state.
            if new_mbi.State != old_mbi.State:
                if new_mbi.is_free():
                    if old_mbi.is_reserved():

                        # Free -> Reserved
                        address = win32.VirtualAllocEx(hProcess,
                                                       old_mbi.BaseAddress,
                                                       old_mbi.RegionSize,
                                                       win32.MEM_RESERVE,
                                                       old_mbi.Protect)
                        if address != old_mbi.BaseAddress:
                            self.free(address)
                            msg = "Error restoring region at address %s"
                            msg = msg % HexDump(old_mbi.BaseAddress,
                                                self.get_bits())
                            raise RuntimeError(msg)
                        # permissions already restored
                        new_mbi.Protect = old_mbi.Protect

                    else:   # elif old_mbi.is_commited():

                        # Free -> Commited
                        address = win32.VirtualAllocEx(hProcess,
                                                       old_mbi.BaseAddress,
                                                       old_mbi.RegionSize,
                                                       win32.MEM_RESERVE | \
                                                       win32.MEM_COMMIT,
                                                       old_mbi.Protect)
                        if address != old_mbi.BaseAddress:
                            self.free(address)
                            msg = "Error restoring region at address %s"
                            msg = msg % HexDump(old_mbi.BaseAddress,
                                                self.get_bits())
                            raise RuntimeError(msg)
                        # permissions already restored
                        new_mbi.Protect = old_mbi.Protect

                elif new_mbi.is_reserved():
                    if old_mbi.is_commited():

                        # Reserved -> Commited
                        address = win32.VirtualAllocEx(hProcess,
                                                       old_mbi.BaseAddress,
                                                       old_mbi.RegionSize,
                                                       win32.MEM_COMMIT,
                                                       old_mbi.Protect)
                        if address != old_mbi.BaseAddress:
                            self.free(address)
                            msg = "Error restoring region at address %s"
                            msg = msg % HexDump(old_mbi.BaseAddress,
                                                self.get_bits())
                            raise RuntimeError(msg)
                        # permissions already restored
                        new_mbi.Protect = old_mbi.Protect

                    else:   # elif old_mbi.is_free():

                        # Reserved -> Free
                        win32.VirtualFreeEx(hProcess,
                                            old_mbi.BaseAddress,
                                            old_mbi.RegionSize,
                                            win32.MEM_RELEASE)

                else:   # elif new_mbi.is_commited():
                    if old_mbi.is_reserved():

                        # Commited -> Reserved
                        win32.VirtualFreeEx(hProcess,
                                            old_mbi.BaseAddress,
                                            old_mbi.RegionSize,
                                            win32.MEM_DECOMMIT)

                    else:   # elif old_mbi.is_free():

                        # Commited -> Free
                        win32.VirtualFreeEx(hProcess,
                                            old_mbi.BaseAddress,
                                            old_mbi.RegionSize,
                                            win32.MEM_DECOMMIT | win32.MEM_RELEASE)

            new_mbi.State = old_mbi.State

            # Restore the region permissions.
            if old_mbi.is_commited() and old_mbi.Protect != new_mbi.Protect:
                win32.VirtualProtectEx(hProcess, old_mbi.BaseAddress,
                                       old_mbi.RegionSize, old_mbi.Protect)
                new_mbi.Protect = old_mbi.Protect

            # Restore the region data.
            # Ignore write errors when the region belongs to a mapped file.
            if old_mbi.has_content():
                if old_mbi.Type != 0:
                    if not bSkipMappedFiles:
                        self.poke(old_mbi.BaseAddress, old_mbi.content)
                else:
                    self.write(old_mbi.BaseAddress, old_mbi.content)
                new_mbi.content = old_mbi.content

        # On error, skip this region or raise an exception.
        except Exception:
            if not bSkipOnError:
                raise
            msg = "Error restoring region at address %s: %s"
            msg = msg % (
                HexDump(old_mbi.BaseAddress, self.get_bits()),
                traceback.format_exc())
            warnings.warn(msg, RuntimeWarning)
Used internally by L{restore_memory_snapshot}.