Question Use a module to set and retrieve laptop backlight brightness?

JoeM2000

Member
Joined
Oct 19, 2011
Messages
9
Programming Experience
1-3
Hey guys,

I've been learning about VB.NET off and on since 2005, however I wouldn't consider myself advanced by any means - just experienced; I understand most of the basic stuff and SOME advanced.

However, trying to program something like this confuses me. I'm trying to make a program like this (<<< click the link) but I just wanted to make my own custom program because I don't like some things about that one (and also want to add stuff to it, which are quite basic features)

I've been searching on the web for the past hour and have not come up with anything that works for me, much less that I understand. Is there a module, perhaps, that I could use like this:

VB.NET:
Private Sub Form1_Load (whatever, generated code)
Label1.Text = LaptopDisplayBrightness
End Sub

Private Sub Button1_Click Handles (whatever, generated code)
'Increase brightness
SetBrightness(SystemBrightness += 1)
End Sub
'Also lower brightness

Does that make sense? All of the content I have found by searching around is in C, or is referring to changing the desktop gamma which is not what I'm trying to do - I'm trying to change the brightness of the actual backlight to increase battery life, for example. (The link I gave explains it pretty well)

Thanks!
 
Thanks for the quick reply.

The file he supplied doesn't appear to work. When I run the provided C# source .exe through the command prompt, it crashes, and prints the following error:

C:\DisplayBrightnessConsole>DisplayBrightnessConsole.exe

Unhandled Exception: System.Management.ManagementException: Not supported
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStat
us errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.Mo
veNext()
at DisplayBrightnessConsole.Program.GetBrightness()
at DisplayBrightnessConsole.Program.Main(String[] args)

C:\DisplayBrightnessConsole>


(EDIT: For some reason, the forum puts random spaces from what was in the console, so I apologize)


Also when using the converter, I get the following code: (no need for you to analyze it all just yet btw - mainly just focus on the top)
VB.NET:
Imports System.Collections.Generic
Imports System.Text

Namespace DisplayBrightnessConsole
    Class Program
        Private Shared Sub Main(args As String())
            If args.Length = 0 Then
                Console.WriteLine(GetBrightness())
            ElseIf args(0) = "-getlevels" Then
                Dim BrightnessLevels As Byte() = GetBrightnessLevels()

                For Each b As Byte In BrightnessLevels
                    Console.WriteLine(b.ToString())
                Next
            Else
                'parse switch value
                Dim targetBrightness As Byte = Byte.Parse(args(0))
                SetBrightness(targetBrightness)
                    'success value
                Console.WriteLine("0")
            End If
        End Sub

        Private Shared Function GetBrightness() As Byte
            'define scope (namespace)
            Dim s As New System.Management.ManagementScope("root\WMI")

            'define query
            Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")

            'output current brightness
            Dim mos As New System.Management.ManagementObjectSearcher(s, q)

            Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

            'store result
            Dim curBrightness As Byte = 0

            For Each o As System.Management.ManagementObject In moc
                curBrightness = CByte(o.GetPropertyValue("CurrentBrightness"))
                    'only work on the first object
                Exit For
            Next

            moc.Dispose()
            mos.Dispose()

            Return curBrightness
        End Function

        Private Shared Function GetBrightnessLevels() As Byte()
            'define scope (namespace)
            Dim s As New System.Management.ManagementScope("root\WMI")

            'define query
            Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")

            'output current brightness
            Dim mos As New System.Management.ManagementObjectSearcher(s, q)

            Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

            'store result
            Dim BrightnessLevels As Byte() = New Byte(-1) {}

            For Each o As System.Management.ManagementObject In moc
                BrightnessLevels = DirectCast(o.GetPropertyValue("Level"), Byte())
                    'only work on the first object
                Exit For
            Next

            moc.Dispose()
            mos.Dispose()

            Return BrightnessLevels
        End Function

        Private Shared Sub SetBrightness(targetBrightness As Byte)
            'define scope (namespace)
            Dim s As New System.Management.ManagementScope("root\WMI")

            'define query
            Dim q As New System.Management.SelectQuery("WmiMonitorBrightnessMethods")

            'output current brightness
            Dim mos As New System.Management.ManagementObjectSearcher(s, q)

            Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

            For Each o As System.Management.ManagementObject In moc
                o.InvokeMethod("WmiSetBrightness", New [Object]() {UInt32.MaxValue, targetBrightness})
                'note the reversed order - won't work otherwise!
                    'only work on the first object
                Exit For
            Next

            moc.Dispose()
            mos.Dispose()
        End Sub
    End Class
