gpt4 book ai didi

interop - GetPhysicalMonitorsFromHMONITOR 返回的句柄始终为空

转载 作者:行者123 更新时间:2023-12-01 04:22:09 25 4
gpt4 key购买 nike

在媒体基础 SDK 上有 GetPhysicalMonitorsFromHMONITOR功能
我正在尝试使用 C# 来实现,但没有运气......

在返回的 PHYSICAL_MONITOR[] 中,该函数返回监视器的字符串描述,但由于一些神秘的原因,hPhysicalMonitor 句柄保持为 0。

我已经使用 P/Invoke Interop Assistant 生成了签名,并稍作修改。

PHYSICAL_MONITOR 结构或其他任何东西是否需要进一步调整?

谢谢你。

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WindowsFormsApplication1;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public enum MC_DISPLAY_TECHNOLOGY_TYPE
{
MC_SHADOW_MASK_CATHODE_RAY_TUBE,

MC_APERTURE_GRILL_CATHODE_RAY_TUBE,

MC_THIN_FILM_TRANSISTOR,

MC_LIQUID_CRYSTAL_ON_SILICON,

MC_PLASMA,

MC_ORGANIC_LIGHT_EMITTING_DIODE,

MC_ELECTROLUMINESCENT,

MC_MICROELECTROMECHANICAL,

MC_FIELD_EMISSION_DEVICE,
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szPhysicalMonitorDescription;
}

#region Imports

[DllImport("user32.dll", EntryPoint = "MonitorFromWindow")]
public static extern IntPtr MonitorFromWindow(
[In] IntPtr hwnd, uint dwFlags);

[DllImport("dxva2.dll", EntryPoint = "GetMonitorTechnologyType")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMonitorTechnologyType(
IntPtr hMonitor, ref MC_DISPLAY_TECHNOLOGY_TYPE pdtyDisplayTechnologyType);

[DllImport("dxva2.dll", EntryPoint = "GetMonitorCapabilities")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMonitorCapabilities(
IntPtr hMonitor, ref uint pdwMonitorCapabilities, ref uint pdwSupportedColorTemperatures);

[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyPhysicalMonitors(
uint dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR[] pPhysicalMonitorArray);

[DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(
IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

[DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPhysicalMonitorsFromHMONITOR(
IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);

#endregion

public Form1() { InitializeComponent(); }

private void Form1_Load(object sender, EventArgs e)
{
// Get monitor handle.
uint dwFlags = 0u;
IntPtr ptr = MonitorFromWindow(Handle, dwFlags);

// Get number of physical monitors.
uint pdwNumberOfPhysicalMonitors = 0u;
bool b1 = GetNumberOfPhysicalMonitorsFromHMONITOR(ptr, ref pdwNumberOfPhysicalMonitors);

if (b1)
{
// Get physical monitors.
uint dwPhysicalMonitorArraySize = 0u;
dwPhysicalMonitorArraySize = pdwNumberOfPhysicalMonitors;
PHYSICAL_MONITOR[] pPhysicalMonitorArray = new PHYSICAL_MONITOR[dwPhysicalMonitorArraySize];

//NOTE : Handles remain null !
bool b2 = GetPhysicalMonitorsFromHMONITOR(ptr, dwPhysicalMonitorArraySize, pPhysicalMonitorArray);

if (pPhysicalMonitorArray[0].hPhysicalMonitor
== IntPtr.Zero)
{
throw new Exception("ERROR !");
}

// Monitor has capabilities to do that ?
if (b2)
{
uint pdwMonitorCapabilities = 0u;
uint pdwSupportedColorTemperatures = 0u;
bool b3 = GetMonitorCapabilities(
ptr, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);

// If yes, get technology type.
if (b3)
{
MC_DISPLAY_TECHNOLOGY_TYPE type = MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;

bool b4 = GetMonitorTechnologyType(ptr, ref type);
if (b4)
{
// Do work.
}
else
{
throw new Exception("Couldn't get monitor technology type.");
}
}
else
{
throw new Exception("Couldn't get monitor capabilities.");
}
}
else
{
throw new Exception("The monitor doesn't have the required capabilities.");
}

bool b5 = DestroyPhysicalMonitors(dwPhysicalMonitorArraySize, ref pPhysicalMonitorArray);
if (!b5)
{
throw new Exception("Couldn't destroy physical monitors.");
}
}
else
{
throw new Exception("Couldn't get number of physical monitors.");
}
}
}
}

最佳答案

你的声明:

The function returns the string description of the monitor but for some mysterious reasons, the hMonitor handle remains at 0.



是正确的。如果您查看文档 here ,你会看到 hMonitor 显然是一个 [in]参数,不会改变。

更新以下评论:

抱歉,没有意识到您的意思是在结构中返回物理句柄。我能找到的有关该特定问题的所有信息似乎都表明您的显示器可能不完全兼容 DDC/CI(例如, here )。

根据 MSDN 上针对该特定调用的文档,您所有的结构定义对我来说都很好。事实上,它正在为您填充描述。

GetNumberOfPhysicalMonitorsFromHMONITOR 返回的物理监视器数量的值是多少? ( pdwNumberOfPhysicalMonitors )?

另外,你的 PHYSICAL_MONITOR的尺寸是多少?结构,你是在 32 位还是 64 位上运行?

关于interop - GetPhysicalMonitorsFromHMONITOR 返回的句柄始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/846518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com