文章开头感谢国内唯一(搜索前几页能找到的)完善了背光调节API资料的独苗 :https://blog.csdn.net/weixin_34111819/article/details/86328019
PS:他代码的主干对了,细节处有很多相对于VS的错误,毕竟是DEV写的,这样很难用作工程。
-
#define _CRT_SECURE_NO_WARNINGS
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <windows.h>
-
#include <iostream>
-
#include <Ntddvdeo.h>
-
using
namespace
std;
-
-
#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)
-
-
-
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
-
*/
-
-
/*
-
* char* 和 wchar_t* 互相转换参考博客
-
* https://www.cnblogs.com/icqw/p/4614877.html
-
*/
-
/*
-
* c++代码参考博客
-
* https://blog.csdn.net/weixin_34111819/article/details/86328019
-
*/
-
char temchar[] =
"\\\\.\\LCD";
-
char* szSour = temchar;
-
WCHAR Temp[
128] = {
0 };
-
mbstowcs(Temp, szSour,
strlen(szSour));
-
h = CreateFile(Temp, 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 a;
-
cin >> a;
-
setBrightness(a);
-
return
0;
-
}
编译环境是VS,众所周知VS对代码的要求相对严格。所以这位大佬的代码有些部分需要修改。在这里我会指出修改地方以及对修改的部分进行解释。
首先#include <Ntddvdeo.h>直接引用就可以了不要加前缀,VS会自动寻找这个头文件的。
-
char temchar[] =
"\\\\.\\LCD";
-
char* szSour = temchar;
-
WCHAR Temp[
128] = {
0 };
-
mbstowcs(Temp, szSour,
strlen(szSour));
-
h = CreateFile(Temp, GENERIC_READ | GENERIC_WRITE,
0,
NULL, OPEN_EXISTING,
0,
0);
其次这里的Temp参数,之前大佬的写法是直接“\\\\.\\LCD”,但是这个参数的位置是填到一个WCHAR类型的参数,所以我们要对其进行转换。在转换的过程中VS不允许使用char*指针指向一个字符串常量,所以我们要先将这个字符串常量赋值给一个字符数组,然后再用指针指向它就行了。转换的方法在另一位大佬的博客中,我在代码中已经作为注释给出地址了(感谢各个前辈们做出的贡献)。
最后我要注明一下,这个int setBrightness(int level)中的level的范围是0到100,虽然大多数情况下调到0也能看清,但是万一呢是吧,最好测试的时候别调到零。
上面的是c++代码现在让我们来做成c#模块。(这个模块文末我会放出连接,有百度云和CSDN两个连接,还有gitHub有全部的源代码)我们的思路是将C++编译成DLL模块,然后由c#引用,并在C#中封装成模块的方式来供C#调用。
下面是c++封装DLL的教程(正在准备材料,还没写完,先存着)
建立DLL工程之后对工程稍作修改,改动如下:
将这一句删掉,因为我们要用到这些极少用的内容。
然后将dllmain.cpp的内容改为如下内容
-
// dllmain.cpp : 定义 DLL 应用程序的入口点。
-
#define _CRT_SECURE_NO_WARNINGS
-
#include "pch.h"
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <windows.h>
-
#include <iostream>
-
#include <Ntddvdeo.h>
-
BOOL APIENTRY DllMain(HMODULE hModule,
-
DWORD ul_reason_for_call,
-
LPVOID lpReserved
-
)
-
{
-
switch (ul_reason_for_call)
-
{
-
case DLL_PROCESS_ATTACH:
-
case DLL_THREAD_ATTACH:
-
case DLL_THREAD_DETACH:
-
case DLL_PROCESS_DETACH:
-
break;
-
}
-
return TRUE;
-
}
-
#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)
-
extern
"C" _declspec(dllexport)
void 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
-
*/
-
-
/*
-
* char* 和 wchar_t* 互相转换参考博客
-
* https://www.cnblogs.com/icqw/p/4614877.html
-
*/
-
/*
-
* c++代码参考博客
-
* https://blog.csdn.net/weixin_34111819/article/details/86328019
-
*/
-
char temchar[] =
"\\\\.\\LCD";
-
char* szSour = temchar;
-
WCHAR Temp[
128] = {
0 };
-
mbstowcs(Temp, szSour,
strlen(szSour));
-
h = CreateFile(Temp, 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);
-
}
-
-
注意这里作为c语言导出DLL是有必要的,否则导出后对外名字可能就改变了,不再是你声明的那个函数名了,会给后面的调用造成一定的困难。
-
extern
"C" _declspec(dllexport)
void setBrightness(int level)
-
//这里的C就是C语言形式导出的意思
设置完成之后右键点击如下位置,然后选择生成解决方案。然后右键打开菜单之后选择“在资源管理器中打开”就能找到项目文件夹,然后找到debug文件夹,在文件夹下面就有dll文件,包括dll在内所有文件烤走就能用了。
完成之后开始创建C#版的DLL对c++的DLL进行二次封装
新建完成后,将DLL拖入项目资源,记住这里不能添加引用,非托管类型的DLL没法添加引用。
新建一个类,并填入如下代码
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Runtime.InteropServices;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace
BGLight
-
{
-
public
class
ChangeLight
-
{
-
[
DllImport("BGLightChangeDLL.dll")]
-
public static extern void setBrightness(Int32 level);
-
public void Change(Int32 level)
-
{
-
setBrightness(level);
-
}
-
}
-
}
其中下面两句就是引入DLL的语句
-
[
DllImport("BGLightChangeDLL.dll")]
-
public static extern void setBrightness(Int32 level);
然后同样的步骤生成C#版的DLL。
得到c#版的DLL之后,在其他的c#工程只需要在工程中添加对DLL的引用并using相应的命名空间即可。
c#版:https://download.csdn.net/download/u011471253/15500112
c++版:https://download.csdn.net/download/u011471253/15500113
转载:https://blog.csdn.net/u011471253/article/details/114170009