当前位置:   article > 正文

使用IOCTL代码实现LCD背光调节_ioctl_video_set_display_brightness

ioctl_video_set_display_brightness

国内这样的代码找不到,于是参考了相关代码后完善如下代码,且实现方式通过IOCTL代码实现LCD背光调节的功能。

适合场合为平板电脑或者笔记本。主要还是要靠BIOS支持与否。

编译环境使用:Dev-cpp工具编写。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <ddk/Ntddvdeo.h>
  5. typedef struct _DISPLAY_BRIGHTNESS {
  6. UCHAR ucDisplayPolicy;
  7. UCHAR ucACBrightness;
  8. UCHAR ucDCBrightness;
  9. } DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS;
  10. #define IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x125, METHOD_BUFFERED, FILE_ANY_ACCESS)
  11. #define IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x126, METHOD_BUFFERED, FILE_ANY_ACCESS)
  12. #define IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS CTL_CODE(FILE_DEVICE_VIDEO, 0x127, METHOD_BUFFERED, FILE_ANY_ACCESS)
  13. void help(void)
  14. {
  15. fprintf(stderr, "(c) Tody, 2014\n");
  16. fprintf(stderr, "Usage: wbl.exe -b <level>\n");
  17. fprintf(stderr, " Option:\n");
  18. fprintf(stderr, " -b <level>\t brightness level need to be specified\n");
  19. fprintf(stderr, "\n Error code:\n");
  20. fprintf(stderr, " Pass: 0; Fail: 1\n");
  21. exit(1);
  22. }
  23. int setBrightness(int level)
  24. {
  25. HANDLE h ;
  26. DWORD nOutBufferSize = 256;
  27. BYTE SupportedBrightness[256];
  28. DWORD g_supportedLevelCount;
  29. DISPLAY_BRIGHTNESS DisplayBrightness;
  30. memset(SupportedBrightness, 0, sizeof(SupportedBrightness));
  31. /* use createfile function to open lcd.
  32. * url from microsoft about IOCTL code.
  33. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa372703%28v=vs.85%29.aspx
  34. */
  35. h = CreateFile("\\\\.\\LCD", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
  36. if(h == INVALID_HANDLE_VALUE){
  37. printf("Open \\\\.\\LCD error");
  38. exit(1);
  39. }
  40. /* Query for display supported level */
  41. if(!DeviceIoControl(h, IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS, NULL, 0, SupportedBrightness, nOutBufferSize, &g_supportedLevelCount, NULL)){
  42. printf("IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize);
  43. exit(1);
  44. }
  45. if (g_supportedLevelCount == 0) /* 0 means not supported */
  46. {
  47. printf("\nLCD not support LEVEL COUNT", g_supportedLevelCount);
  48. exit(1);
  49. }
  50. DisplayBrightness.ucDisplayPolicy = 0;
  51. DisplayBrightness.ucACBrightness = level;
  52. DisplayBrightness.ucDCBrightness = level;
  53. /* Set display backlight level */
  54. nOutBufferSize = sizeof(DisplayBrightness);
  55. if(!DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS *)&DisplayBrightness, nOutBufferSize, NULL, 0, &nOutBufferSize, NULL)){
  56. printf("IOCTL_VIDEO_SET_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize);
  57. exit(1);
  58. }
  59. Sleep(500); /* delay for some time while wmi event changed */
  60. nOutBufferSize = sizeof(DisplayBrightness);
  61. if(!DeviceIoControl(h, IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS, NULL, 0, (DISPLAY_BRIGHTNESS *)&DisplayBrightness, nOutBufferSize, &nOutBufferSize, NULL)){
  62. printf("IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS error - %d, - buffer: %d ", g_supportedLevelCount, nOutBufferSize);
  63. exit(1);
  64. }
  65. printf("\nBrightness_AC: %d\nBrightness_DC: %d", DisplayBrightness.ucACBrightness, DisplayBrightness.ucDCBrightness);
  66. }
  67. int main(int argc, char *argv[]) {
  68. /* program with arguments support */
  69. if (argc == 3 && strcmp("-b", argv[1])==0){
  70. setBrightness(atoi(argv[2]));
  71. }else{
  72. help();
  73. }
  74. return 0;
  75. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/460583
推荐阅读
相关标签
  

闽ICP备14008679号