views:

949

answers:

5

I have Windows Server 2008 installed on a Sony laptop and the brightness control doesn't work. I'd like to write a program to allow me to change it.

Currently what I have to do is open the Power control panel, click advanced settings, and fight through so many UAC boxes that anybody watching me must think I'm completely crazy.

I just want a simple little program to do it but i dont know what API to call

A: 

This is vista only:

http://msdn.microsoft.com/en-us/library/ms775232.aspx

You need to identify the monitor with GetPhysicalMonitorsFromHMONITOR before setting its brightness with SetMonitorBrightness. I suspect nobody's done it before in .net so you'll probably need to write your own interop. The api doesn't appear to be particularly difficult so you should be able to do it pretty easily.

DaEagle
Vista and Server 2008
Lucas
+1  A: 

As far as I'm aware, there are no managed (.NET) APIs for this. However, for Vista, there are unmanaged APIs available via interop. See MSDN: Monitor Configuration APIs and WmiSetBrightness.

There may be managed ways of hitting the WmiSetBrightness method, but I'm not aware of them.

If you're not on Vista or Server 2008, you're in for a world of unpleasantness: The software configuration would have to be done directly through the monitor driver. Based on your mentioning UAC above, I suspect this isn't the case for you, but the next guy might want to know. :)

John Rudy
+1, I based my answer on yours ;)
Lucas
A: 

To add to DaEagles answer, the http://www.pinvoke.net/ site can be a life saver when working with win32-apis in .Net. If the information does not exist there, please add it when you have made a succesful interop for .Net.

Stefan
A: 

Just a brainstormer here... On the laptop you can change the brightness using some key-kombinations on the keyboard. It should be possible to send those keyspresses If you just want to make it darker/lighter and not have to set it to a specific value.

Stefan
the problem is that stupid sony has a driver for brightness (its not hardware based). i just couldnt get it working with 2008 server with UAC. never tried without
Simon_Weaver
cos you know you REALLY need an onscreen indication that brightness has changed. and a beep. i got the volume control working, but brightness doesnt
Simon_Weaver
A: 

I looked up John Rudy's link to WmiSetBrightness in MSDN and came up with this:

ManagementClass mclass = new ManagementClass("WmiMonitorBrightnessMethods");
mclass.Scope = new ManagementScope(@"\\.\root\wmi");
ManagementObjectCollection instances = mclass.GetInstances();

// I assume you get one instance per monitor
foreach(ManagementObject instance in instances)
{
    ulong timeout = 1; // in seconds
    ushort brightness = 50; // in percent
    object[] args = new object[] { timeout, brightness };
    instance.InvokeMethod("WmiSetBrightness", args);
}

Note: ManagementClass, ManagementObjectCollection, and ManagementObject all implement IDisposable. You should call Dispose() or use "using" to avoid leaking resources.

Lucas