QueryDisplayConfig() 没有正确初始化 pathArray 和 modeArray
QueryDisplayConfig() does not initialize pathArray and modeArray correctly
我正在尝试设置一个简单的一键式屏幕模式更改(扩展 <--> 断开连接)
但是 QueryDisplayConfig
方法没有为我的屏幕分配 ID。
(我正在使用 https://github.com/AArnott/pinvoke 中的 User32 PInvoke 库,以及您可以在下面的代码中找到的库)
我试过了:
- 使用断点单步执行代码,确保每个值都是默认值。
- 使用管理员权限将 VS 提升到 运行。
- 额外确保标志和错误正常运行。
[DllImport("User32.dll")]
public static extern int GetDisplayConfigBufferSizes(uint flags, ref uint numPathArrayElements, ref uint numModeInfoArrayElements);
[DllImport("User32.dll")]
public static extern int QueryDisplayConfig(
uint flags,
ref uint numPathArrayElements, DISPLAYCONFIG_PATH_INFO[] pathArray,
ref uint numModeInfoArrayElements, DISPLAYCONFIG_MODE_INFO[] modeInfoArray,
DISPLAYCONFIG_TOPOLOGY_ID[] currentTopologyId
);
const int QDC_ALL_PATHS = 1;
const int QDC_ONLY_ACTIVE_PATHS = 2;
const int QDC_DATABASE_CURRENT = 4;
public static void CheckDisplays() {
uint numPathArrayElements = 0;
uint numModeInfoArrayElements = 0;
uint filter = QDC_ONLY_ACTIVE_PATHS;
int bufferError = GetDisplayConfigBufferSizes(filter, ref numPathArrayElements, ref numModeInfoArrayElements);
DISPLAYCONFIG_PATH_INFO[] pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
DISPLAYCONFIG_MODE_INFO[] modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
int queryError = QueryDisplayConfig(filter, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, null);
Console.WriteLine();
Console.WriteLine("Elements: " + numPathArrayElements); // Prints the correct amount of connected screens.
Console.WriteLine("BUFFER ERROR: " + bufferError); // Prints 0 -- as in Success.
Console.WriteLine("PATH ERROR: " + queryError); // Prints 0 -- as in Success.
for (int i = 0; i < pathArray.Length; i++) {
if (pathArray[i].sourceInfo.id != 0) { Console.WriteLine($"Path{i} has been initialized correctly!!"); }
// Every object in the array has default values and IDs of 0.
// Nothing prints here.
}
}
每个屏幕都应分配一个 ID 和适当的模式。
相反,一切都有默认值,我似乎被卡住了。
您对 QueryDisplayConfig 的声明是错误的:数组必须是 [Out]
这对我有用:
(我只有1个监视器,但数组中接收到的值与C++中的相同(我从SDK翻译结构headers))
(IntPtr.Zero for currentTopologyId with QDC_ONLY_ACTIVE_PATHS)
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int QueryDisplayConfig(uint flags, ref uint numPathArrayElements, [Out] DISPLAYCONFIG_PATH_INFO[] pathArray,
ref uint modeInfoArrayElements, [Out] DISPLAYCONFIG_MODE_INFO[] modeInfoArray, IntPtr currentTopologyId);
我正在尝试设置一个简单的一键式屏幕模式更改(扩展 <--> 断开连接)
但是 QueryDisplayConfig
方法没有为我的屏幕分配 ID。
(我正在使用 https://github.com/AArnott/pinvoke 中的 User32 PInvoke 库,以及您可以在下面的代码中找到的库)
我试过了:
- 使用断点单步执行代码,确保每个值都是默认值。
- 使用管理员权限将 VS 提升到 运行。
- 额外确保标志和错误正常运行。
[DllImport("User32.dll")]
public static extern int GetDisplayConfigBufferSizes(uint flags, ref uint numPathArrayElements, ref uint numModeInfoArrayElements);
[DllImport("User32.dll")]
public static extern int QueryDisplayConfig(
uint flags,
ref uint numPathArrayElements, DISPLAYCONFIG_PATH_INFO[] pathArray,
ref uint numModeInfoArrayElements, DISPLAYCONFIG_MODE_INFO[] modeInfoArray,
DISPLAYCONFIG_TOPOLOGY_ID[] currentTopologyId
);
const int QDC_ALL_PATHS = 1;
const int QDC_ONLY_ACTIVE_PATHS = 2;
const int QDC_DATABASE_CURRENT = 4;
public static void CheckDisplays() {
uint numPathArrayElements = 0;
uint numModeInfoArrayElements = 0;
uint filter = QDC_ONLY_ACTIVE_PATHS;
int bufferError = GetDisplayConfigBufferSizes(filter, ref numPathArrayElements, ref numModeInfoArrayElements);
DISPLAYCONFIG_PATH_INFO[] pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
DISPLAYCONFIG_MODE_INFO[] modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];
int queryError = QueryDisplayConfig(filter, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, null);
Console.WriteLine();
Console.WriteLine("Elements: " + numPathArrayElements); // Prints the correct amount of connected screens.
Console.WriteLine("BUFFER ERROR: " + bufferError); // Prints 0 -- as in Success.
Console.WriteLine("PATH ERROR: " + queryError); // Prints 0 -- as in Success.
for (int i = 0; i < pathArray.Length; i++) {
if (pathArray[i].sourceInfo.id != 0) { Console.WriteLine($"Path{i} has been initialized correctly!!"); }
// Every object in the array has default values and IDs of 0.
// Nothing prints here.
}
}
每个屏幕都应分配一个 ID 和适当的模式。
相反,一切都有默认值,我似乎被卡住了。
您对 QueryDisplayConfig 的声明是错误的:数组必须是 [Out]
这对我有用:
(我只有1个监视器,但数组中接收到的值与C++中的相同(我从SDK翻译结构headers))
(IntPtr.Zero for currentTopologyId with QDC_ONLY_ACTIVE_PATHS)
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int QueryDisplayConfig(uint flags, ref uint numPathArrayElements, [Out] DISPLAYCONFIG_PATH_INFO[] pathArray,
ref uint modeInfoArrayElements, [Out] DISPLAYCONFIG_MODE_INFO[] modeInfoArray, IntPtr currentTopologyId);