4
\$\begingroup\$

I am coming from a stackoverflow question and after I hit a brick wall I tried to find a work around. I only found this working for me and work around the Access is denied errors.

import os
import time
import subprocess
import ctypes
import sys

# Path to MSI Afterburner executable and profile configuration file
afterburner_path = ('"C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe"')
profile_file_path = ("C:\Program Files (x86)\MSI Afterburner\Profiles\VEN_10DE&DEV_1F08&SUBSYS_3FC11458&REV_A1&BUS_1&DEV_0&FN_0.cfg")

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def run_as_admin():
    if not is_admin():
        ctypes.windll.shell32.ShellExecuteW(
            None, "runas", sys.executable, ' '.join(sys.argv), None, 1)
        sys.exit(0)

def get_current_profile():
    with open(profile_file_path, "r") as file:
        for line in file:
            if line.strip().startswith("PowerLimit"):
                power_limit = line.split("=")[-1].strip()
                if power_limit == "100":
                    return "2"
                elif power_limit == "117":
                    return "1"
                else:
                    return "Unknown"

def switch_profile():
    with open(profile_file_path, "r") as file:
        lines = file.readlines()

    new_power_limit = None
    for i, line in enumerate(lines):
        if line.strip().startswith("PowerLimit"):
            power_limit = line.split("=")[-1].strip()
            if power_limit == "100":
                new_power_limit = "117"  # Switching to Profile 1
            elif power_limit == "117":
                new_power_limit = "100"  # Switching to Profile 2
            else:
                return "Unknown"

            # Update the power limit in the configuration file
            lines[i] = f"PowerLimit={new_power_limit}\n"
            break

    if new_power_limit:
        with open(profile_file_path, "w") as file:
            file.writelines(lines)
        return f"Switched to Profile {new_power_limit}"
    return "Unknown"

def restart_afterburner():
    # Kill MSI Afterburner process
    subprocess.run(["taskkill", "/F", "/IM", "MSIAfterburner.exe"], capture_output=True)

    # Wait a moment to ensure the process is terminated
    time.sleep(2)

    # Restart MSI Afterburner
    subprocess.run(afterburner_path, capture_output=True)

if __name__ == "__main__":
    # Ensure the script is running with elevated privileges
    run_as_admin()

    # Check current profile and switch to the other profile
    current_profile = get_current_profile()
    print(f"Current profile: {current_profile}")

    switch_result = switch_profile()
    print("Switching result:", switch_result)

    # Restart MSI Afterburner to apply changes
    restart_afterburner()
    print("MSI Afterburner has been restarted.")

The .cfg file contains:

