При использовании API QueryDisplayConfig/SetDisplayedConfig для изменения разрешения экрана независимо от операции возвращается код 87. Я не могу знать, что стало причиной этого. Я использовал метод размеров буфера VNet DisplayedConfig, но мне все равно не удалось
[Изображение]: https://i.sstatic.net/gY0OfDlI.png
Вот мой полный код:using System;
using System.IO;
using System.Runtime.InteropServices;
class Program
{
// Structure definition
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_PATH_INFO
{
public uint pathId;
public uint sourceMode;
public uint targetMode;
public uint statusFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_MODE_INFO
{
public uint width;
public uint height;
public uint refreshRate;
}
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_PATH_INFO_V2
{
public uint pathId;
public DISPLAYCONFIG_MODE_INFO sourceMode;
public DISPLAYCONFIG_MODE_INFO targetMode;
public uint statusFlags;
}
// API
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetDisplayConfigBufferSizes(uint pathInfoCount, uint modeInfoCount, ref uint pathInfoSize, ref uint modeInfoSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int QueryDisplayConfig(uint flags, ref uint pathInfoSize, IntPtr pathInfo, ref uint modeInfoSize, IntPtr modeInfo, IntPtr currentSettings);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetDisplayConfig(uint flags, uint pathInfoCount, IntPtr pathInfo, uint modeInfoCount, IntPtr modeInfo, uint currentSettings);
const uint SDC_TOPOLOGY_CLONE = 0x00000001;
const uint SDC_TOPOLOGY_EXTEND = 0x00000002;
const uint SDC_TOPOLOGY_INTERNAL = 0x00000004;
// log
private static void Log(string message)
{
string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}";
Console.WriteLine(logMessage);
// Optionally log to a file
try
{
string logFile = "screen_resolution_change.log";
File.AppendAllText(logFile, logMessage + Environment.NewLine);
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to log file: {ex.Message}");
}
}
public static void Main()
{
uint pathInfoSize = 0;
uint modeInfoSize = 0;
// Get buffer size
Log("Getting buffer sizes for display configuration...");
int result = GetDisplayConfigBufferSizes(0, 0, ref pathInfoSize, ref modeInfoSize);
if (result != 0)
{
Log($"Error in GetDisplayConfigBufferSizes: {result}");
return;
}
Log($"Buffer sizes - PathInfoSize: {pathInfoSize}, ModeInfoSize: {modeInfoSize}");
// Allocate memory for path information and mode information
IntPtr pathInfoBuffer = Marshal.AllocHGlobal((int)pathInfoSize);
IntPtr modeInfoBuffer = Marshal.AllocHGlobal((int)modeInfoSize);
try
{
// Query display configuration
Log("Querying current display configuration...");
result = QueryDisplayConfig(0, ref pathInfoSize, pathInfoBuffer, ref modeInfoSize, modeInfoBuffer, IntPtr.Zero);
if (result != 0)
{
Log($"Error in QueryDisplayConfig: {result}");
return;
}
Log("Current display configuration retrieved successfully.");
// Obtain and modify resolution
DISPLAYCONFIG_PATH_INFO_V2 pathInfo = Marshal.PtrToStructure(pathInfoBuffer);
DISPLAYCONFIG_MODE_INFO newMode = pathInfo.sourceMode;
newMode.width = 1920; // New resolution width
newMode.height = 1080; // New resolution height
// Output the modified resolution
Log($"Changing resolution to {newMode.width}x{newMode.height}");
// Modify display settings
Marshal.StructureToPtr(newMode, modeInfoBuffer, false);
// Apply new display configuration
Log("Applying new display configuration...");
result = SetDisplayConfig(0, 1, pathInfoBuffer, 1, modeInfoBuffer, 0);
if (result != 0)
{
Log($"Error in SetDisplayConfig: {result}");
}
else
{
Log("Resolution changed successfully!");
}
}
finally
{
// free memory
Marshal.FreeHGlobal(pathInfoBuffer);
Marshal.FreeHGlobal(modeInfoBuffer);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ion-failed
Не удалось установить разрешение API QueryDisplayConfig/SetDisplayConfig. ⇐ C#
Место общения программистов C#
1732069410
Anonymous
При использовании API QueryDisplayConfig/SetDisplayedConfig для изменения разрешения экрана независимо от операции возвращается код 87. Я не могу знать, что стало причиной этого. Я использовал метод размеров буфера VNet DisplayedConfig, но мне все равно не удалось
[Изображение]: https://i.sstatic.net/gY0OfDlI.png
Вот мой полный код:using System;
using System.IO;
using System.Runtime.InteropServices;
class Program
{
// Structure definition
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_PATH_INFO
{
public uint pathId;
public uint sourceMode;
public uint targetMode;
public uint statusFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_MODE_INFO
{
public uint width;
public uint height;
public uint refreshRate;
}
[StructLayout(LayoutKind.Sequential)]
public struct DISPLAYCONFIG_PATH_INFO_V2
{
public uint pathId;
public DISPLAYCONFIG_MODE_INFO sourceMode;
public DISPLAYCONFIG_MODE_INFO targetMode;
public uint statusFlags;
}
// API
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetDisplayConfigBufferSizes(uint pathInfoCount, uint modeInfoCount, ref uint pathInfoSize, ref uint modeInfoSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int QueryDisplayConfig(uint flags, ref uint pathInfoSize, IntPtr pathInfo, ref uint modeInfoSize, IntPtr modeInfo, IntPtr currentSettings);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetDisplayConfig(uint flags, uint pathInfoCount, IntPtr pathInfo, uint modeInfoCount, IntPtr modeInfo, uint currentSettings);
const uint SDC_TOPOLOGY_CLONE = 0x00000001;
const uint SDC_TOPOLOGY_EXTEND = 0x00000002;
const uint SDC_TOPOLOGY_INTERNAL = 0x00000004;
// log
private static void Log(string message)
{
string logMessage = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}";
Console.WriteLine(logMessage);
// Optionally log to a file
try
{
string logFile = "screen_resolution_change.log";
File.AppendAllText(logFile, logMessage + Environment.NewLine);
}
catch (Exception ex)
{
Console.WriteLine($"Error writing to log file: {ex.Message}");
}
}
public static void Main()
{
uint pathInfoSize = 0;
uint modeInfoSize = 0;
// Get buffer size
Log("Getting buffer sizes for display configuration...");
int result = GetDisplayConfigBufferSizes(0, 0, ref pathInfoSize, ref modeInfoSize);
if (result != 0)
{
Log($"Error in GetDisplayConfigBufferSizes: {result}");
return;
}
Log($"Buffer sizes - PathInfoSize: {pathInfoSize}, ModeInfoSize: {modeInfoSize}");
// Allocate memory for path information and mode information
IntPtr pathInfoBuffer = Marshal.AllocHGlobal((int)pathInfoSize);
IntPtr modeInfoBuffer = Marshal.AllocHGlobal((int)modeInfoSize);
try
{
// Query display configuration
Log("Querying current display configuration...");
result = QueryDisplayConfig(0, ref pathInfoSize, pathInfoBuffer, ref modeInfoSize, modeInfoBuffer, IntPtr.Zero);
if (result != 0)
{
Log($"Error in QueryDisplayConfig: {result}");
return;
}
Log("Current display configuration retrieved successfully.");
// Obtain and modify resolution
DISPLAYCONFIG_PATH_INFO_V2 pathInfo = Marshal.PtrToStructure(pathInfoBuffer);
DISPLAYCONFIG_MODE_INFO newMode = pathInfo.sourceMode;
newMode.width = 1920; // New resolution width
newMode.height = 1080; // New resolution height
// Output the modified resolution
Log($"Changing resolution to {newMode.width}x{newMode.height}");
// Modify display settings
Marshal.StructureToPtr(newMode, modeInfoBuffer, false);
// Apply new display configuration
Log("Applying new display configuration...");
result = SetDisplayConfig(0, 1, pathInfoBuffer, 1, modeInfoBuffer, 0);
if (result != 0)
{
Log($"Error in SetDisplayConfig: {result}");
}
else
{
Log("Resolution changed successfully!");
}
}
finally
{
// free memory
Marshal.FreeHGlobal(pathInfoBuffer);
Marshal.FreeHGlobal(modeInfoBuffer);
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79201924/querydisplayconfig-setdisplayconfig-apis-set-resolution-failed[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия