What does MethodImplAttribute (InternalCall, Runtime) do for Interop COM interface methods?

In the Windows API Code for the .NET Framework, many MethodImplAttribute COM methods 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); 

The documentation for MethodImplOptions.InternalCall says:

The call is internal, i.e. calls a method implemented in a common language environment.

The documentation for MethodCodeType.Runtime says:

Indicates that the implementation of the method is provided by the runtime.

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

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

+6
source share
1 answer

This is just a side effect of how a Microsoft programmer got an interface declaration. Some of the shell interfaces are available in the type library, for example, IShellItem, so the easiest way to get them into the source code is to run Tlbimp.exe to create an interop library and a disassembler to decompile the assembly into C # code. This also leads to additional attributes generated by Tlbimp.exe, for example [MethodImpl].

Other interfaces are not available in the type library, such as IShellLinkW and IPersistStream, and type libraries from IDL cannot be generated, so the programmer had no choice but to enter them into himself. He missed unnecessary things.

And no, [MethodImpl] is not critical here. These are interface types, so there is no method. Tlbimp.exe is just not very sophisticated.

+8
source

Source: https://habr.com/ru/post/955512/


All Articles