[Startup]
Format=2
PowerLimit=100
ThermalLimit=
ThermalPrioritize=0
CoreClkBoost=0
VFCurve=0000020080000000000000000000E1430000E143000000000020E4430000E143000000000040E7430000E143000000000060EA430000E143000000000080ED430000E1430000000000A0F0430000E1430000000000C0F3430000E1430000000000E0F6430000E143000000000000FA430000E143000000000020FD430000E14300000000002000440000E1430000000000B001440000E14300000000004003440000E1430000000000D004440080E84300000000006006440080F7430000000000F007440000FF43000000000080094400C00A440000000000100B44004012440000000000A00C4400C019440000000000300E44004021440000000000C00F4400802C440000000000501144000034440000000000E0124400803B44000000000070144400004344000000000000164400804A44000000000090174400005244000000000020194400405D440000000000B01A4400C064440000000000401C4400406C440000000000D01D4400C073440000000000601F4400407B440000000000F0204400608144000000000080224400208544000000000010244400E088440000000000A0254400A08C440000000000302744006090440000000000C02844002094440000000000502A4400E097440000000000E02B4400A09B440000000000702D4400609F440000000000002F440020A344000000000090304400E0A644000000000020324400A0AA440000000000B033440080AC4400000000004035440060AE440000000000D036440040B04400000000006038440020B2440000000000F039440000B4440000000000803B4400E0B5440000000000103D4400C0B7440000000000A03E440080BB4400000000003040440060BD440000000000C041440040BF4400000000005043440020C1440000000000E044440000C344000000000070464400E0C444000000000000484400C0C644000000000090494400A0C8440000000000204B440080CA440000000000B04C440080CA440000000000404E440060CC440000000000D04F440040CE4400000000006051440020D0440000000000F052440000D244000000000080544400E0D344000000000010564400C0D5440000000000A0574400A0D744000000000030594400A0D7440000000000C05A440080D9440000000000505C440060DB440000000000E05D440040DD440000000000705F440020DF4400000000000061440020DF4400000000009062440000E144000000000020644400E0E2440000000000B0654400E0E244000000000040674400C0E4440000000000D0684400A0E6440000000000606A4400A0E6440000000000F06B440080E8440000000000806D440060EA440000000000106F440060EA440000000000A070440040EC4400000000003072440040EC440000000000C073440020EE4400000000005075440000F0440000000000E076440000F044000000000070784400E0F1440000000000007A4400E0F1440000000000907B4400C0F3440000000000207D4400C0F3440000000000B07E4400A0F544000000000020804400A0F5440000000000E8804400A0F5440000000000B081440080F74400000000007882440080F74400000000004083440060F94400000000000884440060F9440000000000D084440060F94400000000009885440060F94400000000006086440060F94400000000002887440060F9440000000000F087440060F9440000000000B888440060F94400000000008089440060F9440000000000488A440060F9440000000000108B440040FB440000000000D88B440040FB440000000000A08C440040FB440000000000688D440040FB440000000000308E440040FB440000000000F88E440020FD440000000000C08F440020FD4400000000008890440020FD4400000000005091440020FD4400000000001892440020FD440000000000E092440020FD440000000000A893440020FD4400000000007094440020FD4400000000003895440020FD4400000000000096440020FD440000000000C896440020FD4400000000009097440020FD4400000000005898440020FD4400000000002099440020FD440000000000E899440020FD440000000000B09A440020FD440000000000789B440020FD4400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000060DB440000A6420060DB440000AE420060DB440000AA420060DB440000AE4200A0AA440000AE4200A0AA440000AE4200000000000000000000000000000000
MemClkBoost=0
FanMode=1
FanSpeed=25
[Profile1]
Format=2
PowerLimit=117
ThermalLimit=
ThermalPrioritize=0
CoreClkBoost=92000
VFCurve=0000020080000000000000000000E1430000E0430000B8420020E4430000E0430000B8420040E7430000E0430000B8420060EA430000E0430000B8420080ED430000E0430000B84200A0F0430000E0430000B84200C0F3430000E0430000B84200E0F6430000E0430000B8420000FA430000E0430000B8420020FD430000E0430000B842002000440000E0430000B84200B001440000E0430000B842004003440000EF430000B84200D004440080F6430000B8420060064400C002440000B84200F00744008006440000B8420080094400000E440000B84200100B44004019440000B84200A00C4400C020440000B84200300E44004028440000B84200C00F4400C02F440000B84200501144004037440000B84200E0124400C03E440000B84200701444004046440000B8420000164400C04D440000B84200901744004055440000B84200201944008060440000B84200B01A44000068440000B84200401C4400806F440000B84200D01D44000077440000B84200601F4400807E440000B84200F02044002081440000B8420080224400E084440000B8420010244400A088440000B84200A0254400608C440000B84200302744002090440000B84200C0284400E093440000B84200502A4400A097440000B84200E02B4400609B440000B84200702D4400209F440000B84200002F440000A1440000B8420090304400A0A6440000B842002032440080A8440000B84200B033440060AA440000B842004035440040AC440000B84200D036440020AE440000B842006038440000B0440000B84200F0394400E0B1440000B84200803B4400C0B3440000B84200103D4400A0B5440000B84200A03E440080B7440000B842003040440060B9440000B84200C041440040BB440000B842005043440020BD440000B84200E044440000BF440000B8420070464400E0C0440000B8420000484400C0C2440000B8420090494400A0C4440000B84200204B4400A0C4440000B84200B04C440080C6440000B84200404E440060C8440000B84200D04F440040CA440000B842006051440020CC440000B84200F052440020CC440000B842008054440000CE440000B8420010564400E0CF440000B84200A0574400C0D1440000B8420030594400C0D1440000B84200C05A4400A0D3440000B84200505C440080D5440000B84200E05D440060D7440000B84200705F440060D7440000B842000061440040D9440000B842009062440040D9440000B842002064440020DB440000B84200B065440000DD440000B842004067440000DD440000B84200D0684400E0DE440000B84200606A4400E0DE440000B84200F06B4400C0E0440000B84200806D4400C0E0440000B84200106F4400A0E2440000B84200A0704400A0E2440000B842003072440080E4440000B84200C073440080E4440000B842005075440060E6440000B84200E076440060E6440000B842007078440040E8440000B84200007A440040E8440000B84200907B440020EA440000B84200207D440020EA440000B84200B07E440020EA440000B842002080440000EC440000B84200E880440000EC440000B84200B081440000EC440000B8420078824400E0ED440000B8420040834400E0ED440000B8420008844400E0ED440000B84200D0844400E0ED440000B8420098854400E0ED440000B8420060864400E0ED440000B8420028874400E0ED440000B84200F0874400E0ED440000B84200B8884400E0ED440000B8420080894400E0ED440000B84200488A4400E0ED440000B84200108B4400E0ED440000B84200D88B4400E0ED440000B84200A08C4400E0ED440000B84200688D4400E0ED440000B84200308E4400E0ED440000B84200F88E4400E0ED440000B84200C08F4400E0ED440000B8420088904400E0ED440000B8420050914400C0EF440000B8420018924400C0EF440000B84200E0924400C0EF440000B84200A8934400C0EF440000B8420070944400C0EF440000B8420038954400C0EF440000B8420000964400C0EF440000B84200C8964400C0EF440000B8420090974400C0EF440000B8420058984400C0EF440000B8420020994400C0EF440000B84200E8994400C0EF440000B84200B09A4400C0EF440000B84200789B4400C0EF440000B842000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000060DB440000A6420060DB440000AE420060DB440000AA420060DB440000AE4200A0AA440000AE4200A0AA440000AE4200000000000000000000000000000000
MemClkBoost=70000
FanMode=1
FanSpeed=88
[Profile2]
Format=2
PowerLimit=100
ThermalLimit=
ThermalPrioritize=0
CoreClkBoost=0
VFCurve=0000020080000000000000000000E1430000E143000000000020E4430000E143000000000040E7430000E143000000000060EA430000E143000000000080ED430000E1430000000000A0F0430000E1430000000000C0F3430000E1430000000000E0F6430000E143000000000000FA430000E143000000000020FD430000E14300000000002000440000E1430000000000B001440000E14300000000004003440000E1430000000000D004440000F04300000000006006440080F7430000000000F0074400400344000000000080094400C00A440000000000100B44000016440000000000A00C4400801D440000000000300E44000025440000000000C00F4400802C440000000000501144000034440000000000E0124400803B44000000000070144400C04644000000000000164400404E44000000000090174400C05544000000000020194400405D440000000000B01A4400C064440000000000401C4400406C440000000000D01D4400C073440000000000601F4400407B440000000000F0204400608144000000000080224400208544000000000010244400E088440000000000A0254400A08C440000000000302744006090440000000000C02844002094440000000000502A4400E097440000000000E02B4400A09B440000000000702D4400609F440000000000002F440020A344000000000090304400E0A644000000000020324400C0A8440000000000B033440080AC4400000000004035440060AE440000000000D036440040B04400000000006038440020B2440000000000F039440000B4440000000000803B4400E0B5440000000000103D4400C0B7440000000000A03E4400A0B94400000000003040440080BB440000000000C041440060BD4400000000005043440040BF440000000000E044440020C14400000000007046440000C344000000000000484400E0C444000000000090494400C0C6440000000000204B4400A0C8440000000000B04C440080CA440000000000404E440060CC440000000000D04F440060CC4400000000006051440040CE440000000000F052440020D04400000000008054440000D244000000000010564400E0D3440000000000A0574400C0D544000000000030594400C0D5440000000000C05A4400A0D7440000000000505C440080D9440000000000E05D440060DB440000000000705F440060DB4400000000000061440040DD4400000000009062440020DF4400000000002064440020DF440000000000B065440000E144000000000040674400E0E2440000000000D0684400E0E2440000000000606A4400C0E4440000000000F06B4400A0E6440000000000806D4400A0E6440000000000106F440080E8440000000000A070440080E84400000000003072440060EA440000000000C073440060EA4400000000005075440040EC440000000000E076440040EC4400000000007078440020EE440000000000007A440020EE440000000000907B440000F0440000000000207D440000F0440000000000B07E4400E0F144000000000020804400E0F1440000000000E8804400E0F1440000000000B0814400C0F344000000000078824400C0F344000000000040834400C0F344000000000008844400C0F3440000000000D0844400C0F344000000000098854400A0F544000000000060864400A0F544000000000028874400A0F5440000000000F0874400A0F5440000000000B8884400A0F544000000000080894400A0F5440000000000488A4400A0F5440000000000108B4400A0F5440000000000D88B4400A0F5440000000000A08C4400A0F5440000000000688D440080F7440000000000308E440080F7440000000000F88E440080F7440000000000C08F440080F74400000000008890440080F74400000000005091440080F74400000000001892440080F7440000000000E092440080F7440000000000A893440080F74400000000007094440080F74400000000003895440080F74400000000000096440080F7440000000000C896440080F74400000000009097440080F74400000000005898440080F74400000000002099440080F7440000000000E899440080F7440000000000B09A440080F7440000000000789B440080F74400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000060DB440000A6420060DB440000A6420060DB440000AA420060DB440000AA4200A0AA440000AE4200A0AA440000AE4200000000000000000000000000000000
MemClkBoost=0
FanMode=1
FanSpeed=48
[Profile3]
Format=2
PowerLimit=
ThermalLimit=
ThermalPrioritize=
CoreClkBoost=
VFCurve=
MemClkBoost=
FanMode=
FanSpeed=
[Defaults]
Format=2
PowerLimit=100
ThermalLimit=
ThermalPrioritize=0
CoreClkBoost=0
VFCurve=0000020080000000000000000000E1430000E143000000000020E4430000E143000000000040E7430000E143000000000060EA430000E143000000000080ED430000E1430000000000A0F0430000E1430000000000C0F3430000E1430000000000E0F6430000E143000000000000FA430000E143000000000020FD430000E14300000000002000440000E1430000000000B001440000E14300000000004003440000E1430000000000D004440000F04300000000006006440080F7430000000000F0074400400344000000000080094400C00A440000000000100B44004012440000000000A00C4400C019440000000000300E44000025440000000000C00F4400802C440000000000501144000034440000000000E0124400803B44000000000070144400004344000000000000164400404E44000000000090174400C05544000000000020194400405D440000000000B01A4400C064440000000000401C4400406C440000000000D01D4400C073440000000000601F4400407B440000000000F0204400608144000000000080224400208544000000000010244400E088440000000000A0254400A08C440000000000302744006090440000000000C02844002094440000000000502A4400E097440000000000E02B4400A09B440000000000702D4400609F440000000000002F440020A344000000000090304400E0A644000000000020324400C0A8440000000000B033440080AC4400000000004035440060AE440000000000D036440040B04400000000006038440020B2440000000000F039440000B4440000000000803B4400E0B5440000000000103D4400C0B7440000000000A03E4400A0B94400000000003040440080BB440000000000C041440060BD4400000000005043440040BF440000000000E044440020C14400000000007046440000C344000000000000484400E0C444000000000090494400C0C6440000000000204B4400A0C8440000000000B04C440080CA440000000000404E440060CC440000000000D04F440040CE4400000000006051440020D0440000000000F052440000D24400000000008054440000D244000000000010564400E0D3440000000000A0574400C0D544000000000030594400A0D7440000000000C05A440080D9440000000000505C440080D9440000000000E05D440060DB440000000000705F440040DD4400000000000061440020DF4400000000009062440020DF4400000000002064440000E1440000000000B0654400E0E244000000000040674400E0E2440000000000D0684400C0E4440000000000606A4400A0E6440000000000F06B4400A0E6440000000000806D440080E8440000000000106F440080E8440000000000A070440060EA4400000000003072440040EC440000000000C073440040EC4400000000005075440020EE440000000000E076440020EE4400000000007078440000F0440000000000007A440000F0440000000000907B4400E0F1440000000000207D4400E0F1440000000000B07E4400E0F144000000000020804400C0F3440000000000E8804400C0F3440000000000B0814400A0F544000000000078824400A0F544000000000040834400A0F544000000000008844400A0F5440000000000D0844400A0F54400000000009885440080F74400000000006086440080F74400000000002887440080F7440000000000F087440080F7440000000000B888440080F74400000000008089440080F7440000000000488A440080F7440000000000108B440080F7440000000000D88B440080F7440000000000A08C440060F9440000000000688D440060F9440000000000308E440060F9440000000000F88E440060F9440000000000C08F440060F94400000000008890440060F94400000000005091440060F94400000000001892440040FB440000000000E092440040FB440000000000A893440040FB4400000000007094440040FB4400000000003895440040FB4400000000000096440040FB440000000000C896440040FB4400000000009097440040FB4400000000005898440040FB4400000000002099440040FB440000000000E899440040FB440000000000B09A440040FB440000000000789B440040FB4400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000060DB440000A6420060DB440000A6420060DB440000AA420060DB440000AA4200A0AA440000AE4200A0AA440000AE4200000000000000000000000000000000
MemClkBoost=0
FanMode=1
FanSpeed=25
[Settings]
CaptureDefaults=0
[Profile5]
Format=2
PowerLimit=
ThermalLimit=
ThermalPrioritize=
CoreClkBoost=
VFCurve=
MemClkBoost=
FanMode=
FanSpeed=
[PreSuspendedMode]
Format=2
PowerLimit=
ThermalLimit=
ThermalPrioritize=
CoreClkBoost=
VFCurve=
MemClkBoost=
FanMode=
FanSpeed=

