如何使用可以接受许多不同类型的参数来调用/调用函数?

时间:2013-04-20 17:21:17

标签: c# pinvoke

看看:

[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 ++版本只有一个重载。它会从我传给的指针中找出其余部分。

// structs:

[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接口为空。

1 个答案:

答案 0 :(得分:2)

感谢大卫,我能够提出聪明的解决方案(imo),它看起来效果很好。

[DllImport("User32.dll")]
private static extern StatusCode DisplayConfigSetDeviceInfo(IntPtr requestPacket);
public static StatusCode DisplayConfigSetDeviceInfo<T>(ref T displayConfig) 
   where T : IDisplayConfigInfo
{
    return WrapStructureAndCall(ref displayConfig, DisplayConfigSetDeviceInfo);
}


[DllImport("User32.dll")]
private static extern StatusCode DisplayConfigGetDeviceInfo(IntPtr targetDeviceName);
public static StatusCode DisplayConfigGetDeviceInfo<T>(ref T displayConfig) 
  where T : IDisplayConfigInfo
{
    return WrapStructureAndCall(ref displayConfig, DisplayConfigGetDeviceInfo);
}

public static StatusCode WrapStructureAndCall<T>(ref T displayConfig,
    Func<IntPtr, StatusCode> func) where T : IDisplayConfigInfo
{
    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(displayConfig));
    Marshal.StructureToPtr(displayConfig, ptr, false);

    var retval = func(ptr);

    displayConfig = (T)Marshal.PtrToStructure(ptr, displayConfig.GetType());

    Marshal.FreeHGlobal(ptr);
    return retval;
}