End Namespace

When I start a new vb.net 2010 console application, erase all its default contents, and paste over the code - it tells me that the "System.Management.<whatever>" stuff is not defined, totaling 15 errors. I've also have tried pasting it in between the default generated code like so: (once again, just focus on the top and bottom of the code to begin with)
VB.NET:
Imports System.Collections.Generic
Imports System.Text

Module Module1

    Sub Main()

Namespace DisplayBrightnessConsole
        Class Program
            Private Shared Sub Main(ByVal args As String())
                If args.Length = 0 Then
                    Console.WriteLine(GetBrightness())
                ElseIf args(0) = "-getlevels" Then
                    Dim BrightnessLevels As Byte() = GetBrightnessLevels()

                    For Each b As Byte In BrightnessLevels
                        Console.WriteLine(b.ToString())
                    Next
                Else
                    'parse switch value
                    Dim targetBrightness As Byte = Byte.Parse(args(0))
                    SetBrightness(targetBrightness)
                    'success value
                    Console.WriteLine("0")
                End If
            End Sub

            Private Shared Function GetBrightness() As Byte
                'define scope (namespace)
                Dim s As New System.Management.ManagementScope("root\WMI")

                'define query
                Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")

                'output current brightness
                Dim mos As New System.Management.ManagementObjectSearcher(s, q)

                Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

                'store result
                Dim curBrightness As Byte = 0

                For Each o As System.Management.ManagementObject In moc
                    curBrightness = CByte(o.GetPropertyValue("CurrentBrightness"))
                    'only work on the first object
                    Exit For
                Next

                moc.Dispose()
                mos.Dispose()

                Return curBrightness
            End Function

            Private Shared Function GetBrightnessLevels() As Byte()
                'define scope (namespace)
                Dim s As New System.Management.ManagementScope("root\WMI")

                'define query
                Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")

                'output current brightness
                Dim mos As New System.Management.ManagementObjectSearcher(s, q)

                Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

                'store result
                Dim BrightnessLevels As Byte() = New Byte(-1) {}

                For Each o As System.Management.ManagementObject In moc
                    BrightnessLevels = DirectCast(o.GetPropertyValue("Level"), Byte())
                    'only work on the first object
                    Exit For
                Next

                moc.Dispose()
                mos.Dispose()

                Return BrightnessLevels
            End Function

            Private Shared Sub SetBrightness(ByVal targetBrightness As Byte)
                'define scope (namespace)
                Dim s As New System.Management.ManagementScope("root\WMI")

                'define query
                Dim q As New System.Management.SelectQuery("WmiMonitorBrightnessMethods")

                'output current brightness
                Dim mos As New System.Management.ManagementObjectSearcher(s, q)

                Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()

                For Each o As System.Management.ManagementObject In moc
                    o.InvokeMethod("WmiSetBrightness", New [Object]() {UInt32.MaxValue, targetBrightness})
                    'note the reversed order - won't work otherwise!
                    'only work on the first object
                    Exit For
                Next

                moc.Dispose()
                mos.Dispose()
            End Sub
        End Class
    End Namespace


End Module

This takes away all of the errors except this:
Error 1 Statement cannot appear within a method body. End of method assumed. C:\Users\...\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb 8 1 ConsoleApplication1

...referring to Namespace in the top "Namespace DisplayBrightnessConsole" line.

Help! Thanks again :)
 
To use the Management classes you have to add reference System.Management assembly first.

About "Not supported" in GetBrightness it queries WmiMonitorBrightness class, which documents minimum supported client Windows Vista, so perhaps that is why, it was also mentioned in article. The Monitor Configuration Functions that the WMI class uses also has same requirements.
 
THANKS SO MUCH! Adding the System.Management reference worked on Windows 7 even. :D Tomorrow I'll probably add a tutorial if that's fine because there's like nothing simplistic on the interwebs about how to programatically change the brightness on a laptop. Thanks again!
 
Back
Top