MSI Afterburner - MSI Graphics Card Performance Booster

This example demonstrates using this library in C# to set the fan speed (including toggling off 'auto'), setting the core clock speed, and using serialization to save and restore profiles.

C# Console MACM Application Demo

CopyC#
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using MSI.Afterburner;
using MSI.Afterburner.Exceptions;

namespace CSharpMACMDemo
{
    class CSharpMACMDemo
    {
        static void Main(string[] args)
        {
            bool isDirty = false;

            try
            {
                // connect to MACM shared memory
                ControlMemory macm = new ControlMemory();

                // print out current MACM Header values
                Console.WriteLine("***** MSI AFTERTERBURNER CONTROL HEADER *****");
                Console.WriteLine(macm.Header.ToString().Replace(";", "\n"));
                Console.WriteLine();

                // print out current MACM GPU Entry values
                for(int i = 0; i < macm.Header.GpuEntryCount; i++)
                {
                    Console.WriteLine("***** MSI AFTERTERBURNER GPU " + i + " *****");
                    Console.WriteLine(macm.GpuEntries[i].ToString().Replace(";", "\n"));
                    Console.WriteLine();
                }
                //return;

                // save existing settings for GPU 0 to file
                string filename = System.Environment.GetEnvironmentVariable("TEMP") + @"\gpuEntry0.gpu";
                Stream stream = File.Open(filename, FileMode.Create);
                BinaryFormatter bformatter = new BinaryFormatter();
                bformatter.Serialize(stream, macm.GpuEntries[0]);
                stream.Close();

                // prompt user to change fan speed
                Console.WriteLine("Enter a new Fan speed % for GPU 0");
                Console.WriteLine("Current Fan speed for GPU 0 is: " + macm.GpuEntries[0].FanSpeedCur + "%");
                Console.WriteLine("New value must be between " + macm.GpuEntries[0].FanSpeedMin + " and " + macm.GpuEntries[0].FanSpeedMax);                
                String input = Console.ReadLine();
                UInt32 newValue;
                if (UInt32.TryParse(input, out newValue))
                {
                    try
                    {
                        macm.GpuEntries[0].FanSpeedCur = newValue;
                        isDirty = true;
                    }
                    catch (MACMFanControlNotManual e)
                    {
                        Console.WriteLine("\nYour fan is currently set to auto.  Would you like to change it to manual?  Enter 'Y'");
                        ConsoleKeyInfo cki = Console.ReadKey(true);
                        if (cki.Key == ConsoleKey.Y)
                        {
                            macm.GpuEntries[0].FanFlagsCur = MACM_SHARED_MEMORY_GPU_ENTRY_FAN_FLAG.None;
                            macm.GpuEntries[0].FanSpeedCur = newValue;
                            isDirty = true;
                        }
                        else
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("New fan speed entered is not valid: " + input);
                }

                // prompt user to change core clock speed
                Console.WriteLine("\nEnter a new Core Clock speed for GPU 0");
                Console.WriteLine("Current Core Clock speed for GPU 0 is: " + macm.GpuEntries[0].CoreClockCur);
                Console.WriteLine("New value must be between " + macm.GpuEntries[0].CoreClockMin + " and " + macm.GpuEntries[0].CoreClockMax);                
                input = Console.ReadLine();
                if (UInt32.TryParse(input, out newValue))
                {
                    try
                    {
                        macm.GpuEntries[0].CoreClockCur = newValue;
                        isDirty = true;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                else
                {
                    Console.WriteLine("New clock speed entered is not valid: " + input);
                }         

                // prompt user to apply changes in Afterburner
                if (isDirty)
                {
                    Console.WriteLine("\nWould you like to apply these changes?  Enter 'Y'");
                    ConsoleKeyInfo cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.Y)
                    {
                        // apply these changes in MSI Afterburner
                        Console.WriteLine("Committing Changes.....Pausing 2 seconds\n");
                        macm.CommitChanges();
                        System.Threading.Thread.Sleep(2000);
                        // reread changes from Afterburner to ensure the change took place
                        macm.ReloadAll();
                        Console.WriteLine("Fan Speed for GPU 0 is: " + macm.GpuEntries[0].FanSpeedCur);
                        Console.WriteLine("Core Clock Speed for GPU 0 is: " + macm.GpuEntries[0].CoreClockCur);

                        // prompt user to undo changes
                        Console.WriteLine("\nWould you like to undo these changes?  Enter 'Y'");
                        cki = Console.ReadKey(true);
                        if (cki.Key == ConsoleKey.Y)
                        {
                            // undo changes by reading back saved profile and applying it
                            stream = File.Open(filename, FileMode.Open);
                            bformatter = new BinaryFormatter();
                            ControlMemoryGpuEntry gpu = new ControlMemoryGpuEntry(macm.Header, 0);
                            gpu = (ControlMemoryGpuEntry)bformatter.Deserialize(stream);
                            stream.Close();
                            macm.GpuEntries[0] = gpu;
                            Console.WriteLine("Undoing Changes.....Pausing 2 seconds\n");
                            macm.CommitChanges();
                            System.Threading.Thread.Sleep(2000);
                            // reread changes from Afterburner to ensure the change took place
                            macm.ReloadAll();
                            Console.WriteLine("Fan Speed for GPU 0 is: " + macm.GpuEntries[0].FanSpeedCur);
                            Console.WriteLine("Core Clock Speed for GPU 0 is: " + macm.GpuEntries[0].CoreClockCur);
                        }
                    }
                    else
                    {
                        Console.WriteLine("\n*** Changes aborted ***");
                        Console.WriteLine();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (e.InnerException != null)
                    Console.WriteLine(e.InnerException.Message);
            }

            Console.WriteLine("\nPress any key to exit");
            Console.ReadKey();
        }
    }
}

C# Console MACM Application Output

***** MSI AFTERTERBURNER CONTROL HEADER *****

Signature = MACM

Version = 1.0

HeaderSize = 36

GpuEntryCount = 1

GpuEntrySize = 172

MasterGpu = 0

Flags = LINK, SYNC

Time = 05:17:29 Mar-10-2011

Command = None

-

***** MSI AFTERTERBURNER GPU 0 *****

IsMaster = True

Flags = CORE_CLOCK, SHADER_CLOCK, MEMORY_CLOCK, FAN_SPEED, CORE_VOLTAGE, SYNCHRONIZED_WITH_MASTER

FanFlagsCur = AUTO

FanFlagsDev = AUTO

FanSpeed = Cur:30, Min:30, Max:100, Def:0

CoreClock = Cur:650000, Min:325000, Max:975000, Def:650000

ShaderClock = Cur:1404000, Min:700000, Max:2110000, Def:1404000

MemoryClock = Cur:1163000, Min:580000, Max:1515000, Def:1163000

CoreVoltage = Cur:1187, Min:800, Max:1350, Def:1187

MemoryVoltage = Cur:0, Min:0, Max:0, Def:0

AuxVoltage = Cur:0, Min:0, Max:0, Def:0

CoreVoltageBoost = Cur:0, Min:0, Max:0, Def:0

MemoryVoltageBoost = Cur:0, Min:0, Max:0, Def:0

AuxVoltageBoost = Cur:0, Min:0, Max:0, Def:0

-

Enter a new Fan speed % for GPU 0

Current Fan speed for GPU 0 is: 30%

New value must be between 30 and 100

50

-

Your fan is currently set to auto. Would you like to change it to manual? Enter 'Y'

-

Enter a new Core Clock speed for GPU 0

Current Core Clock speed for GPU 0 is: 650000

New value must be between 325000 and 975000

680000

-

Would you like to apply these changes? Enter 'Y'

Committing Changes.....Pausing 2 seconds

-

Fan Speed for GPU 0 is: 50

Core Clock Speed for GPU 0 is: 680000

-

Would you like to undo these changes? Enter 'Y'

Undoing Changes.....Pausing 2 seconds

-

Fan Speed for GPU 0 is: 50

Core Clock Speed for GPU 0 is: 650000

-

Press any key to exit