\$\endgroup\$
2

2 Answers 2

4
\$\begingroup\$

argv

In run_as_admin(), this isn't quite the right approach.

        ctypes.windll.shell32.ShellExecuteW(
            None, "runas", sys.executable, ' '.join(sys.argv), None, 1)

This is similar to the innumerable errors that linters like shellcheck call out when someone uses $VARIABLE expansion without surrounding double quotes. There's a difference between e.g.
$ grep "hello world" readme and
$ grep hello world readme.

Rather than a .join(), you wanted ... , *sys.argv, None, 1) there.

Path

Please use from pathlib import Path, rather than str.

afterburner_path = ('"C:\Program Files (x86)\MSI Afterburner\MSIAfterburner.exe"')
profile_file_path = ("C:\Program Files (x86)\MSI Afterburner\Profiles\VEN_... .cfg")

That first one is very odd. Does .run() maybe ask CMD.EXE to strip off the first layer of double quoting? If so, make that explicit, by assigning a path to this path variable, and then specifying extra quotes down at the call site: subprocess.run(f'"{afterburner_path}"', ... ). Please prefer a raw string, r"C:\Program Files...", or better still use ordinary slashes, "C:/Program Files...".

The second one should also use an r-string, or ordinary slashes. At least it doesn't suffer from crazy quoting.

