By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

WmiSetBrightness method not supported (.Net 4.0, Winforms, Windows 7).

P: 79
I'm trying to adjust the brightness (not the gamma ramp values) of the monitor using WmiSetBrightness method but the system throws a ManagementException during runtime saying 'Not supported'. I have seen a lot of people using this concept and running successfully. So what's wrong with mine? Is it because this is not supported in Windows 7 Ultimate; on which I'm working?
My Code follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1_Click
  2.     Try
  3.         Dim mclass As New ManagementClass("WmiMonitorBrightnessMethods")
  4.         mclass.Scope = New ManagementScope("\\.\root\wmi")
  5.         Dim instances As ManagementObjectCollection = mclass.GetInstances()
  6.  
  7.         For Each instance As ManagementObject In instances
  8.             Dim brightness As Byte = 50 'In percent.
  9.             Dim timeout As UInt64 = 1 'In seconds.
  10.             Dim args As Object() = New Object() {brightness, timeout}
  11.             instance.InvokeMethod("WmiSetBrightness", args) 'Only work on the first object.
  12.             Exit For
  13.         Next
  14.     Catch ex As ManagementException
  15.         MessageBox.Show(ex.Message)
  16.     End Try
  17. End Sub
To test if the instances are literally obtained, I commented the foreach part and tried to print the count of the instances using instances.Count.ToString() through a MessageBox but still the exception is thrown.

As an alternative, I tried to work with DeviceIOControl and IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS but of no success. The code follows:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Declare Auto Function CreateFile Lib "kernel32" _
  3.     (ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
  4.      ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
  5.      ByVal dwCreationDisposition As Int32, _
  6.      ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As IntPtr
  7.  
  8.     Const GENERIC_READ As Int32 = &H80000000%
  9.     Const SHARE_ALL As Int32 = &H7%
  10.     Const OPEN_EXISTING As Int32 = &H3%
  11.  
  12.     Const IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS As Integer = &H23049C%
  13.  
  14.     Private Structure BRIGHTNESS
  15.         Public ucDisplayPolicy As Byte
  16.         Public ucACBrightness As Byte
  17.         Public ucDCBrightness As Byte
  18.     End Structure
  19.  
  20.     Private Declare Auto Function DeviceIoControl Lib "kernel32" _
  21.      (ByVal hDevice As IntPtr, ByVal dwIoControlCode As Int32, _
  22.      ByRef lpInBuffer As BRIGHTNESS, ByVal nInBufferSize As Int32, _
  23.      ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Int32, _
  24.      ByRef lpBytesReturned As Int32, ByVal lpOverlapped As IntPtr) As Boolean
  25.  
  26.     Private Declare Auto Function CloseHandle Lib "kernel32" _
  27.      (ByVal hObject As IntPtr) As Boolean
  28.  
  29.     ''' <summary>
  30.     ''' Set the brightness of the screen.
  31.     ''' </summary>
  32.     ''' <param name="Brightness">The Screen Brightness.</param>
  33.     Private Sub SetBrightness(ByRef Brightness As BRIGHTNESS)
  34.         ' Get the display.
  35.         Dim pDisplay As IntPtr = _
  36.          CreateFile("\\.\LCD", GENERIC_READ, SHARE_ALL, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
  37.  
  38.         ' Set the brightness.
  39.         DeviceIoControl(pDisplay, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, _
  40.         Brightness, Runtime.InteropServices.Marshal.SizeOf(Brightness), IntPtr.Zero, 0, Nothing, IntPtr.Zero)
  41.  
  42.         ' Close the handle.
  43.         CloseHandle(pDisplay)
  44.     End Sub
  45.  
  46.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  47.         Dim brightness As BRIGHTNESS
  48.  
  49.         ' The brightness on DC Power.
  50.         brightness.ucDCBrightness = 100
  51.         ' The Brightness on AC Power.
  52.         brightness.ucACBrightness = 100
  53.  
  54.         ' Call the function.
  55.         SetBrightness(brightness)
  56.     End Sub
  57. End Class
For some reason DeviceIoControl always returns False.
Please assist in sorting these out.

BTW, this is not about modifying gamma ramp values using SetDeviceGammaRamp, as it adjusts the contrast of the monitor; not the brightness. So, any post on that matter won't be of much help.

Regards!
Sep 15 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.