我可以简化我在ComImport中没有使用的方法签名吗?

时间:2017-11-30 01:50:09

标签: c# com pinvoke com-interop

我想在GetImageDlg上致电IWiaDevMgr2。有许多相当复杂的方法(我没有使用)引用了许多类型(我也没有使用)。由于我找不到自动生成ComImport的TLB或IDL,我宁愿避免手动翻译所有引用的类型。

我可以跳过"从

代替的方法和类型
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("79C07CF1-CBDD-41ee-8EC3-F00080CADA7A")]
public interface IWiaDevMgr2
{
    IEnumWIA_DEV_INFO EnumDeviceInfo(
        int lFlags);

    IWiaItem2 CreateDevice(
        int lFlags,
        [MarshalAs(UnmanagedType.BStr)] string bstrDeviceID);

    // ...snip five other method declarations...

    IWiaItem2 GetImageDlg(
        int lFlags,
        [MarshalAs(UnmanagedType.BStr)] string bstrDeviceID,
        IntPtr IntPtrParent,
        [MarshalAs(UnmanagedType.BStr)] string bstrFolderName,
        [MarshalAs(UnmanagedType.BStr)] string bstrFilename,
        /* [out] */ out int plNumFiles,
        /* [size_is][size_is][out] */ [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5, ArraySubType = UnmanagedType.BStr)] out string[] ppbstrFilePaths);

};

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("79C07CF1-CBDD-41ee-8EC3-F00080CADA7A")]
public interface IWiaDevMgr2_Fake
{
    void VTablePlaceholder0();
    void VTablePlaceholder1();
    void VTablePlaceholder2();
    void VTablePlaceholder3();
    void VTablePlaceholder4();
    void VTablePlaceholder5();
    void VTablePlaceholder6();

    [return: MarshalAs(UnmanagedType.Interface)]
    object GetImageDlg(
        int lFlags,
        [MarshalAs(UnmanagedType.BStr)] string bstrDeviceID,
        IntPtr IntPtrParent,
        [MarshalAs(UnmanagedType.BStr)] string bstrFolderName,
        [MarshalAs(UnmanagedType.BStr)] string bstrFilename,
        /* [out] */ out int plNumFiles,
        /* [size_is][size_is][out] */ [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5, ArraySubType = UnmanagedType.BStr)] out string[] ppbstrFilePaths);

};

一切似乎才能正常工作,只要我不打电话给任何占位符。我能这样做而没有后果吗?

1 个答案:

答案 0 :(得分:2)

当然可以这样做。您是唯一一个将使用此界面定义的人,所以很好。

当然,如果此接口用作从本机到托管的回调,那么这将是一个问题,但在这种情况下,您将以某种方式实现它。实施这种虚拟方法是未来问题的标志。

请注意,这是正式的做法。您可以为占位符使用特殊的_VtblGap{0}_{1}名称,其中0是索引,1是您要跳过的方法数。

请参阅Roslyn中的实现(它也在公共语言基础结构规范中):https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/MetadataReader/ModuleExtensions.cs

        // From IMetaDataEmit::DefineMethod documentation (http://msdn.microsoft.com/en-us/library/ms230861(VS.100).aspx)
        // ----------------------
        // In the case where one or more slots need to be skipped, such as to preserve parity with a COM interface layout, 
        // a dummy method is defined to take up the slot or slots in the v-table; set the dwMethodFlags to the mdRTSpecialName 
        // value of the CorMethodAttr enumeration and specify the name as:
        //
        // _VtblGap<SequenceNumber><_CountOfSlots>
        //
        // where SequenceNumber is the sequence number of the method and CountOfSlots is the number of slots to skip in the v-table. 
        // If CountOfSlots is omitted, 1 is assumed.
        // ----------------------
        //
        // From "Partition II Metadata.doc"
        // ----------------------
        // For COM Interop, an additional class of method names are permitted:
        // _VtblGap<SequenceNumber><_CountOfSlots>
        // where <SequenceNumber> and <CountOfSlots> are decimal numbers
        // ----------------------

所以在你的情况下,它将是:

[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("79C07CF1-CBDD-41ee-8EC3-F00080CADA7A")]
public interface IWiaDevMgr2_Fake
{
    void _VtblGap1_7(); // skip seven methods

    ...
};