bare except

def is_admin():
    try:
        return ...
    except:

No.

We never write a bare except, for fear of disabling important functionality like CTRL/C handling. Use except Exception: if you really don't know what might happen. Use a more specific exception if you'd like to offer the Gentle Reader a clue about the trouble you anticipate. Routinely use a linter (E722) for help with such details.

docstring

get_current_profile() includes several magic numbers such as 117 that could use explaining. A """docstring""" would be a good place to do that, if you're not going the symbolic MANIFEST_CONSTANT route.

Oh, and then we see them repeated in switch_profile(). Either DRY it up, or define some symbols.

main guard

Thank you for ensuring that a test suite can safely import this module without side effects.

The various helpers are well named, and they make the main code read quite smoothly; I appreciate that.

\$\endgroup\$
6
\$\begingroup\$

It seems doubtful that you should be elevating this process to admin at all. Admin can do anything on your system. Instead, why not just change the permissions on the config file so that your user can write? This is much more narrow, and if the designers of Afterburner were thinking about the user they would have done this by way of storing the file in the user's profile directory instead of in program files.

Don't parse the config file on your own; use the Python built-in for this purpose.

100 and 117 seem fragile. At the least, you should have these set as constants for the script. It's yet more fragile to do

            if power_limit == "100":
                return "2"
            elif power_limit == "117":
                return "1"

Instead of hard-coding 1 and 2, you should be returning the section names in which those values are found.

taskkill is probably not the ideal way to kill a process in Windows from Python. I would far sooner use TerminateProcess through ctypes than spawn a whole new process.

Reading of the config file can look like

import configparser
import pathlib
import time


def get_current_power_limits(profile_file_path: pathlib.Path) -> dict[str, int]:
    config = configparser.ConfigParser()
    config.read(profile_file_path)
    return {
        section: int(value)
        for section in config.sections()
        if (
            value := config.get(section=section, option='PowerLimit', fallback='')
        ) != ''
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.