使用IOCTL代码实现LCD背光调节
国内这种代码找不到。于是參考了相关代码后完好例如以下代码,且实现方式通过IOCTL代码实现LCD背光调节的功能。
适合场合为平板电脑或者笔记本。主要还是要靠BIOS支持与否。
编译环境使用:Dev-cpp工具编写。
#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <ddk/Ntddvdeo.h> typedef struct _DISPLAY_BRIGHTNESS { UCHAR ucDisplayPolicy; UCHAR ucACBrightness; UCHAR ucDCBrightness; } DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS; #define IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x125, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x126, METHOD_BUFFERED, FILE_ANY_ACCESS) #define IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x127, METHOD_BUFFERED, FILE_ANY_ACCESS) void help(void) { fprintf(stderr, "(c) Tody, 2014\n"); fprintf(stderr, "Usage: wbl.exe -b <level>\n"); fprintf(stderr, " Option:\n"); fprintf(stderr, " -b <level>\t brightness level need to be specified\n"); fprintf(stderr, "\n Error code:\n"); fprintf(stderr, " Pass: 0; Fail: 1\n"); exit(1); } int setBrightness(int level) { HANDLE h ; DWORD nOutBufferSize = 256; BYTE SupportedBrightness[256]; DWORD g_supportedLevelCount; DISPLAY_BRIGHTNESS DisplayBrightness; memset(SupportedBrightness, 0, sizeof(SupportedBrightness)); /* use createfile function to open lcd. * url from microsoft about IOCTL code. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa372703%28v=vs.85%29.aspx */ h = CreateFile("\\\\.\\LCD", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0); if(h == INVALID_HANDLE_VALUE){ printf("Open \\\\.\\LCD error"); exit(1); } /* Query for display supported level */ if(!DeviceIoControl(h, IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS, NULL, 0, SupportedBrightness, nOutBufferSize, &g_supportedLevelCount, NULL)){ printf("IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize); exit(1); } if (g_supportedLevelCount == 0) /* 0 means not supported */ { printf("\nLCD not support LEVEL COUNT", g_supportedLevelCount); exit(1); } DisplayBrightness.ucDisplayPolicy = 0; DisplayBrightness.ucACBrightness = level; DisplayBrightness.ucDCBrightness = level; /* Set display backlight level */ nOutBufferSize = sizeof(DisplayBrightness); if(!DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS *)&DisplayBrightness, nOutBufferSize, NULL, 0, &nOutBufferSize, NULL)){ printf("IOCTL_VIDEO_SET_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize); exit(1); } Sleep(500); /* delay for some time while wmi event changed */ nOutBufferSize = sizeof(DisplayBrightness); if(!DeviceIoControl(h, IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS, NULL, 0, (DISPLAY_BRIGHTNESS *)&DisplayBrightness, nOutBufferSize, &nOutBufferSize, NULL)){ printf("IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize); exit(1); } printf("\nBrightness_AC: %d\nBrightness_DC: %d", DisplayBrightness.ucACBrightness, DisplayBrightness.ucDCBrightness); } int main(int argc, char *argv[]) { /* program with arguments support */ if (argc == 3 && strcmp("-b", argv[1])==0){ setBrightness(atoi(argv[2])); }else{ help(); } return 0; }