MethodImplAttribute(InternalCall,Runtime)对COM Interop接口的方法有什么作用? [英] What does MethodImplAttribute(InternalCall, Runtime) do for methods of COM Interop interfaces?

查看:203
本文介绍了MethodImplAttribute(InternalCall,Runtime)对COM Interop接口的方法有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows API代码包for .NET Framework 中,COM Interop接口的许多方法都使用 MethodImplAttribute ,例如:

 内部接口IShellItem 
{
[PreserveSig]
[MethodImpl(
MethodImplOptions.InternalCall,
MethodCodeType = MethodCodeType.Runtime)]
HResult BindToHandler(
[In] IntPtr pbc,[In] ref参考文献:[引用] [引用] [引用] [帮助]

MethodImplOptions.InternalCall 值说:


调用是内部调用,即调用在公共语言运行时实现的方法。


MethodCodeType.Runtime 的文档


b

但是,如果我理解正确,两个语句都不正确。 IShellItem 根据MSDN实现在 shell32.dll 中。 shell32.dll 不是CLR的一部分。有趣的是,不是所有的方法都有 MethodImplAttribute IShellFolder 没有,没有, IPersistStream 不等等。



为什么是 MethodImplAttribute 应用于一些COM Interop接口?在这种情况下是什么意思,它如何修改interop的行为?

解决方案

这只是一个副作用方式微软程序员获得了接口声明。一些shell接口在类型库(如IShellItem)中可用,因此将它们转换为源代码的最简单方法是运行Tlbimp.exe生成互操作库,反汇编器将汇编转换为C#代码。这也带来了Tlbimp.exe生成的额外属性,如[MethodImpl]。



其他接口在类型库中不可用,如IShellLinkW和IPeristStream,从IDL生成类型库,所以程序员别无选择,只能自己输入。他跳过了不必要的东西。



不,[MethodImpl]在这里不重要。这些是接口类型,所以没有方法。 Tlbimp.exe只是不是很复杂的。


In Windows API Code Pack for .NET Framework, many methods of COM Interop interfaces are decorated with MethodImplAttribute, for example:

internal interface IShellItem
{
    [PreserveSig]
    [MethodImpl(
        MethodImplOptions.InternalCall,
        MethodCodeType = MethodCodeType.Runtime)]
    HResult BindToHandler(
        [In] IntPtr pbc, [In] ref Guid bhid, [In] ref Guid riid,
        [Out, MarshalAs(UnmanagedType.Interface)] out IShellFolder ppv);

Documentation for MethodImplOptions.InternalCall value says:

The call is internal, that is, it calls a method that is implemented within the common language runtime.

Documentation for MethodCodeType.Runtime says:

Specifies that the method implementation is provided by the runtime.

But, if I understand correctly, neither statement is correct. IShellItem is implemented in shell32.dll, according to MSDN. shell32.dll is not a part of CLR. What's interesting, not all methods have the MethodImplAttribute. IShellFolder does, IShellLinkW doesn't, IShellLibrary does, IPersistStream doesn't etc.

Why is MethodImplAttribute applied to some of the COM Interop interfaces? What does it mean in this case and how does it modify behavior of interop?

解决方案

It is just a side-effect from the way the Microsoft programmer obtained the interface declaration. Some of the shell interfaces are available in a type library, like IShellItem, so the easiest way to get them into source code is to run Tlbimp.exe to generate the interop library and a disassembler to decompile the assembly into C# code. Which also brings in the extra attributes that Tlbimp.exe generates, like [MethodImpl].

Other interfaces are not available in a type library, like IShellLinkW and IPeristStream, nor can a type library be generated from the IDL so the programmer had no choice but to type them in himself. He skipped the unnecessary stuff.

And no, [MethodImpl] isn't critical here. These are interface types so there is no method anyway. Tlbimp.exe just isn't terribly sophisticated about it.

这篇关于MethodImplAttribute(InternalCall,Runtime)对COM Interop接口的方法有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