看一看:
[DllImport("User32.dll")]
public static extern StatusCode DisplayConfigGetDeviceInfo(
ref IDisplayConfigInfo a
);
现在我的结构继承自 IDisplayConfigInfo:
var displayConfigTargetDeviceName = new DisplayConfigTargetDeviceName
{
header = new DisplayConfigDeviceInfoHeader
{
adapterId = targetModeInfo.adapterId,
id = targetModeInfo.id,
size = Marshal.SizeOf(typeof(DisplayConfigTargetDeviceName)),
type = DisplayConfigDeviceInfoType.GetTargetName,
}
};
var configTargetDeviceName = (IDisplayConfigInfo) displayConfigTargetDeviceName;
var retval = CCDWrapper.DisplayConfigGetDeviceInfo(ref configTargetDeviceName);
现在有一个问题。retval 将返回“InvalidParameter”值。这是为什么?这是因为我尝试使用界面,但我不明白为什么。
当我明确地说 DisplayConfigGetDeviceInfo() 接受 DisplayConfigTargetDeviceName 而不是接口,并直接将 displayconfigTargetDeviceName 传递给它时,它就可以工作了。
问题是,我不想为每个结构创建 8-9 个重载。请注意,C++ 版本只有一个重载。它会从我传递的指针中找出其余部分。
// 结构:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DisplayConfigTargetDeviceName : IDisplayConfigInfo
{
public DisplayConfigDeviceInfoHeader header;
public DisplayConfigTargetDeviceNameFlags flags;
public DisplayConfigVideoOutputTechnology outputTechnology;
public ushort edidManufactureId;
public ushort edidProductCodeId;
public uint connectorInstance;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string monitorFriendlyDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string monitorDevicePath;
}
[StructLayout(LayoutKind.Sequential)]
public struct DisplayConfigDeviceInfoHeader
{
public DisplayConfigDeviceInfoType type;
public int size;
public LUID adapterId;
public uint id;
}
而 IDisplayConfig 接口为空。